blob: 7093d749589b4931b3d691c4c117a27db5ad6a06 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110020#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110022#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
39#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_attr.h"
46#include "xfs_rw.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_quota.h"
49#include "xfs_utils.h"
Nathan Scotta844f452005-11-02 14:38:42 +110050#include "xfs_rtalloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "xfs_trans_space.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_log_priv.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100053#include "xfs_filestream.h"
Christoph Hellwig993386c2007-08-28 16:12:30 +100054#include "xfs_vnodeops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Christoph Hellwig993386c2007-08-28 16:12:30 +100056int
Linus Torvalds1da177e2005-04-16 15:20:36 -070057xfs_open(
Christoph Hellwig993386c2007-08-28 16:12:30 +100058 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 int mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
63 return XFS_ERROR(EIO);
64
65 /*
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
68 */
Christoph Hellwig993386c2007-08-28 16:12:30 +100069 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 mode = xfs_ilock_map_shared(ip);
71 if (ip->i_d.di_nextents > 0)
72 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
73 xfs_iunlock(ip, mode);
74 }
75 return 0;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * xfs_setattr
80 */
81int
82xfs_setattr(
Christoph Hellwig993386c2007-08-28 16:12:30 +100083 xfs_inode_t *ip,
Nathan Scott8285fb52006-06-09 17:07:12 +100084 bhv_vattr_t *vap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int flags,
86 cred_t *credp)
87{
Christoph Hellwig993386c2007-08-28 16:12:30 +100088 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int mask;
91 int code;
92 uint lock_flags;
93 uint commit_flags=0;
94 uid_t uid=0, iuid=0;
95 gid_t gid=0, igid=0;
96 int timeflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 xfs_prid_t projid=0, iprojid=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
99 int file_owner;
Dean Roehrich5fcbab3552005-05-05 13:27:19 -0700100 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100102 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Christoph Hellwigbd186aa2007-08-30 17:21:12 +1000104 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return XFS_ERROR(EROFS);
106
107 /*
108 * Cannot set certain attributes.
109 */
110 mask = vap->va_mask;
111 if (mask & XFS_AT_NOSET) {
112 return XFS_ERROR(EINVAL);
113 }
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (XFS_FORCED_SHUTDOWN(mp))
116 return XFS_ERROR(EIO);
117
118 /*
119 * Timestamps do not need to be logged and hence do not
120 * need to be done within a transaction.
121 */
122 if (mask & XFS_AT_UPDTIMES) {
123 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
124 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
125 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
126 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
127 xfs_ichgtime(ip, timeflags);
128 return 0;
129 }
130
131 olddquot1 = olddquot2 = NULL;
132 udqp = gdqp = NULL;
133
134 /*
135 * If disk quotas is on, we make sure that the dquots do exist on disk,
136 * before we start any other transactions. Trying to do this later
137 * is messy. We don't care to take a readlock to look at the ids
138 * in inode here, because we can't hold it across the trans_reserve.
139 * If the IDs do change before we take the ilock, we're covered
140 * because the i_*dquot fields will get updated anyway.
141 */
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000142 if (XFS_IS_QUOTA_ON(mp) &&
143 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 uint qflags = 0;
145
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000146 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 uid = vap->va_uid;
148 qflags |= XFS_QMOPT_UQUOTA;
149 } else {
150 uid = ip->i_d.di_uid;
151 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000152 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 gid = vap->va_gid;
154 qflags |= XFS_QMOPT_GQUOTA;
155 } else {
156 gid = ip->i_d.di_gid;
157 }
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000158 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
159 projid = vap->va_projid;
160 qflags |= XFS_QMOPT_PQUOTA;
161 } else {
162 projid = ip->i_d.di_projid;
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /*
165 * We take a reference when we initialize udqp and gdqp,
166 * so it is important that we never blindly double trip on
167 * the same variable. See xfs_create() for an example.
168 */
169 ASSERT(udqp == NULL);
170 ASSERT(gdqp == NULL);
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000171 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
172 &udqp, &gdqp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (code)
Jesper Juhl014c2542006-01-15 02:37:08 +0100174 return code;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 /*
178 * For the other attributes, we acquire the inode lock and
179 * first do an error checking pass.
180 */
181 tp = NULL;
182 lock_flags = XFS_ILOCK_EXCL;
Dean Roehrich5fcbab3552005-05-05 13:27:19 -0700183 if (flags & ATTR_NOLOCK)
184 need_iolock = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (!(mask & XFS_AT_SIZE)) {
186 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
187 (mp->m_flags & XFS_MOUNT_WSYNC)) {
188 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
189 commit_flags = 0;
190 if ((code = xfs_trans_reserve(tp, 0,
191 XFS_ICHANGE_LOG_RES(mp), 0,
192 0, 0))) {
193 lock_flags = 0;
194 goto error_return;
195 }
196 }
197 } else {
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000198 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 !(flags & ATTR_DMI)) {
200 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100201 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 vap->va_size, 0, dmflags, NULL);
203 if (code) {
204 lock_flags = 0;
205 goto error_return;
206 }
207 }
208 if (need_iolock)
209 lock_flags |= XFS_IOLOCK_EXCL;
210 }
211
212 xfs_ilock(ip, lock_flags);
213
214 /* boolean: are we the file owner? */
215 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
216
217 /*
218 * Change various properties of a file.
219 * Only the owner or users with CAP_FOWNER
220 * capability may do these things.
221 */
222 if (mask &
223 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
224 XFS_AT_GID|XFS_AT_PROJID)) {
225 /*
226 * CAP_FOWNER overrides the following restrictions:
227 *
228 * The user ID of the calling process must be equal
229 * to the file owner ID, except in cases where the
230 * CAP_FSETID capability is applicable.
231 */
232 if (!file_owner && !capable(CAP_FOWNER)) {
233 code = XFS_ERROR(EPERM);
234 goto error_return;
235 }
236
237 /*
238 * CAP_FSETID overrides the following restrictions:
239 *
240 * The effective user ID of the calling process shall match
241 * the file owner when setting the set-user-ID and
242 * set-group-ID bits on that file.
243 *
244 * The effective group ID or one of the supplementary group
245 * IDs of the calling process shall match the group owner of
246 * the file when setting the set-group-ID bit on that file
247 */
248 if (mask & XFS_AT_MODE) {
249 mode_t m = 0;
250
251 if ((vap->va_mode & S_ISUID) && !file_owner)
252 m |= S_ISUID;
253 if ((vap->va_mode & S_ISGID) &&
254 !in_group_p((gid_t)ip->i_d.di_gid))
255 m |= S_ISGID;
256#if 0
257 /* Linux allows this, Irix doesn't. */
Christoph Hellwig42173f62008-04-22 17:33:25 +1000258 if ((vap->va_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 m |= S_ISVTX;
260#endif
261 if (m && !capable(CAP_FSETID))
262 vap->va_mode &= ~m;
263 }
264 }
265
266 /*
267 * Change file ownership. Must be the owner or privileged.
268 * If the system was configured with the "restricted_chown"
269 * option, the owner is not permitted to give away the file,
270 * and can change the group id only to a group of which he
271 * or she is a member.
272 */
273 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
274 /*
275 * These IDs could have changed since we last looked at them.
276 * But, we're assured that if the ownership did change
277 * while we didn't have the inode locked, inode's dquot(s)
278 * would have changed also.
279 */
280 iuid = ip->i_d.di_uid;
281 iprojid = ip->i_d.di_projid;
282 igid = ip->i_d.di_gid;
283 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
284 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
285 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
286 iprojid;
287
288 /*
289 * CAP_CHOWN overrides the following restrictions:
290 *
291 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
292 * shall override the restriction that a process cannot
293 * change the user ID of a file it owns and the restriction
294 * that the group ID supplied to the chown() function
295 * shall be equal to either the group ID or one of the
296 * supplementary group IDs of the calling process.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298 if (restricted_chown &&
299 (iuid != uid || (igid != gid &&
300 !in_group_p((gid_t)gid))) &&
301 !capable(CAP_CHOWN)) {
302 code = XFS_ERROR(EPERM);
303 goto error_return;
304 }
305 /*
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000306 * Do a quota reservation only if uid/projid/gid is actually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * going to change.
308 */
309 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000310 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
312 ASSERT(tp);
313 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
314 capable(CAP_FOWNER) ?
315 XFS_QMOPT_FORCE_RES : 0);
316 if (code) /* out of quota */
317 goto error_return;
318 }
319 }
320
321 /*
322 * Truncate file. Must have write permission and not be a directory.
323 */
324 if (mask & XFS_AT_SIZE) {
325 /* Short circuit the truncate case for zero length files */
326 if ((vap->va_size == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000327 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 xfs_iunlock(ip, XFS_ILOCK_EXCL);
329 lock_flags &= ~XFS_ILOCK_EXCL;
330 if (mask & XFS_AT_CTIME)
331 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
332 code = 0;
333 goto error_return;
334 }
335
Christoph Hellwig42173f62008-04-22 17:33:25 +1000336 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 code = XFS_ERROR(EISDIR);
338 goto error_return;
Christoph Hellwig42173f62008-04-22 17:33:25 +1000339 } else if (!S_ISREG(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 code = XFS_ERROR(EINVAL);
341 goto error_return;
342 }
343 /*
344 * Make sure that the dquots are attached to the inode.
345 */
346 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
347 goto error_return;
348 }
349
350 /*
351 * Change file access or modified times.
352 */
353 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
354 if (!file_owner) {
355 if ((flags & ATTR_UTIME) &&
356 !capable(CAP_FOWNER)) {
357 code = XFS_ERROR(EPERM);
358 goto error_return;
359 }
360 }
361 }
362
363 /*
364 * Change extent size or realtime flag.
365 */
366 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
367 /*
368 * Can't change extent size if any extents are allocated.
369 */
Eric Sandeene94af022005-11-02 15:10:41 +1100370 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
372 vap->va_extsize) ) {
373 code = XFS_ERROR(EINVAL); /* EFBIG? */
374 goto error_return;
375 }
376
377 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 * Can't change realtime flag if any extents are allocated.
379 */
Eric Sandeene94af022005-11-02 15:10:41 +1100380 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
381 (mask & XFS_AT_XFLAGS) &&
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100382 (XFS_IS_REALTIME_INODE(ip)) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
384 code = XFS_ERROR(EINVAL); /* EFBIG? */
385 goto error_return;
386 }
387 /*
388 * Extent size must be a multiple of the appropriate block
389 * size, if set at all.
390 */
391 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
392 xfs_extlen_t size;
393
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100394 if (XFS_IS_REALTIME_INODE(ip) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ((mask & XFS_AT_XFLAGS) &&
396 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
397 size = mp->m_sb.sb_rextsize <<
398 mp->m_sb.sb_blocklog;
399 } else {
400 size = mp->m_sb.sb_blocksize;
401 }
402 if (vap->va_extsize % size) {
403 code = XFS_ERROR(EINVAL);
404 goto error_return;
405 }
406 }
407 /*
408 * If realtime flag is set then must have realtime data.
409 */
410 if ((mask & XFS_AT_XFLAGS) &&
411 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
412 if ((mp->m_sb.sb_rblocks == 0) ||
413 (mp->m_sb.sb_rextsize == 0) ||
414 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
415 code = XFS_ERROR(EINVAL);
416 goto error_return;
417 }
418 }
419
420 /*
421 * Can't modify an immutable/append-only file unless
422 * we have appropriate permission.
423 */
424 if ((mask & XFS_AT_XFLAGS) &&
425 (ip->i_d.di_flags &
426 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
427 (vap->va_xflags &
428 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
429 !capable(CAP_LINUX_IMMUTABLE)) {
430 code = XFS_ERROR(EPERM);
431 goto error_return;
432 }
433 }
434
435 /*
436 * Now we can make the changes. Before we join the inode
437 * to the transaction, if XFS_AT_SIZE is set then take care of
438 * the part of the truncation that must be done without the
439 * inode lock. This needs to be done before joining the inode
440 * to the transaction, because the inode cannot be unlocked
441 * once it is a part of the transaction.
442 */
443 if (mask & XFS_AT_SIZE) {
444 code = 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000445 if ((vap->va_size > ip->i_size) &&
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100446 (flags & ATTR_NOSIZETOK) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 code = xfs_igrow_start(ip, vap->va_size, credp);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000450
451 /*
452 * We are going to log the inode size change in this
453 * transaction so any previous writes that are beyond the on
454 * disk EOF and the new EOF that have not been written out need
455 * to be written here. If we do not write the data out, we
456 * expose ourselves to the null files problem.
457 *
458 * Only flush from the on disk size to the smaller of the in
459 * memory file size or the new size as that's the range we
460 * really care about here and prevents waiting for other data
461 * not within the range we care about here.
462 */
463 if (!code &&
464 (ip->i_size != ip->i_d.di_size) &&
465 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000466 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000467 ip->i_d.di_size, vap->va_size,
468 XFS_B_ASYNC, FI_NONE);
469 }
470
471 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000472 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!code)
475 code = xfs_itruncate_data(ip, vap->va_size);
476 if (code) {
477 ASSERT(tp == NULL);
478 lock_flags &= ~XFS_ILOCK_EXCL;
479 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
480 goto error_return;
481 }
482 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
483 if ((code = xfs_trans_reserve(tp, 0,
484 XFS_ITRUNCATE_LOG_RES(mp), 0,
485 XFS_TRANS_PERM_LOG_RES,
486 XFS_ITRUNCATE_LOG_COUNT))) {
487 xfs_trans_cancel(tp, 0);
488 if (need_iolock)
489 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
490 return code;
491 }
492 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
493 xfs_ilock(ip, XFS_ILOCK_EXCL);
494 }
495
496 if (tp) {
497 xfs_trans_ijoin(tp, ip, lock_flags);
498 xfs_trans_ihold(tp, ip);
499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /*
502 * Truncate file. Must have write permission and not be a directory.
503 */
504 if (mask & XFS_AT_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100505 /*
506 * Only change the c/mtime if we are changing the size
507 * or we are explicitly asked to change it. This handles
508 * the semantic difference between truncate() and ftruncate()
509 * as implemented in the VFS.
510 */
511 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
512 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
513
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000514 if (vap->va_size > ip->i_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 xfs_igrow_finish(tp, ip, vap->va_size,
516 !(flags & ATTR_DMI));
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000517 } else if ((vap->va_size <= ip->i_size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
519 /*
520 * signal a sync transaction unless
521 * we're truncating an already unlinked
522 * file on a wsync filesystem
523 */
524 code = xfs_itruncate_finish(&tp, ip,
525 (xfs_fsize_t)vap->va_size,
526 XFS_DATA_FORK,
527 ((ip->i_d.di_nlink != 0 ||
528 !(mp->m_flags & XFS_MOUNT_WSYNC))
529 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000530 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000532 /*
533 * Truncated "down", so we're removing references
534 * to old data here - if we now delay flushing for
535 * a long time, we expose ourselves unduly to the
536 * notorious NULL files problem. So, we mark this
537 * vnode and flush it when the file is closed, and
538 * do not wait the usual (long) time for writeout.
539 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000540 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
543
544 /*
545 * Change file access modes.
546 */
547 if (mask & XFS_AT_MODE) {
548 ip->i_d.di_mode &= S_IFMT;
549 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
550
551 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
552 timeflags |= XFS_ICHGTIME_CHG;
553 }
554
555 /*
556 * Change file ownership. Must be the owner or privileged.
557 * If the system was configured with the "restricted_chown"
558 * option, the owner is not permitted to give away the file,
559 * and can change the group id only to a group of which he
560 * or she is a member.
561 */
562 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
563 /*
564 * CAP_FSETID overrides the following restrictions:
565 *
566 * The set-user-ID and set-group-ID bits of a file will be
567 * cleared upon successful return from chown()
568 */
569 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
570 !capable(CAP_FSETID)) {
571 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
572 }
573
574 /*
575 * Change the ownerships and register quota modifications
576 * in the transaction.
577 */
578 if (iuid != uid) {
579 if (XFS_IS_UQUOTA_ON(mp)) {
580 ASSERT(mask & XFS_AT_UID);
581 ASSERT(udqp);
582 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
583 &ip->i_udquot, udqp);
584 }
585 ip->i_d.di_uid = uid;
586 }
587 if (igid != gid) {
588 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000589 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ASSERT(mask & XFS_AT_GID);
591 ASSERT(gdqp);
592 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
593 &ip->i_gdquot, gdqp);
594 }
595 ip->i_d.di_gid = gid;
596 }
597 if (iprojid != projid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000598 if (XFS_IS_PQUOTA_ON(mp)) {
599 ASSERT(!XFS_IS_GQUOTA_ON(mp));
600 ASSERT(mask & XFS_AT_PROJID);
601 ASSERT(gdqp);
602 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
603 &ip->i_gdquot, gdqp);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 ip->i_d.di_projid = projid;
606 /*
607 * We may have to rev the inode as well as
608 * the superblock version number since projids didn't
609 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
610 */
611 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
612 xfs_bump_ino_vers2(tp, ip);
613 }
614
615 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
616 timeflags |= XFS_ICHGTIME_CHG;
617 }
618
619
620 /*
621 * Change file access or modified times.
622 */
623 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
624 if (mask & XFS_AT_ATIME) {
625 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
626 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
627 ip->i_update_core = 1;
628 timeflags &= ~XFS_ICHGTIME_ACC;
629 }
630 if (mask & XFS_AT_MTIME) {
631 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
632 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
633 timeflags &= ~XFS_ICHGTIME_MOD;
634 timeflags |= XFS_ICHGTIME_CHG;
635 }
636 if (tp && (flags & ATTR_UTIME))
637 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
638 }
639
640 /*
641 * Change XFS-added attributes.
642 */
643 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
644 if (mask & XFS_AT_EXTSIZE) {
645 /*
646 * Converting bytes to fs blocks.
647 */
648 ip->i_d.di_extsize = vap->va_extsize >>
649 mp->m_sb.sb_blocklog;
650 }
651 if (mask & XFS_AT_XFLAGS) {
652 uint di_flags;
653
654 /* can't set PREALLOC this way, just preserve it */
655 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
656 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
657 di_flags |= XFS_DIFLAG_IMMUTABLE;
658 if (vap->va_xflags & XFS_XFLAG_APPEND)
659 di_flags |= XFS_DIFLAG_APPEND;
660 if (vap->va_xflags & XFS_XFLAG_SYNC)
661 di_flags |= XFS_DIFLAG_SYNC;
662 if (vap->va_xflags & XFS_XFLAG_NOATIME)
663 di_flags |= XFS_DIFLAG_NOATIME;
664 if (vap->va_xflags & XFS_XFLAG_NODUMP)
665 di_flags |= XFS_DIFLAG_NODUMP;
Nathan Scott365ca832005-06-21 15:39:12 +1000666 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
667 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000668 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
669 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000670 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
671 di_flags |= XFS_DIFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
673 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
674 di_flags |= XFS_DIFLAG_RTINHERIT;
675 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
676 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100677 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
678 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
679 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000680 if (vap->va_xflags & XFS_XFLAG_REALTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100682 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
683 di_flags |= XFS_DIFLAG_EXTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 ip->i_d.di_flags = di_flags;
686 }
687 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
688 timeflags |= XFS_ICHGTIME_CHG;
689 }
690
691 /*
692 * Change file inode change time only if XFS_AT_CTIME set
693 * AND we have been called by a DMI function.
694 */
695
696 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
697 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
698 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
699 ip->i_update_core = 1;
700 timeflags &= ~XFS_ICHGTIME_CHG;
701 }
702
703 /*
704 * Send out timestamp changes that need to be set to the
705 * current time. Not done when called by a DMI function.
706 */
707 if (timeflags && !(flags & ATTR_DMI))
708 xfs_ichgtime(ip, timeflags);
709
710 XFS_STATS_INC(xs_ig_attrchg);
711
712 /*
713 * If this is a synchronous mount, make sure that the
714 * transaction goes to disk before returning to the user.
715 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000716 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * One for the truncate and one for the timestamps since we
718 * don't want to change the timestamps unless we're sure the
719 * truncate worked. Truncates are less than 1% of the laddis
720 * mix so this probably isn't worth the trouble to optimize.
721 */
722 code = 0;
723 if (tp) {
724 if (mp->m_flags & XFS_MOUNT_WSYNC)
725 xfs_trans_set_sync(tp);
726
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000727 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 xfs_iunlock(ip, lock_flags);
731
732 /*
733 * Release any dquot(s) the inode had kept before chown.
734 */
735 XFS_QM_DQRELE(mp, olddquot1);
736 XFS_QM_DQRELE(mp, olddquot2);
737 XFS_QM_DQRELE(mp, udqp);
738 XFS_QM_DQRELE(mp, gdqp);
739
740 if (code) {
741 return code;
742 }
743
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000744 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 !(flags & ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100746 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 NULL, DM_RIGHT_NULL, NULL, NULL,
748 0, 0, AT_DELAY_FLAG(flags));
749 }
750 return 0;
751
752 abort_return:
753 commit_flags |= XFS_TRANS_ABORT;
754 /* FALLTHROUGH */
755 error_return:
756 XFS_QM_DQRELE(mp, udqp);
757 XFS_QM_DQRELE(mp, gdqp);
758 if (tp) {
759 xfs_trans_cancel(tp, commit_flags);
760 }
761 if (lock_flags != 0) {
762 xfs_iunlock(ip, lock_flags);
763 }
764 return code;
765}
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000768 * The maximum pathlen is 1024 bytes. Since the minimum file system
769 * blocksize is 512 bytes, we can get a max of 2 extents back from
770 * bmapi.
771 */
772#define SYMLINK_MAPS 2
773
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000774STATIC int
775xfs_readlink_bmap(
776 xfs_inode_t *ip,
777 char *link)
778{
779 xfs_mount_t *mp = ip->i_mount;
780 int pathlen = ip->i_d.di_size;
781 int nmaps = SYMLINK_MAPS;
782 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
783 xfs_daddr_t d;
784 int byte_cnt;
785 int n;
786 xfs_buf_t *bp;
787 int error = 0;
788
789 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
790 mval, &nmaps, NULL, NULL);
791 if (error)
792 goto out;
793
794 for (n = 0; n < nmaps; n++) {
795 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
796 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
797
798 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
799 error = XFS_BUF_GETERROR(bp);
800 if (error) {
801 xfs_ioerror_alert("xfs_readlink",
802 ip->i_mount, bp, XFS_BUF_ADDR(bp));
803 xfs_buf_relse(bp);
804 goto out;
805 }
806 if (pathlen < byte_cnt)
807 byte_cnt = pathlen;
808 pathlen -= byte_cnt;
809
810 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
811 xfs_buf_relse(bp);
812 }
813
814 link[ip->i_d.di_size] = '\0';
815 error = 0;
816
817 out:
818 return error;
819}
820
Christoph Hellwig993386c2007-08-28 16:12:30 +1000821int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000823 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000824 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000826 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100830 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 if (XFS_FORCED_SHUTDOWN(mp))
833 return XFS_ERROR(EIO);
834
835 xfs_ilock(ip, XFS_ILOCK_SHARED);
836
837 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000838 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000840 pathlen = ip->i_d.di_size;
841 if (!pathlen)
842 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000845 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
846 link[pathlen] = '\0';
847 } else {
848 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000851 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return error;
854}
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * xfs_fsync
858 *
859 * This is called to sync the inode and its data out to disk.
860 * We need to hold the I/O lock while flushing the data, and
861 * the inode lock while flushing the inode. The inode lock CANNOT
862 * be held while flushing the data, so acquire after we're done
863 * with that.
864 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000865int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866xfs_fsync(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000867 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 int flag,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 xfs_off_t start,
870 xfs_off_t stop)
871{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 xfs_trans_t *tp;
873 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100874 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100876 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 ASSERT(start >= 0 && stop >= -1);
879
880 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
881 return XFS_ERROR(EIO);
882
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000883 if (flag & FSYNC_DATA)
884 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /*
887 * We always need to make sure that the required inode state
888 * is safe on disk. The vnode might be clean but because
889 * of committed transactions that haven't hit the disk yet.
890 * Likewise, there could be unflushed non-transactional
891 * changes to the inode core that have to go to disk.
892 *
893 * The following code depends on one assumption: that
894 * any transaction that changes an inode logs the core
895 * because it has to change some field in the inode core
896 * (typically nextents or nblocks). That assumption
897 * implies that any transactions against an inode will
898 * catch any non-transactional updates. If inode-altering
899 * transactions exist that violate this assumption, the
900 * code breaks. Right now, it figures that if the involved
901 * update_* field is clear and the inode is unpinned, the
902 * inode is clean. Either it's been flushed or it's been
903 * committed and the commit has hit the disk unpinning the inode.
904 * (Note that xfs_inode_item_format() called at commit clears
905 * the update_* fields.)
906 */
907 xfs_ilock(ip, XFS_ILOCK_SHARED);
908
909 /* If we are flushing data then we care about update_size
910 * being set, otherwise we care about update_core
911 */
912 if ((flag & FSYNC_DATA) ?
913 (ip->i_update_size == 0) :
914 (ip->i_update_core == 0)) {
915 /*
916 * Timestamps/size haven't changed since last inode
917 * flush or inode transaction commit. That means
918 * either nothing got written or a transaction
919 * committed which caught the updates. If the
920 * latter happened and the transaction hasn't
921 * hit the disk yet, the inode will be still
922 * be pinned. If it is, force the log.
923 */
924
925 xfs_iunlock(ip, XFS_ILOCK_SHARED);
926
927 if (xfs_ipincount(ip)) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100928 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 XFS_LOG_FORCE |
930 ((flag & FSYNC_WAIT)
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100931 ? XFS_LOG_SYNC : 0),
932 &log_flushed);
933 } else {
934 /*
935 * If the inode is not pinned and nothing
936 * has changed we don't need to flush the
937 * cache.
938 */
939 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941 error = 0;
942 } else {
943 /*
944 * Kick off a transaction to log the inode
945 * core to get the updates. Make it
946 * sync if FSYNC_WAIT is passed in (which
947 * is done by everybody but specfs). The
948 * sync transaction will also force the log.
949 */
950 xfs_iunlock(ip, XFS_ILOCK_SHARED);
951 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
952 if ((error = xfs_trans_reserve(tp, 0,
953 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
954 0, 0, 0))) {
955 xfs_trans_cancel(tp, 0);
956 return error;
957 }
958 xfs_ilock(ip, XFS_ILOCK_EXCL);
959
960 /*
961 * Note - it's possible that we might have pushed
962 * ourselves out of the way during trans_reserve
963 * which would flush the inode. But there's no
964 * guarantee that the inode buffer has actually
965 * gone out yet (it's delwri). Plus the buffer
966 * could be pinned anyway if it's part of an
967 * inode in another recent transaction. So we
968 * play it safe and fire off the transaction anyway.
969 */
970 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
971 xfs_trans_ihold(tp, ip);
972 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
973 if (flag & FSYNC_WAIT)
974 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000975 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 xfs_iunlock(ip, XFS_ILOCK_EXCL);
978 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100979
980 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
981 /*
982 * If the log write didn't issue an ordered tag we need
983 * to flush the disk cache for the data device now.
984 */
985 if (!log_flushed)
986 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
987
988 /*
989 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000990 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100991 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100992 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100993 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
994 }
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return error;
997}
998
999/*
David Chinner92dfe8d2007-05-24 15:22:19 +10001000 * This is called by xfs_inactive to free any blocks beyond eof
1001 * when the link count isn't zero and by xfs_dm_punch_hole() when
1002 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001004int
1005xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +10001007 xfs_inode_t *ip,
1008 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 xfs_trans_t *tp;
1011 int error;
1012 xfs_fileoff_t end_fsb;
1013 xfs_fileoff_t last_fsb;
1014 xfs_filblks_t map_len;
1015 int nimaps;
1016 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +10001017 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /*
1020 * Figure out if there are any blocks beyond the end
1021 * of the file. If not, then there is nothing to do.
1022 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001023 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1025 map_len = last_fsb - end_fsb;
1026 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 nimaps = 1;
1030 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001031 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001032 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1034
1035 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001036 (imap.br_startblock != HOLESTARTBLOCK ||
1037 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * Attach the dquots to the inode up front.
1040 */
1041 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001042 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
1045 * There are blocks after the end of file.
1046 * Free them up now by truncating the file to
1047 * its current size.
1048 */
1049 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1050
1051 /*
1052 * Do the xfs_itruncate_start() call before
1053 * reserving any log space because
1054 * itruncate_start will call into the buffer
1055 * cache and we can't
1056 * do that within a transaction.
1057 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001058 if (use_iolock)
1059 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001060 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001061 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001062 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001063 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001064 if (use_iolock)
1065 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001066 return error;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 error = xfs_trans_reserve(tp, 0,
1070 XFS_ITRUNCATE_LOG_RES(mp),
1071 0, XFS_TRANS_PERM_LOG_RES,
1072 XFS_ITRUNCATE_LOG_COUNT);
1073 if (error) {
1074 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1075 xfs_trans_cancel(tp, 0);
1076 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001077 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
1080 xfs_ilock(ip, XFS_ILOCK_EXCL);
1081 xfs_trans_ijoin(tp, ip,
1082 XFS_IOLOCK_EXCL |
1083 XFS_ILOCK_EXCL);
1084 xfs_trans_ihold(tp, ip);
1085
1086 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001087 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 XFS_DATA_FORK,
1089 0);
1090 /*
1091 * If we get an error at this point we
1092 * simply don't bother truncating the file.
1093 */
1094 if (error) {
1095 xfs_trans_cancel(tp,
1096 (XFS_TRANS_RELEASE_LOG_RES |
1097 XFS_TRANS_ABORT));
1098 } else {
1099 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001100 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001102 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1103 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001105 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
1108/*
1109 * Free a symlink that has blocks associated with it.
1110 */
1111STATIC int
1112xfs_inactive_symlink_rmt(
1113 xfs_inode_t *ip,
1114 xfs_trans_t **tpp)
1115{
1116 xfs_buf_t *bp;
1117 int committed;
1118 int done;
1119 int error;
1120 xfs_fsblock_t first_block;
1121 xfs_bmap_free_t free_list;
1122 int i;
1123 xfs_mount_t *mp;
1124 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1125 int nmaps;
1126 xfs_trans_t *ntp;
1127 int size;
1128 xfs_trans_t *tp;
1129
1130 tp = *tpp;
1131 mp = ip->i_mount;
1132 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1133 /*
1134 * We're freeing a symlink that has some
1135 * blocks allocated to it. Free the
1136 * blocks here. We know that we've got
1137 * either 1 or 2 extents and that we can
1138 * free them all in one bunmapi call.
1139 */
1140 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1141 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1142 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1143 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1144 xfs_trans_cancel(tp, 0);
1145 *tpp = NULL;
1146 return error;
1147 }
1148 /*
1149 * Lock the inode, fix the size, and join it to the transaction.
1150 * Hold it so in the normal path, we still have it locked for
1151 * the second transaction. In the error paths we need it
1152 * held so the cancel won't rele it, see below.
1153 */
1154 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1155 size = (int)ip->i_d.di_size;
1156 ip->i_d.di_size = 0;
1157 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1158 xfs_trans_ihold(tp, ip);
1159 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1160 /*
1161 * Find the block(s) so we can inval and unmap them.
1162 */
1163 done = 0;
1164 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001165 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1167 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001168 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto error0;
1170 /*
1171 * Invalidate the block(s).
1172 */
1173 for (i = 0; i < nmaps; i++) {
1174 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1175 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1176 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1177 xfs_trans_binval(tp, bp);
1178 }
1179 /*
1180 * Unmap the dead block(s) to the free_list.
1181 */
1182 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001183 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 goto error1;
1185 ASSERT(done);
1186 /*
1187 * Commit the first transaction. This logs the EFI and the inode.
1188 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001189 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto error1;
1191 /*
1192 * The transaction must have been committed, since there were
1193 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1194 * The new tp has the extent freeing and EFDs.
1195 */
1196 ASSERT(committed);
1197 /*
1198 * The first xact was committed, so add the inode to the new one.
1199 * Mark it dirty so it will be logged and moved forward in the log as
1200 * part of every commit.
1201 */
1202 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1203 xfs_trans_ihold(tp, ip);
1204 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1205 /*
1206 * Get a new, empty transaction to return to our caller.
1207 */
1208 ntp = xfs_trans_dup(tp);
1209 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001210 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * If we get an error on the commit here or on the reserve below,
1212 * we need to unlock the inode since the new transaction doesn't
1213 * have the inode attached.
1214 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001215 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 tp = ntp;
1217 if (error) {
1218 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1219 goto error0;
1220 }
1221 /*
1222 * Remove the memory for extent descriptions (just bookkeeping).
1223 */
1224 if (ip->i_df.if_bytes)
1225 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1226 ASSERT(ip->i_df.if_bytes == 0);
1227 /*
1228 * Put an itruncate log reservation in the new transaction
1229 * for our caller.
1230 */
1231 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1232 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1233 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1234 goto error0;
1235 }
1236 /*
1237 * Return with the inode locked but not joined to the transaction.
1238 */
1239 *tpp = tp;
1240 return 0;
1241
1242 error1:
1243 xfs_bmap_cancel(&free_list);
1244 error0:
1245 /*
1246 * Have to come here with the inode locked and either
1247 * (held and in the transaction) or (not in the transaction).
1248 * If the inode isn't held then cancel would iput it, but
1249 * that's wrong since this is inactive and the vnode ref
1250 * count is 0 already.
1251 * Cancel won't do anything to the inode if held, but it still
1252 * needs to be locked until the cancel is done, if it was
1253 * joined to the transaction.
1254 */
1255 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1256 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1257 *tpp = NULL;
1258 return error;
1259
1260}
1261
1262STATIC int
1263xfs_inactive_symlink_local(
1264 xfs_inode_t *ip,
1265 xfs_trans_t **tpp)
1266{
1267 int error;
1268
1269 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1270 /*
1271 * We're freeing a symlink which fit into
1272 * the inode. Just free the memory used
1273 * to hold the old symlink.
1274 */
1275 error = xfs_trans_reserve(*tpp, 0,
1276 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1277 0, XFS_TRANS_PERM_LOG_RES,
1278 XFS_ITRUNCATE_LOG_COUNT);
1279
1280 if (error) {
1281 xfs_trans_cancel(*tpp, 0);
1282 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001283 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1286
1287 /*
1288 * Zero length symlinks _can_ exist.
1289 */
1290 if (ip->i_df.if_bytes > 0) {
1291 xfs_idata_realloc(ip,
1292 -(ip->i_df.if_bytes),
1293 XFS_DATA_FORK);
1294 ASSERT(ip->i_df.if_bytes == 0);
1295 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299STATIC int
1300xfs_inactive_attrs(
1301 xfs_inode_t *ip,
1302 xfs_trans_t **tpp)
1303{
1304 xfs_trans_t *tp;
1305 int error;
1306 xfs_mount_t *mp;
1307
1308 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1309 tp = *tpp;
1310 mp = ip->i_mount;
1311 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001312 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001314 if (error)
1315 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001318 if (error)
1319 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1322 error = xfs_trans_reserve(tp, 0,
1323 XFS_IFREE_LOG_RES(mp),
1324 0, XFS_TRANS_PERM_LOG_RES,
1325 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001326 if (error)
1327 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 xfs_ilock(ip, XFS_ILOCK_EXCL);
1330 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1331 xfs_trans_ihold(tp, ip);
1332 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1333
1334 ASSERT(ip->i_d.di_anextents == 0);
1335
1336 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001337 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001338
1339error_cancel:
1340 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1341 xfs_trans_cancel(tp, 0);
1342error_unlock:
1343 *tpp = NULL;
1344 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1345 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346}
1347
Christoph Hellwig993386c2007-08-28 16:12:30 +10001348int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001350 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001352 bhv_vnode_t *vp = XFS_ITOV(ip);
1353 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 int error;
1355
Christoph Hellwig42173f62008-04-22 17:33:25 +10001356 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001360 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362
David Chinner2a82b8b2007-07-11 11:09:12 +10001363 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001364 int truncated;
1365
David Chinner2a82b8b2007-07-11 11:09:12 +10001366 /*
1367 * If we are using filestreams, and we have an unlinked
1368 * file that we are processing the last close on, then nothing
1369 * will be able to reopen and write to this file. Purge this
1370 * inode from the filestreams cache so that it doesn't delay
1371 * teardown of the inode.
1372 */
1373 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1374 xfs_filestream_deassociate(ip);
1375
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001376 /*
1377 * If we previously truncated this file and removed old data
1378 * in the process, we want to initiate "early" writeout on
1379 * the last close. This is an attempt to combat the notorious
1380 * NULL files problem which is particularly noticable from a
1381 * truncate down, buffered (re-)write (delalloc), followed by
1382 * a crash. What we are effectively doing here is
1383 * significantly reducing the time window where we'd otherwise
1384 * be exposed to that problem.
1385 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001386 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001387 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001388 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001389 }
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 if (ip->i_d.di_nlink != 0) {
1392 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001393 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001394 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001396 (!(ip->i_d.di_flags &
1397 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001398 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1399 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001400 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402 }
1403
1404 return 0;
1405}
1406
1407/*
1408 * xfs_inactive
1409 *
1410 * This is called when the vnode reference count for the vnode
1411 * goes to zero. If the file has been unlinked, then it must
1412 * now be truncated. Also, we clear all of the read-ahead state
1413 * kept for the inode here since the file is now closed.
1414 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001415int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001417 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001419 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001420 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 xfs_fsblock_t first_block;
1422 int committed;
1423 xfs_trans_t *tp;
1424 xfs_mount_t *mp;
1425 int error;
1426 int truncate;
1427
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001428 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /*
1431 * If the inode is already free, then there can be nothing
1432 * to clean up here.
1433 */
1434 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1435 ASSERT(ip->i_df.if_real_bytes == 0);
1436 ASSERT(ip->i_df.if_broot_bytes == 0);
1437 return VN_INACTIVE_CACHE;
1438 }
1439
1440 /*
1441 * Only do a truncate if it's a regular file with
1442 * some actual space in it. It's OK to look at the
1443 * inode's fields without the lock because we're the
1444 * only one with a reference to the inode.
1445 */
1446 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001447 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1448 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1450
1451 mp = ip->i_mount;
1452
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001453 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1454 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 error = 0;
1457
1458 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001459 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 goto out;
1461
1462 if (ip->i_d.di_nlink != 0) {
1463 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001464 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001465 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001466 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1467 (!(ip->i_d.di_flags &
1468 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1469 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001470 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1471 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001472 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 }
1474 goto out;
1475 }
1476
1477 ASSERT(ip->i_d.di_nlink == 0);
1478
1479 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001480 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1483 if (truncate) {
1484 /*
1485 * Do the xfs_itruncate_start() call before
1486 * reserving any log space because itruncate_start
1487 * will call into the buffer cache and we can't
1488 * do that within a transaction.
1489 */
1490 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1491
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001492 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1493 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001494 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001495 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1496 return VN_INACTIVE_CACHE;
1497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 error = xfs_trans_reserve(tp, 0,
1500 XFS_ITRUNCATE_LOG_RES(mp),
1501 0, XFS_TRANS_PERM_LOG_RES,
1502 XFS_ITRUNCATE_LOG_COUNT);
1503 if (error) {
1504 /* Don't call itruncate_cleanup */
1505 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1506 xfs_trans_cancel(tp, 0);
1507 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001508 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
1510
1511 xfs_ilock(ip, XFS_ILOCK_EXCL);
1512 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1513 xfs_trans_ihold(tp, ip);
1514
1515 /*
1516 * normally, we have to run xfs_itruncate_finish sync.
1517 * But if filesystem is wsync and we're in the inactive
1518 * path, then we know that nlink == 0, and that the
1519 * xaction that made nlink == 0 is permanently committed
1520 * since xfs_remove runs as a synchronous transaction.
1521 */
1522 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1523 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1524
1525 if (error) {
1526 xfs_trans_cancel(tp,
1527 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1528 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001529 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1532
1533 /*
1534 * If we get an error while cleaning up a
1535 * symlink we bail out.
1536 */
1537 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1538 xfs_inactive_symlink_rmt(ip, &tp) :
1539 xfs_inactive_symlink_local(ip, &tp);
1540
1541 if (error) {
1542 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001543 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545
1546 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1547 xfs_trans_ihold(tp, ip);
1548 } else {
1549 error = xfs_trans_reserve(tp, 0,
1550 XFS_IFREE_LOG_RES(mp),
1551 0, XFS_TRANS_PERM_LOG_RES,
1552 XFS_INACTIVE_LOG_COUNT);
1553 if (error) {
1554 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1555 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001556 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
1559 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1560 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1561 xfs_trans_ihold(tp, ip);
1562 }
1563
1564 /*
1565 * If there are attributes associated with the file
1566 * then blow them away now. The code calls a routine
1567 * that recursively deconstructs the attribute fork.
1568 * We need to just commit the current transaction
1569 * because we can't use it for xfs_attr_inactive().
1570 */
1571 if (ip->i_d.di_anextents > 0) {
1572 error = xfs_inactive_attrs(ip, &tp);
1573 /*
1574 * If we got an error, the transaction is already
1575 * cancelled, and the inode is unlocked. Just get out.
1576 */
1577 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001578 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 } else if (ip->i_afp) {
1580 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1581 }
1582
1583 /*
1584 * Free the inode.
1585 */
1586 XFS_BMAP_INIT(&free_list, &first_block);
1587 error = xfs_ifree(tp, ip, &free_list);
1588 if (error) {
1589 /*
1590 * If we fail to free the inode, shut down. The cancel
1591 * might do that, we need to make sure. Otherwise the
1592 * inode might be lost for a long time or forever.
1593 */
1594 if (!XFS_FORCED_SHUTDOWN(mp)) {
1595 cmn_err(CE_NOTE,
1596 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1597 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001598 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 }
1600 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1601 } else {
1602 /*
1603 * Credit the quota account(s). The inode is gone.
1604 */
1605 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1606
1607 /*
David Chinner78e9da72008-04-10 12:24:17 +10001608 * Just ignore errors at this point. There is nothing we can
1609 * do except to try to keep going. Make sure it's not a silent
1610 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
David Chinner78e9da72008-04-10 12:24:17 +10001612 error = xfs_bmap_finish(&tp, &free_list, &committed);
1613 if (error)
1614 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1615 "xfs_bmap_finish() returned error %d", error);
1616 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1617 if (error)
1618 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1619 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 /*
1622 * Release the dquots held by inode, if any.
1623 */
1624 XFS_QM_DQDETACH(mp, ip);
1625
1626 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1627
1628 out:
1629 return VN_INACTIVE_CACHE;
1630}
1631
1632
Christoph Hellwig993386c2007-08-28 16:12:30 +10001633int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001635 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001636 struct xfs_name *name,
Christoph Hellwigef1f5e72008-03-06 13:46:25 +11001637 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001639 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 xfs_ino_t e_inum;
1641 int error;
1642 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001644 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1647 return XFS_ERROR(EIO);
1648
1649 lock_mode = xfs_ilock_map_shared(dp);
Barry Naujok556b8b12008-04-10 12:22:07 +10001650 error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!error) {
Christoph Hellwigef1f5e72008-03-06 13:46:25 +11001652 *ipp = ip;
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001653 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655 xfs_iunlock_map_shared(dp, lock_mode);
1656 return error;
1657}
1658
Christoph Hellwig993386c2007-08-28 16:12:30 +10001659int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001661 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001662 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001663 mode_t mode,
1664 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001665 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 cred_t *credp)
1667{
Barry Naujok556b8b12008-04-10 12:22:07 +10001668 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001669 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001671 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 xfs_bmap_free_t free_list;
1673 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001674 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 int dm_event_sent = 0;
1676 uint cancel_flags;
1677 int committed;
1678 xfs_prid_t prid;
1679 struct xfs_dquot *udqp, *gdqp;
1680 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001682 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001683 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001685 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001687 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001688 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001689 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 if (error)
1692 return error;
1693 dm_event_sent = 1;
1694 }
1695
1696 if (XFS_FORCED_SHUTDOWN(mp))
1697 return XFS_ERROR(EIO);
1698
1699 /* Return through std_return after this point. */
1700
1701 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001702 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1703 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 else
1705 prid = (xfs_prid_t)dfltprid;
1706
1707 /*
1708 * Make sure that we have allocated dquot(s) on disk.
1709 */
1710 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001711 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1713 if (error)
1714 goto std_return;
1715
1716 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1719 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001720 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * Initially assume that the file does not exist and
1723 * reserve the resources for that case. If that is not
1724 * the case we'll drop the one we have and get a more
1725 * appropriate transaction later.
1726 */
1727 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1728 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1729 if (error == ENOSPC) {
1730 resblks = 0;
1731 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1732 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1733 }
1734 if (error) {
1735 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 goto error_return;
1737 }
1738
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001739 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001740 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 XFS_BMAP_INIT(&free_list, &first_block);
1743
1744 ASSERT(ip == NULL);
1745
1746 /*
1747 * Reserve disk quota and the inode.
1748 */
1749 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1750 if (error)
1751 goto error_return;
1752
Barry Naujok556b8b12008-04-10 12:22:07 +10001753 error = xfs_dir_canenter(tp, dp, name, resblks);
1754 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001756 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 rdev, credp, prid, resblks > 0,
1758 &ip, &committed);
1759 if (error) {
1760 if (error == ENOSPC)
1761 goto error_return;
1762 goto abort_return;
1763 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001764 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 /*
1767 * At this point, we've gotten a newly allocated inode.
1768 * It is locked (and joined to the transaction).
1769 */
1770
1771 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1772
1773 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001774 * Now we join the directory inode to the transaction. We do not do it
1775 * earlier because xfs_dir_ialloc might commit the previous transaction
1776 * (and release all the locks). An error from here on will result in
1777 * the transaction cancel unlocking dp so don't do it explicitly in the
1778 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001780 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001782 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Barry Naujok556b8b12008-04-10 12:22:07 +10001784 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001785 &first_block, &free_list, resblks ?
1786 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (error) {
1788 ASSERT(error != ENOSPC);
1789 goto abort_return;
1790 }
1791 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1792 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1793
1794 /*
1795 * If this is a synchronous mount, make sure that the
1796 * create transaction goes to disk before returning to
1797 * the user.
1798 */
1799 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1800 xfs_trans_set_sync(tp);
1801 }
1802
1803 dp->i_gen++;
1804
1805 /*
1806 * Attach the dquot(s) to the inodes and modify them incore.
1807 * These ids of the inode couldn't have changed since the new
1808 * inode has been locked ever since it was created.
1809 */
1810 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1811
1812 /*
1813 * xfs_trans_commit normally decrements the vnode ref count
1814 * when it unlocks the inode. Since we want to return the
1815 * vnode to the caller, we bump the vnode ref count now.
1816 */
1817 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001819 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (error) {
1821 xfs_bmap_cancel(&free_list);
1822 goto abort_rele;
1823 }
1824
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001825 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (error) {
1827 IRELE(ip);
1828 tp = NULL;
1829 goto error_return;
1830 }
1831
1832 XFS_QM_DQRELE(mp, udqp);
1833 XFS_QM_DQRELE(mp, gdqp);
1834
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001835 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 /* Fallthrough to std_return with error = 0 */
1838
1839std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001840 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001841 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001843 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001844 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001845 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001846 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
1848 return error;
1849
1850 abort_return:
1851 cancel_flags |= XFS_TRANS_ABORT;
1852 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Jesper Juhl014c2542006-01-15 02:37:08 +01001854 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (tp != NULL)
1856 xfs_trans_cancel(tp, cancel_flags);
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 XFS_QM_DQRELE(mp, udqp);
1859 XFS_QM_DQRELE(mp, gdqp);
1860
Christoph Hellwig993386c2007-08-28 16:12:30 +10001861 if (unlock_dp_on_error)
1862 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto std_return;
1865
1866 abort_rele:
1867 /*
1868 * Wait until after the current transaction is aborted to
1869 * release the inode. This prevents recursive transactions
1870 * and deadlocks from xfs_inactive.
1871 */
1872 cancel_flags |= XFS_TRANS_ABORT;
1873 xfs_trans_cancel(tp, cancel_flags);
1874 IRELE(ip);
1875
1876 XFS_QM_DQRELE(mp, udqp);
1877 XFS_QM_DQRELE(mp, gdqp);
1878
1879 goto std_return;
1880}
1881
1882#ifdef DEBUG
1883/*
1884 * Some counters to see if (and how often) we are hitting some deadlock
1885 * prevention code paths.
1886 */
1887
1888int xfs_rm_locks;
1889int xfs_rm_lock_delays;
1890int xfs_rm_attempts;
1891#endif
1892
1893/*
1894 * The following routine will lock the inodes associated with the
1895 * directory and the named entry in the directory. The locks are
1896 * acquired in increasing inode number.
1897 *
1898 * If the entry is "..", then only the directory is locked. The
1899 * vnode ref count will still include that from the .. entry in
1900 * this case.
1901 *
1902 * There is a deadlock we need to worry about. If the locked directory is
1903 * in the AIL, it might be blocking up the log. The next inode we lock
1904 * could be already locked by another thread waiting for log space (e.g
1905 * a permanent log reservation with a long running transaction (see
1906 * xfs_itruncate_finish)). To solve this, we must check if the directory
1907 * is in the ail and use lock_nowait. If we can't lock, we need to
1908 * drop the inode lock on the directory and try again. xfs_iunlock will
1909 * potentially push the tail if we were holding up the log.
1910 */
1911STATIC int
1912xfs_lock_dir_and_entry(
1913 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 xfs_inode_t *ip) /* inode of entry 'name' */
1915{
1916 int attempts;
1917 xfs_ino_t e_inum;
1918 xfs_inode_t *ips[2];
1919 xfs_log_item_t *lp;
1920
1921#ifdef DEBUG
1922 xfs_rm_locks++;
1923#endif
1924 attempts = 0;
1925
1926again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001927 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 e_inum = ip->i_ino;
1930
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001931 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /*
1934 * We want to lock in increasing inum. Since we've already
1935 * acquired the lock on the directory, we may need to release
1936 * if if the inum of the entry turns out to be less.
1937 */
1938 if (e_inum > dp->i_ino) {
1939 /*
1940 * We are already in the right order, so just
1941 * lock on the inode of the entry.
1942 * We need to use nowait if dp is in the AIL.
1943 */
1944
1945 lp = (xfs_log_item_t *)dp->i_itemp;
1946 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1947 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1948 attempts++;
1949#ifdef DEBUG
1950 xfs_rm_attempts++;
1951#endif
1952
1953 /*
1954 * Unlock dp and try again.
1955 * xfs_iunlock will try to push the tail
1956 * if the inode is in the AIL.
1957 */
1958
1959 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1960
1961 if ((attempts % 5) == 0) {
1962 delay(1); /* Don't just spin the CPU */
1963#ifdef DEBUG
1964 xfs_rm_lock_delays++;
1965#endif
1966 }
1967 goto again;
1968 }
1969 } else {
1970 xfs_ilock(ip, XFS_ILOCK_EXCL);
1971 }
1972 } else if (e_inum < dp->i_ino) {
1973 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1974
1975 ips[0] = ip;
1976 ips[1] = dp;
1977 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
1978 }
1979 /* else e_inum == dp->i_ino */
1980 /* This can happen if we're asked to lock /x/..
1981 * the entry is "..", which is also the parent directory.
1982 */
1983
1984 return 0;
1985}
1986
1987#ifdef DEBUG
1988int xfs_locked_n;
1989int xfs_small_retries;
1990int xfs_middle_retries;
1991int xfs_lots_retries;
1992int xfs_lock_delays;
1993#endif
1994
1995/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001996 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1997 * a different value
1998 */
1999static inline int
2000xfs_lock_inumorder(int lock_mode, int subclass)
2001{
2002 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002003 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002004 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10002005 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002006
2007 return lock_mode;
2008}
2009
2010/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * The following routine will lock n inodes in exclusive mode.
2012 * We assume the caller calls us with the inodes in i_ino order.
2013 *
2014 * We need to detect deadlock where an inode that we lock
2015 * is in the AIL and we start waiting for another inode that is locked
2016 * by a thread in a long running transaction (such as truncate). This can
2017 * result in deadlock since the long running trans might need to wait
2018 * for the inode we just locked in order to push the tail and free space
2019 * in the log.
2020 */
2021void
2022xfs_lock_inodes(
2023 xfs_inode_t **ips,
2024 int inodes,
2025 int first_locked,
2026 uint lock_mode)
2027{
2028 int attempts = 0, i, j, try_lock;
2029 xfs_log_item_t *lp;
2030
2031 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2032
2033 if (first_locked) {
2034 try_lock = 1;
2035 i = 1;
2036 } else {
2037 try_lock = 0;
2038 i = 0;
2039 }
2040
2041again:
2042 for (; i < inodes; i++) {
2043 ASSERT(ips[i]);
2044
2045 if (i && (ips[i] == ips[i-1])) /* Already locked */
2046 continue;
2047
2048 /*
2049 * If try_lock is not set yet, make sure all locked inodes
2050 * are not in the AIL.
2051 * If any are, set try_lock to be used later.
2052 */
2053
2054 if (!try_lock) {
2055 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2056 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2057 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2058 try_lock++;
2059 }
2060 }
2061 }
2062
2063 /*
2064 * If any of the previous locks we have locked is in the AIL,
2065 * we must TRY to get the second and subsequent locks. If
2066 * we can't get any, we must release all we have
2067 * and try again.
2068 */
2069
2070 if (try_lock) {
2071 /* try_lock must be 0 if i is 0. */
2072 /*
2073 * try_lock means we have an inode locked
2074 * that is in the AIL.
2075 */
2076 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002077 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 attempts++;
2079
2080 /*
2081 * Unlock all previous guys and try again.
2082 * xfs_iunlock will try to push the tail
2083 * if the inode is in the AIL.
2084 */
2085
2086 for(j = i - 1; j >= 0; j--) {
2087
2088 /*
2089 * Check to see if we've already
2090 * unlocked this one.
2091 * Not the first one going back,
2092 * and the inode ptr is the same.
2093 */
2094 if ((j != (i - 1)) && ips[j] ==
2095 ips[j+1])
2096 continue;
2097
2098 xfs_iunlock(ips[j], lock_mode);
2099 }
2100
2101 if ((attempts % 5) == 0) {
2102 delay(1); /* Don't just spin the CPU */
2103#ifdef DEBUG
2104 xfs_lock_delays++;
2105#endif
2106 }
2107 i = 0;
2108 try_lock = 0;
2109 goto again;
2110 }
2111 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002112 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
2114 }
2115
2116#ifdef DEBUG
2117 if (attempts) {
2118 if (attempts < 5) xfs_small_retries++;
2119 else if (attempts < 100) xfs_middle_retries++;
2120 else xfs_lots_retries++;
2121 } else {
2122 xfs_locked_n++;
2123 }
2124#endif
2125}
2126
2127#ifdef DEBUG
2128#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2129int remove_which_error_return = 0;
2130#else /* ! DEBUG */
2131#define REMOVE_DEBUG_TRACE(x)
2132#endif /* ! DEBUG */
2133
Christoph Hellwig993386c2007-08-28 16:12:30 +10002134int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002136 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002137 struct xfs_name *name,
2138 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002140 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 int error = 0;
2143 xfs_bmap_free_t free_list;
2144 xfs_fsblock_t first_block;
2145 int cancel_flags;
2146 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int link_zero;
2148 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002150 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (XFS_FORCED_SHUTDOWN(mp))
2153 return XFS_ERROR(EIO);
2154
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002155 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10002156 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
2157 NULL, DM_RIGHT_NULL, name->name, NULL,
2158 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (error)
2160 return error;
2161 }
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * We need to get a reference to ip before we get our log
2165 * reservation. The reason for this is that we cannot call
2166 * xfs_iget for an inode for which we do not have a reference
2167 * once we've acquired a log reservation. This is because the
2168 * inode we are trying to get might be in xfs_inactive going
2169 * for a log reservation. Since we'll have to wait for the
2170 * inactive code to complete before returning from xfs_iget,
2171 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002172 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 * to the inode before getting our log reservation.
2174 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002175 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002177 xfs_itrace_entry(ip);
2178 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 error = XFS_QM_DQATTACH(mp, dp, 0);
2181 if (!error && dp != ip)
2182 error = XFS_QM_DQATTACH(mp, ip, 0);
2183 if (error) {
2184 REMOVE_DEBUG_TRACE(__LINE__);
2185 IRELE(ip);
2186 goto std_return;
2187 }
2188
2189 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2190 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2191 /*
2192 * We try to get the real space reservation first,
2193 * allowing for directory btree deletion(s) implying
2194 * possible bmap insert(s). If we can't get the space
2195 * reservation then we use 0 instead, and avoid the bmap
2196 * btree insert(s) in the directory code by, if the bmap
2197 * insert tries to happen, instead trimming the LAST
2198 * block from the directory.
2199 */
2200 resblks = XFS_REMOVE_SPACE_RES(mp);
2201 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2202 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2203 if (error == ENOSPC) {
2204 resblks = 0;
2205 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2206 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2207 }
2208 if (error) {
2209 ASSERT(error != ENOSPC);
2210 REMOVE_DEBUG_TRACE(__LINE__);
2211 xfs_trans_cancel(tp, 0);
2212 IRELE(ip);
2213 return error;
2214 }
2215
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002216 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (error) {
2218 REMOVE_DEBUG_TRACE(__LINE__);
2219 xfs_trans_cancel(tp, cancel_flags);
2220 IRELE(ip);
2221 goto std_return;
2222 }
2223
2224 /*
2225 * At this point, we've gotten both the directory and the entry
2226 * inodes locked.
2227 */
2228 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2229 if (dp != ip) {
2230 /*
2231 * Increment vnode ref count only in this case since
2232 * there's an extra vnode reference in the case where
2233 * dp == ip.
2234 */
2235 IHOLD(dp);
2236 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2237 }
2238
2239 /*
2240 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2241 */
2242 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10002243 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10002244 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (error) {
2246 ASSERT(error != ENOENT);
2247 REMOVE_DEBUG_TRACE(__LINE__);
2248 goto error1;
2249 }
2250 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2251
2252 dp->i_gen++;
2253 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2254
2255 error = xfs_droplink(tp, ip);
2256 if (error) {
2257 REMOVE_DEBUG_TRACE(__LINE__);
2258 goto error1;
2259 }
2260
2261 /* Determine if this is the last link while
2262 * we are in the transaction.
2263 */
2264 link_zero = (ip)->i_d.di_nlink==0;
2265
2266 /*
2267 * Take an extra ref on the inode so that it doesn't
2268 * go to xfs_inactive() from within the commit.
2269 */
2270 IHOLD(ip);
2271
2272 /*
2273 * If this is a synchronous mount, make sure that the
2274 * remove transaction goes to disk before returning to
2275 * the user.
2276 */
2277 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2278 xfs_trans_set_sync(tp);
2279 }
2280
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002281 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (error) {
2283 REMOVE_DEBUG_TRACE(__LINE__);
2284 goto error_rele;
2285 }
2286
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002287 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (error) {
2289 IRELE(ip);
2290 goto std_return;
2291 }
2292
2293 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002294 * If we are using filestreams, kill the stream association.
2295 * If the file is still open it may get a new one but that
2296 * will get killed on last close in xfs_close() so we don't
2297 * have to worry about that.
2298 */
2299 if (link_zero && xfs_inode_is_filestream(ip))
2300 xfs_filestream_deassociate(ip);
2301
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002302 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 IRELE(ip);
2304
2305/* Fall through to std_return with error = 0 */
2306 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002307 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002309 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002311 name->name, NULL, ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 }
2313 return error;
2314
2315 error1:
2316 xfs_bmap_cancel(&free_list);
2317 cancel_flags |= XFS_TRANS_ABORT;
2318 xfs_trans_cancel(tp, cancel_flags);
2319 goto std_return;
2320
2321 error_rele:
2322 /*
2323 * In this case make sure to not release the inode until after
2324 * the current transaction is aborted. Releasing it beforehand
2325 * can cause us to go to xfs_inactive and start a recursive
2326 * transaction which can easily deadlock with the current one.
2327 */
2328 xfs_bmap_cancel(&free_list);
2329 cancel_flags |= XFS_TRANS_ABORT;
2330 xfs_trans_cancel(tp, cancel_flags);
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 IRELE(ip);
2333
2334 goto std_return;
2335}
2336
Christoph Hellwig993386c2007-08-28 16:12:30 +10002337int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002339 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002340 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002341 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002343 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 xfs_inode_t *ips[2];
2346 int error;
2347 xfs_bmap_free_t free_list;
2348 xfs_fsblock_t first_block;
2349 int cancel_flags;
2350 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002353 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002354 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002356 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (XFS_FORCED_SHUTDOWN(mp))
2359 return XFS_ERROR(EIO);
2360
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002361 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002363 tdp, DM_RIGHT_NULL,
2364 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002365 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (error)
2367 return error;
2368 }
2369
2370 /* Return through std_return after this point. */
2371
2372 error = XFS_QM_DQATTACH(mp, sip, 0);
2373 if (!error && sip != tdp)
2374 error = XFS_QM_DQATTACH(mp, tdp, 0);
2375 if (error)
2376 goto std_return;
2377
2378 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2379 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002380 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2382 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2383 if (error == ENOSPC) {
2384 resblks = 0;
2385 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2386 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2387 }
2388 if (error) {
2389 cancel_flags = 0;
2390 goto error_return;
2391 }
2392
2393 if (sip->i_ino < tdp->i_ino) {
2394 ips[0] = sip;
2395 ips[1] = tdp;
2396 } else {
2397 ips[0] = tdp;
2398 ips[1] = sip;
2399 }
2400
2401 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2402
2403 /*
2404 * Increment vnode ref counts since xfs_trans_commit &
2405 * xfs_trans_cancel will both unlock the inodes and
2406 * decrement the associated ref counts.
2407 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002408 IHOLD(sip);
2409 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2411 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2412
2413 /*
2414 * If the source has too many links, we can't make any more to it.
2415 */
2416 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2417 error = XFS_ERROR(EMLINK);
2418 goto error_return;
2419 }
2420
Nathan Scott365ca832005-06-21 15:39:12 +10002421 /*
2422 * If we are using project inheritance, we only allow hard link
2423 * creation in our tree when the project IDs are the same; else
2424 * the tree quota mechanism could be circumvented.
2425 */
2426 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2427 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002428 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002429 goto error_return;
2430 }
2431
Barry Naujok556b8b12008-04-10 12:22:07 +10002432 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2433 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 goto error_return;
2435
2436 XFS_BMAP_INIT(&free_list, &first_block);
2437
Barry Naujok556b8b12008-04-10 12:22:07 +10002438 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2439 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (error)
2441 goto abort_return;
2442 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2443 tdp->i_gen++;
2444 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2445
2446 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002447 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 /*
2451 * If this is a synchronous mount, make sure that the
2452 * link transaction goes to disk before returning to
2453 * the user.
2454 */
2455 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2456 xfs_trans_set_sync(tp);
2457 }
2458
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002459 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (error) {
2461 xfs_bmap_cancel(&free_list);
2462 goto abort_return;
2463 }
2464
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002465 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002466 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
2469 /* Fall through to std_return with error = 0. */
2470std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002471 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002473 tdp, DM_RIGHT_NULL,
2474 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002475 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
2477 return error;
2478
2479 abort_return:
2480 cancel_flags |= XFS_TRANS_ABORT;
2481 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 error_return:
2484 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 goto std_return;
2486}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002487
2488
Christoph Hellwig993386c2007-08-28 16:12:30 +10002489int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002491 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002492 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002493 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002494 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 cred_t *credp)
2496{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002497 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 int cancel_flags;
2501 int error;
2502 int committed;
2503 xfs_bmap_free_t free_list;
2504 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002505 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 boolean_t created = B_FALSE;
2507 int dm_event_sent = 0;
2508 xfs_prid_t prid;
2509 struct xfs_dquot *udqp, *gdqp;
2510 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if (XFS_FORCED_SHUTDOWN(mp))
2513 return XFS_ERROR(EIO);
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002517 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002519 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002520 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002521 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (error)
2523 return error;
2524 dm_event_sent = 1;
2525 }
2526
2527 /* Return through std_return after this point. */
2528
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002529 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 mp = dp->i_mount;
2532 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002533 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2534 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 else
2536 prid = (xfs_prid_t)dfltprid;
2537
2538 /*
2539 * Make sure that we have allocated dquot(s) on disk.
2540 */
2541 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002542 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2544 if (error)
2545 goto std_return;
2546
2547 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2548 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002549 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2551 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2552 if (error == ENOSPC) {
2553 resblks = 0;
2554 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2555 XFS_TRANS_PERM_LOG_RES,
2556 XFS_MKDIR_LOG_COUNT);
2557 }
2558 if (error) {
2559 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 goto error_return;
2561 }
2562
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002563 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002564 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 /*
2567 * Check for directory link count overflow.
2568 */
2569 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2570 error = XFS_ERROR(EMLINK);
2571 goto error_return;
2572 }
2573
2574 /*
2575 * Reserve disk quota and the inode.
2576 */
2577 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2578 if (error)
2579 goto error_return;
2580
Barry Naujok556b8b12008-04-10 12:22:07 +10002581 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 goto error_return;
2584 /*
2585 * create the directory inode.
2586 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002587 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 0, credp, prid, resblks > 0,
2589 &cdp, NULL);
2590 if (error) {
2591 if (error == ENOSPC)
2592 goto error_return;
2593 goto abort_return;
2594 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002595 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /*
2598 * Now we add the directory inode to the transaction.
2599 * We waited until now since xfs_dir_ialloc might start
2600 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002601 * earlier, the locks might have gotten released. An error
2602 * from here on will result in the transaction cancel
2603 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002605 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002607 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 XFS_BMAP_INIT(&free_list, &first_block);
2610
Barry Naujok556b8b12008-04-10 12:22:07 +10002611 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2612 &first_block, &free_list, resblks ?
2613 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (error) {
2615 ASSERT(error != ENOSPC);
2616 goto error1;
2617 }
2618 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2619
2620 /*
2621 * Bump the in memory version number of the parent directory
2622 * so that other processes accessing it will recognize that
2623 * the directory has changed.
2624 */
2625 dp->i_gen++;
2626
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002627 error = xfs_dir_init(tp, cdp, dp);
2628 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 cdp->i_gen = 1;
2632 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002633 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 created = B_TRUE;
2637
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002638 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 IHOLD(cdp);
2640
2641 /*
2642 * Attach the dquots to the new inode and modify the icount incore.
2643 */
2644 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2645
2646 /*
2647 * If this is a synchronous mount, make sure that the
2648 * mkdir transaction goes to disk before returning to
2649 * the user.
2650 */
2651 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2652 xfs_trans_set_sync(tp);
2653 }
2654
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002655 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (error) {
2657 IRELE(cdp);
2658 goto error2;
2659 }
2660
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002661 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 XFS_QM_DQRELE(mp, udqp);
2663 XFS_QM_DQRELE(mp, gdqp);
2664 if (error) {
2665 IRELE(cdp);
2666 }
2667
2668 /* Fall through to std_return with error = 0 or errno from
2669 * xfs_trans_commit. */
2670
2671std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002672 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002673 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002675 dp, DM_RIGHT_NULL,
2676 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002678 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002679 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
2681 return error;
2682
2683 error2:
2684 error1:
2685 xfs_bmap_cancel(&free_list);
2686 abort_return:
2687 cancel_flags |= XFS_TRANS_ABORT;
2688 error_return:
2689 xfs_trans_cancel(tp, cancel_flags);
2690 XFS_QM_DQRELE(mp, udqp);
2691 XFS_QM_DQRELE(mp, gdqp);
2692
Christoph Hellwig993386c2007-08-28 16:12:30 +10002693 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 goto std_return;
2697}
2698
Christoph Hellwig993386c2007-08-28 16:12:30 +10002699int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002701 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002702 struct xfs_name *name,
2703 xfs_inode_t *cdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002705 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002706 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 int error;
2709 xfs_bmap_free_t free_list;
2710 xfs_fsblock_t first_block;
2711 int cancel_flags;
2712 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 uint resblks;
2715
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002716 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Christoph Hellwig993386c2007-08-28 16:12:30 +10002718 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002721 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002723 dp, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002724 NULL, DM_RIGHT_NULL, name->name,
2725 NULL, cdp->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (error)
2727 return XFS_ERROR(error);
2728 }
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /*
2731 * We need to get a reference to cdp before we get our log
2732 * reservation. The reason for this is that we cannot call
2733 * xfs_iget for an inode for which we do not have a reference
2734 * once we've acquired a log reservation. This is because the
2735 * inode we are trying to get might be in xfs_inactive going
2736 * for a log reservation. Since we'll have to wait for the
2737 * inactive code to complete before returning from xfs_iget,
2738 * we need to make sure that we don't have log space reserved
Nathan Scottc41564b2006-03-29 08:55:14 +10002739 * when we call xfs_iget. Instead we get an unlocked reference
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 * to the inode before getting our log reservation.
2741 */
Christoph Hellwig43973962008-03-06 13:44:50 +11002742 IHOLD(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /*
2745 * Get the dquots for the inodes.
2746 */
2747 error = XFS_QM_DQATTACH(mp, dp, 0);
2748 if (!error && dp != cdp)
2749 error = XFS_QM_DQATTACH(mp, cdp, 0);
2750 if (error) {
2751 IRELE(cdp);
2752 REMOVE_DEBUG_TRACE(__LINE__);
2753 goto std_return;
2754 }
2755
2756 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2757 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2758 /*
2759 * We try to get the real space reservation first,
2760 * allowing for directory btree deletion(s) implying
2761 * possible bmap insert(s). If we can't get the space
2762 * reservation then we use 0 instead, and avoid the bmap
2763 * btree insert(s) in the directory code by, if the bmap
2764 * insert tries to happen, instead trimming the LAST
2765 * block from the directory.
2766 */
2767 resblks = XFS_REMOVE_SPACE_RES(mp);
2768 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2769 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2770 if (error == ENOSPC) {
2771 resblks = 0;
2772 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2773 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2774 }
2775 if (error) {
2776 ASSERT(error != ENOSPC);
2777 cancel_flags = 0;
2778 IRELE(cdp);
2779 goto error_return;
2780 }
2781 XFS_BMAP_INIT(&free_list, &first_block);
2782
2783 /*
2784 * Now lock the child directory inode and the parent directory
2785 * inode in the proper order. This will take care of validating
2786 * that the directory entry for the child directory inode has
2787 * not changed while we were obtaining a log reservation.
2788 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002789 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (error) {
2791 xfs_trans_cancel(tp, cancel_flags);
2792 IRELE(cdp);
2793 goto std_return;
2794 }
2795
2796 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2797 if (dp != cdp) {
2798 /*
2799 * Only increment the parent directory vnode count if
2800 * we didn't bump it in looking up cdp. The only time
2801 * we don't bump it is when we're looking up ".".
2802 */
2803 VN_HOLD(dir_vp);
2804 }
2805
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002806 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2808
2809 ASSERT(cdp->i_d.di_nlink >= 2);
2810 if (cdp->i_d.di_nlink != 2) {
2811 error = XFS_ERROR(ENOTEMPTY);
2812 goto error_return;
2813 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002814 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 error = XFS_ERROR(ENOTEMPTY);
2816 goto error_return;
2817 }
2818
Barry Naujok556b8b12008-04-10 12:22:07 +10002819 error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002820 &first_block, &free_list, resblks);
2821 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2825
2826 /*
2827 * Bump the in memory generation count on the parent
2828 * directory so that other can know that it has changed.
2829 */
2830 dp->i_gen++;
2831
2832 /*
2833 * Drop the link from cdp's "..".
2834 */
2835 error = xfs_droplink(tp, dp);
2836 if (error) {
2837 goto error1;
2838 }
2839
2840 /*
2841 * Drop the link from dp to cdp.
2842 */
2843 error = xfs_droplink(tp, cdp);
2844 if (error) {
2845 goto error1;
2846 }
2847
2848 /*
2849 * Drop the "." link from cdp to self.
2850 */
2851 error = xfs_droplink(tp, cdp);
2852 if (error) {
2853 goto error1;
2854 }
2855
2856 /* Determine these before committing transaction */
2857 last_cdp_link = (cdp)->i_d.di_nlink==0;
2858
2859 /*
2860 * Take an extra ref on the child vnode so that it
2861 * does not go to xfs_inactive() from within the commit.
2862 */
2863 IHOLD(cdp);
2864
2865 /*
2866 * If this is a synchronous mount, make sure that the
2867 * rmdir transaction goes to disk before returning to
2868 * the user.
2869 */
2870 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2871 xfs_trans_set_sync(tp);
2872 }
2873
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002874 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (error) {
2876 xfs_bmap_cancel(&free_list);
2877 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2878 XFS_TRANS_ABORT));
2879 IRELE(cdp);
2880 goto std_return;
2881 }
2882
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002883 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (error) {
2885 IRELE(cdp);
2886 goto std_return;
2887 }
2888
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 IRELE(cdp);
2891
2892 /* Fall through to std_return with error = 0 or the errno
2893 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11002894 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002895 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002897 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002899 name->name, NULL, cdp->i_d.di_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 error, 0);
2901 }
2902 return error;
2903
Nathan Scottbb19fba2006-03-22 14:12:12 +11002904 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 xfs_bmap_cancel(&free_list);
2906 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01002907 /* FALLTHROUGH */
2908
Nathan Scottbb19fba2006-03-22 14:12:12 +11002909 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 xfs_trans_cancel(tp, cancel_flags);
2911 goto std_return;
2912}
2913
Christoph Hellwig993386c2007-08-28 16:12:30 +10002914int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002916 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002917 struct xfs_name *link_name,
2918 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002919 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002920 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 cred_t *credp)
2922{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002923 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 xfs_inode_t *ip;
2926 int error;
2927 int pathlen;
2928 xfs_bmap_free_t free_list;
2929 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002930 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 uint cancel_flags;
2932 int committed;
2933 xfs_fileoff_t first_fsb;
2934 xfs_filblks_t fs_blocks;
2935 int nmaps;
2936 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2937 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002938 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 int byte_cnt;
2940 int n;
2941 xfs_buf_t *bp;
2942 xfs_prid_t prid;
2943 struct xfs_dquot *udqp, *gdqp;
2944 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Christoph Hellwig3937be52008-03-06 13:46:19 +11002946 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 error = 0;
2948 ip = NULL;
2949 tp = NULL;
2950
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002951 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 if (XFS_FORCED_SHUTDOWN(mp))
2954 return XFS_ERROR(EIO);
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /*
2957 * Check component lengths of the target path name.
2958 */
2959 pathlen = strlen(target_path);
2960 if (pathlen >= MAXPATHLEN) /* total string too long */
2961 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002963 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002964 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002966 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (error)
2968 return error;
2969 }
2970
2971 /* Return through std_return after this point. */
2972
2973 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002974 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2975 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 else
2977 prid = (xfs_prid_t)dfltprid;
2978
2979 /*
2980 * Make sure that we have allocated dquot(s) on disk.
2981 */
2982 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002983 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2985 if (error)
2986 goto std_return;
2987
2988 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2989 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2990 /*
2991 * The symlink will fit into the inode data fork?
2992 * There can't be any attributes so we get the whole variable part.
2993 */
2994 if (pathlen <= XFS_LITINO(mp))
2995 fs_blocks = 0;
2996 else
2997 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002998 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3000 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3001 if (error == ENOSPC && fs_blocks == 0) {
3002 resblks = 0;
3003 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3004 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3005 }
3006 if (error) {
3007 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 goto error_return;
3009 }
3010
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10003011 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003012 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 /*
3015 * Check whether the directory allows new symlinks or not.
3016 */
3017 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3018 error = XFS_ERROR(EPERM);
3019 goto error_return;
3020 }
3021
3022 /*
3023 * Reserve disk quota : blocks and inode.
3024 */
3025 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3026 if (error)
3027 goto error_return;
3028
3029 /*
3030 * Check for ability to enter directory entry, if no space reserved.
3031 */
Barry Naujok556b8b12008-04-10 12:22:07 +10003032 error = xfs_dir_canenter(tp, dp, link_name, resblks);
3033 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 goto error_return;
3035 /*
3036 * Initialize the bmap freelist prior to calling either
3037 * bmapi or the directory create code.
3038 */
3039 XFS_BMAP_INIT(&free_list, &first_block);
3040
3041 /*
3042 * Allocate an inode for the symlink.
3043 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10003044 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 1, 0, credp, prid, resblks > 0, &ip, NULL);
3046 if (error) {
3047 if (error == ENOSPC)
3048 goto error_return;
3049 goto error1;
3050 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003051 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Christoph Hellwig993386c2007-08-28 16:12:30 +10003053 /*
3054 * An error after we've joined dp to the transaction will result in the
3055 * transaction cancel unlocking dp so don't do it explicitly in the
3056 * error path.
3057 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11003058 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10003060 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 /*
3063 * Also attach the dquot(s) to it, if applicable.
3064 */
3065 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3066
3067 if (resblks)
3068 resblks -= XFS_IALLOC_SPACE_RES(mp);
3069 /*
3070 * If the symlink will fit into the inode, write it inline.
3071 */
3072 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3073 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3074 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3075 ip->i_d.di_size = pathlen;
3076
3077 /*
3078 * The inode was initially created in extent format.
3079 */
3080 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3081 ip->i_df.if_flags |= XFS_IFINLINE;
3082
3083 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3084 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3085
3086 } else {
3087 first_fsb = 0;
3088 nmaps = SYMLINK_MAPS;
3089
3090 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3091 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3092 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003093 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (error) {
3095 goto error1;
3096 }
3097
3098 if (resblks)
3099 resblks -= fs_blocks;
3100 ip->i_d.di_size = pathlen;
3101 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3102
3103 cur_chunk = target_path;
3104 for (n = 0; n < nmaps; n++) {
3105 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3106 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3107 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3108 BTOBB(byte_cnt), 0);
3109 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3110 if (pathlen < byte_cnt) {
3111 byte_cnt = pathlen;
3112 }
3113 pathlen -= byte_cnt;
3114
3115 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3116 cur_chunk += byte_cnt;
3117
3118 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3119 }
3120 }
3121
3122 /*
3123 * Create the directory entry for the symlink.
3124 */
Barry Naujok556b8b12008-04-10 12:22:07 +10003125 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
3126 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003127 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3130 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3131
3132 /*
3133 * Bump the in memory version number of the parent directory
3134 * so that other processes accessing it will recognize that
3135 * the directory has changed.
3136 */
3137 dp->i_gen++;
3138
3139 /*
3140 * If this is a synchronous mount, make sure that the
3141 * symlink transaction goes to disk before returning to
3142 * the user.
3143 */
3144 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3145 xfs_trans_set_sync(tp);
3146 }
3147
3148 /*
3149 * xfs_trans_commit normally decrements the vnode ref count
3150 * when it unlocks the inode. Since we want to return the
3151 * vnode to the caller, we bump the vnode ref count now.
3152 */
3153 IHOLD(ip);
3154
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003155 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (error) {
3157 goto error2;
3158 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003159 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 XFS_QM_DQRELE(mp, udqp);
3161 XFS_QM_DQRELE(mp, gdqp);
3162
3163 /* Fall through to std_return with error = 0 or errno from
3164 * xfs_trans_commit */
3165std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003166 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003168 dp, DM_RIGHT_NULL,
3169 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10003170 DM_RIGHT_NULL, link_name->name,
3171 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
3173
Christoph Hellwig3937be52008-03-06 13:46:19 +11003174 if (!error)
3175 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 return error;
3177
3178 error2:
3179 IRELE(ip);
3180 error1:
3181 xfs_bmap_cancel(&free_list);
3182 cancel_flags |= XFS_TRANS_ABORT;
3183 error_return:
3184 xfs_trans_cancel(tp, cancel_flags);
3185 XFS_QM_DQRELE(mp, udqp);
3186 XFS_QM_DQRELE(mp, gdqp);
3187
Christoph Hellwig993386c2007-08-28 16:12:30 +10003188 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 goto std_return;
3192}
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003196 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 int flags)
3198{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003199 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 int error = 0;
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (XFS_FORCED_SHUTDOWN(mp))
3203 return XFS_ERROR(EIO);
3204
3205 /*
3206 * Bypass inodes which have already been cleaned by
3207 * the inode flush clustering code inside xfs_iflush
3208 */
David Chinner33540402008-03-06 13:43:59 +11003209 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return 0;
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /*
3213 * We make this non-blocking if the inode is contended,
3214 * return EAGAIN to indicate to the caller that they
3215 * did not succeed. This prevents the flush path from
3216 * blocking on inodes inside another operation right
3217 * now, they get caught later by xfs_sync.
3218 */
David Chinnera3f74ff2008-03-06 13:43:42 +11003219 if (flags & FLUSH_SYNC) {
3220 xfs_ilock(ip, XFS_ILOCK_SHARED);
3221 xfs_iflock(ip);
3222 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3223 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3224 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return EAGAIN;
3226 }
David Chinnera3f74ff2008-03-06 13:43:42 +11003227 } else {
3228 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 }
3230
David Chinnera3f74ff2008-03-06 13:43:42 +11003231 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3232 : XFS_IFLUSH_ASYNC_NOBLOCK);
3233 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return error;
3236}
3237
Christoph Hellwig993386c2007-08-28 16:12:30 +10003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003240xfs_set_dmattrs(
3241 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003243 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003245 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 int error;
3248
3249 if (!capable(CAP_SYS_ADMIN))
3250 return XFS_ERROR(EPERM);
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (XFS_FORCED_SHUTDOWN(mp))
3253 return XFS_ERROR(EIO);
3254
3255 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3256 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3257 if (error) {
3258 xfs_trans_cancel(tp, 0);
3259 return error;
3260 }
3261 xfs_ilock(ip, XFS_ILOCK_EXCL);
3262 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3263
Christoph Hellwig613d7042007-10-11 17:44:08 +10003264 ip->i_d.di_dmevmask = evmask;
3265 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3268 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003269 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
3271 return error;
3272}
3273
Christoph Hellwig993386c2007-08-28 16:12:30 +10003274int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003276 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003278 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003280 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
3282 ASSERT(!VN_MAPPED(vp));
3283
3284 /* bad inode, get out here ASAP */
3285 if (VN_BAD(vp)) {
3286 xfs_ireclaim(ip);
3287 return 0;
3288 }
3289
Christoph Hellwigb677c212007-08-29 11:46:28 +10003290 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003292 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003294 /*
3295 * Make sure the atime in the XFS inode is correct before freeing the
3296 * Linux inode.
3297 */
3298 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
David Chinner4c606582006-11-11 18:05:00 +11003300 /*
3301 * If we have nothing to flush with this inode then complete the
3302 * teardown now, otherwise break the link between the xfs inode and the
3303 * linux inode and clean up the xfs inode later. This avoids flushing
3304 * the inode to disk during the delete operation itself.
3305 *
3306 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3307 * first to ensure that xfs_iunpin() will never see an xfs inode
3308 * that has a linux inode being reclaimed. Synchronisation is provided
3309 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
3311 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3312 xfs_ilock(ip, XFS_ILOCK_EXCL);
3313 xfs_iflock(ip);
3314 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3315 } else {
3316 xfs_mount_t *mp = ip->i_mount;
3317
David Chinner4c606582006-11-11 18:05:00 +11003318 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003320 spin_lock(&ip->i_flags_lock);
3321 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003322 vn_to_inode(vp)->i_private = NULL;
3323 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003324 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 XFS_MOUNT_IUNLOCK(mp);
3327 }
3328 return 0;
3329}
3330
3331int
3332xfs_finish_reclaim(
3333 xfs_inode_t *ip,
3334 int locked,
3335 int sync_mode)
3336{
David Chinnerda353b02007-08-28 14:00:13 +10003337 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003338 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 int error;
3340
3341 if (vp && VN_BAD(vp))
3342 goto reclaim;
3343
3344 /* The hash lock here protects a thread in xfs_iget_core from
3345 * racing with us on linking the inode back with a vnode.
3346 * Once we have the XFS_IRECLAIM flag set it will not touch
3347 * us.
3348 */
David Chinnerda353b02007-08-28 14:00:13 +10003349 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003350 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003351 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3352 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003353 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003354 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 if (locked) {
3356 xfs_ifunlock(ip);
3357 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3358 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003359 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
David Chinner7a18c382006-11-11 18:04:54 +11003361 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003362 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003363 write_unlock(&pag->pag_ici_lock);
3364 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
3366 /*
3367 * If the inode is still dirty, then flush it out. If the inode
3368 * is not in the AIL, then it will be OK to flush it delwri as
3369 * long as xfs_iflush() does not keep any references to the inode.
3370 * We leave that decision up to xfs_iflush() since it has the
3371 * knowledge of whether it's OK to simply do a delwri flush of
3372 * the inode or whether we need to wait until the inode is
3373 * pulled from the AIL.
3374 * We get the flush lock regardless, though, just to make sure
3375 * we don't free it while it is being flushed.
3376 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003377 if (!locked) {
3378 xfs_ilock(ip, XFS_ILOCK_EXCL);
3379 xfs_iflock(ip);
3380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003382 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (ip->i_update_core ||
3384 ((ip->i_itemp != NULL) &&
3385 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3386 error = xfs_iflush(ip, sync_mode);
3387 /*
3388 * If we hit an error, typically because of filesystem
3389 * shutdown, we don't need to let vn_reclaim to know
3390 * because we're gonna reclaim the inode anyway.
3391 */
3392 if (error) {
3393 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3394 goto reclaim;
3395 }
3396 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3397 }
3398
3399 ASSERT(ip->i_update_core == 0);
3400 ASSERT(ip->i_itemp == NULL ||
3401 ip->i_itemp->ili_format.ilf_fields == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003404 xfs_ifunlock(ip);
3405 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 reclaim:
3408 xfs_ireclaim(ip);
3409 return 0;
3410}
3411
3412int
3413xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3414{
3415 int purged;
3416 xfs_inode_t *ip, *n;
3417 int done = 0;
3418
3419 while (!done) {
3420 purged = 0;
3421 XFS_MOUNT_ILOCK(mp);
3422 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3423 if (noblock) {
3424 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3425 continue;
3426 if (xfs_ipincount(ip) ||
3427 !xfs_iflock_nowait(ip)) {
3428 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3429 continue;
3430 }
3431 }
3432 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003433 if (xfs_finish_reclaim(ip, noblock,
3434 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3435 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 purged = 1;
3437 break;
3438 }
3439
3440 done = !purged;
3441 }
3442
3443 XFS_MOUNT_IUNLOCK(mp);
3444 return 0;
3445}
3446
3447/*
3448 * xfs_alloc_file_space()
3449 * This routine allocates disk space for the given file.
3450 *
3451 * If alloc_type == 0, this request is for an ALLOCSP type
3452 * request which will change the file size. In this case, no
3453 * DMAPI event will be generated by the call. A TRUNCATE event
3454 * will be generated later by xfs_setattr.
3455 *
3456 * If alloc_type != 0, this request is for a RESVSP type
3457 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3458 * lower block boundary byte address is less than the file's
3459 * length.
3460 *
3461 * RETURNS:
3462 * 0 on success
3463 * errno on error
3464 *
3465 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003466STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467xfs_alloc_file_space(
3468 xfs_inode_t *ip,
3469 xfs_off_t offset,
3470 xfs_off_t len,
3471 int alloc_type,
3472 int attr_flags)
3473{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003474 xfs_mount_t *mp = ip->i_mount;
3475 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 xfs_filblks_t allocated_fsb;
3477 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003478 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003480 xfs_fsblock_t firstfsb;
3481 int nimaps;
3482 int bmapi_flag;
3483 int quota_flag;
3484 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003486 xfs_bmbt_irec_t imaps[1], *imapp;
3487 xfs_bmap_free_t free_list;
3488 uint qblocks, resblks, resrtextents;
3489 int committed;
3490 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003492 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 if (XFS_FORCED_SHUTDOWN(mp))
3495 return XFS_ERROR(EIO);
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3498 return error;
3499
3500 if (len <= 0)
3501 return XFS_ERROR(EINVAL);
3502
David Chinner957d0eb2007-06-18 16:50:37 +10003503 rt = XFS_IS_REALTIME_INODE(ip);
3504 extsz = xfs_get_extsz_hint(ip);
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003508 nimaps = 1;
3509 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3511 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3512
3513 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003514 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003516 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 xfs_off_t end_dmi_offset;
3518
3519 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003520 if (end_dmi_offset > ip->i_size)
3521 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003522 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3523 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003525 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 }
3527
3528 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003529 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 */
3531retry:
3532 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003533 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534
Nathan Scottdd9f4382006-01-11 15:28:28 +11003535 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003536 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003537 */
3538 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003540 do_div(s, extsz);
3541 s *= extsz;
3542 e = startoffset_fsb + allocatesize_fsb;
3543 if ((temp = do_mod(startoffset_fsb, extsz)))
3544 e += temp;
3545 if ((temp = do_mod(e, extsz)))
3546 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003548 s = 0;
3549 e = allocatesize_fsb;
3550 }
3551
3552 if (unlikely(rt)) {
3553 resrtextents = qblocks = (uint)(e - s);
3554 resrtextents /= mp->m_sb.sb_rextsize;
3555 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3556 quota_flag = XFS_QMOPT_RES_RTBLKS;
3557 } else {
3558 resrtextents = 0;
3559 resblks = qblocks = \
3560 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3561 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 }
3563
3564 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003565 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 */
3567 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003568 error = xfs_trans_reserve(tp, resblks,
3569 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 XFS_TRANS_PERM_LOG_RES,
3571 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003573 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 */
3575 if (error) {
3576 /*
3577 * Free the transaction structure.
3578 */
3579 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3580 xfs_trans_cancel(tp, 0);
3581 break;
3582 }
3583 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003584 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3585 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (error)
3587 goto error1;
3588
3589 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3590 xfs_trans_ihold(tp, ip);
3591
3592 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003593 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 */
3595 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003596 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003597 allocatesize_fsb, bmapi_flag,
3598 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003599 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (error) {
3601 goto error0;
3602 }
3603
3604 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003605 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003607 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 if (error) {
3609 goto error0;
3610 }
3611
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003612 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3614 if (error) {
3615 break;
3616 }
3617
3618 allocated_fsb = imapp->br_blockcount;
3619
Nathan Scottdd9f4382006-01-11 15:28:28 +11003620 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 error = XFS_ERROR(ENOSPC);
3622 break;
3623 }
3624
3625 startoffset_fsb += allocated_fsb;
3626 allocatesize_fsb -= allocated_fsb;
3627 }
3628dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003629 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3630 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003632 ip, DM_RIGHT_NULL,
3633 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3635 if (error == 0)
3636 goto retry; /* Maybe DMAPI app. has made space */
3637 /* else fall through with error from XFS_SEND_DATA */
3638 }
3639
3640 return error;
3641
Nathan Scottdd9f4382006-01-11 15:28:28 +11003642error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003644 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3645
3646error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3648 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3649 goto dmapi_enospc_check;
3650}
3651
3652/*
3653 * Zero file bytes between startoff and endoff inclusive.
3654 * The iolock is held exclusive and no blocks are buffered.
3655 */
3656STATIC int
3657xfs_zero_remaining_bytes(
3658 xfs_inode_t *ip,
3659 xfs_off_t startoff,
3660 xfs_off_t endoff)
3661{
3662 xfs_bmbt_irec_t imap;
3663 xfs_fileoff_t offset_fsb;
3664 xfs_off_t lastoffset;
3665 xfs_off_t offset;
3666 xfs_buf_t *bp;
3667 xfs_mount_t *mp = ip->i_mount;
3668 int nimap;
3669 int error = 0;
3670
3671 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003672 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 mp->m_rtdev_targp : mp->m_ddev_targp);
3674
3675 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3676 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3677 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003678 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003679 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (error || nimap < 1)
3681 break;
3682 ASSERT(imap.br_blockcount >= 1);
3683 ASSERT(imap.br_startoff == offset_fsb);
3684 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3685 if (lastoffset > endoff)
3686 lastoffset = endoff;
3687 if (imap.br_startblock == HOLESTARTBLOCK)
3688 continue;
3689 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3690 if (imap.br_state == XFS_EXT_UNWRITTEN)
3691 continue;
3692 XFS_BUF_UNDONE(bp);
3693 XFS_BUF_UNWRITE(bp);
3694 XFS_BUF_READ(bp);
3695 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3696 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003697 error = xfs_iowait(bp);
3698 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3700 mp, bp, XFS_BUF_ADDR(bp));
3701 break;
3702 }
3703 memset(XFS_BUF_PTR(bp) +
3704 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3705 0, lastoffset - offset + 1);
3706 XFS_BUF_UNDONE(bp);
3707 XFS_BUF_UNREAD(bp);
3708 XFS_BUF_WRITE(bp);
3709 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003710 error = xfs_iowait(bp);
3711 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3713 mp, bp, XFS_BUF_ADDR(bp));
3714 break;
3715 }
3716 }
3717 xfs_buf_free(bp);
3718 return error;
3719}
3720
3721/*
3722 * xfs_free_file_space()
3723 * This routine frees disk space for the given file.
3724 *
3725 * This routine is only called by xfs_change_file_space
3726 * for an UNRESVSP type call.
3727 *
3728 * RETURNS:
3729 * 0 on success
3730 * errno on error
3731 *
3732 */
3733STATIC int
3734xfs_free_file_space(
3735 xfs_inode_t *ip,
3736 xfs_off_t offset,
3737 xfs_off_t len,
3738 int attr_flags)
3739{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003740 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 int committed;
3742 int done;
3743 xfs_off_t end_dmi_offset;
3744 xfs_fileoff_t endoffset_fsb;
3745 int error;
3746 xfs_fsblock_t firstfsb;
3747 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 xfs_bmbt_irec_t imap;
3749 xfs_off_t ioffset;
3750 xfs_extlen_t mod=0;
3751 xfs_mount_t *mp;
3752 int nimap;
3753 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003754 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 int rt;
3756 xfs_fileoff_t startoffset_fsb;
3757 xfs_trans_t *tp;
Dean Roehrich5fcbab3552005-05-05 13:27:19 -07003758 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003760 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 mp = ip->i_mount;
3762
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003763 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3766 return error;
3767
3768 error = 0;
3769 if (len <= 0) /* if nothing being freed */
3770 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003771 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3773 end_dmi_offset = offset + len;
3774 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3775
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003776 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3777 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003778 if (end_dmi_offset > ip->i_size)
3779 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003780 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 offset, end_dmi_offset - offset,
3782 AT_DELAY_FLAG(attr_flags), NULL);
3783 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786
Dean Roehrich5fcbab3552005-05-05 13:27:19 -07003787 if (attr_flags & ATTR_NOLOCK)
3788 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003789 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003791 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003792 }
Dean Roehrich5fcbab3552005-05-05 13:27:19 -07003793
Tim Shimmine6a4b372007-11-23 16:30:42 +11003794 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003796
3797 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003798 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3799 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003800 if (error)
3801 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003802 }
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 /*
3805 * Need to zero the stuff we're not freeing, on disk.
3806 * If its a realtime file & can't use unwritten extents then we
3807 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3808 * will take care of it for us.
3809 */
Eric Sandeen62118702008-03-06 13:44:28 +11003810 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003812 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003813 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 if (error)
3815 goto out_unlock_iolock;
3816 ASSERT(nimap == 0 || nimap == 1);
3817 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3818 xfs_daddr_t block;
3819
3820 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3821 block = imap.br_startblock;
3822 mod = do_div(block, mp->m_sb.sb_rextsize);
3823 if (mod)
3824 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3825 }
3826 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003827 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003828 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (error)
3830 goto out_unlock_iolock;
3831 ASSERT(nimap == 0 || nimap == 1);
3832 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3833 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3834 mod++;
3835 if (mod && (mod != mp->m_sb.sb_rextsize))
3836 endoffset_fsb -= mod;
3837 }
3838 }
3839 if ((done = (endoffset_fsb <= startoffset_fsb)))
3840 /*
3841 * One contiguous piece to clear
3842 */
3843 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3844 else {
3845 /*
3846 * Some full blocks, possibly two pieces to clear
3847 */
3848 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3849 error = xfs_zero_remaining_bytes(ip, offset,
3850 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3851 if (!error &&
3852 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3853 error = xfs_zero_remaining_bytes(ip,
3854 XFS_FSB_TO_B(mp, endoffset_fsb),
3855 offset + len - 1);
3856 }
3857
3858 /*
3859 * free file space until done or until there is an error
3860 */
3861 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3862 while (!error && !done) {
3863
3864 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003865 * allocate and setup the transaction. Allow this
3866 * transaction to dip into the reserve blocks to ensure
3867 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 */
3869 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003870 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 error = xfs_trans_reserve(tp,
3872 resblks,
3873 XFS_WRITE_LOG_RES(mp),
3874 0,
3875 XFS_TRANS_PERM_LOG_RES,
3876 XFS_WRITE_LOG_COUNT);
3877
3878 /*
3879 * check for running out of space
3880 */
3881 if (error) {
3882 /*
3883 * Free the transaction structure.
3884 */
3885 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3886 xfs_trans_cancel(tp, 0);
3887 break;
3888 }
3889 xfs_ilock(ip, XFS_ILOCK_EXCL);
3890 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003891 ip->i_udquot, ip->i_gdquot, resblks, 0,
3892 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 if (error)
3894 goto error1;
3895
3896 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3897 xfs_trans_ihold(tp, ip);
3898
3899 /*
3900 * issue the bunmapi() call to free the blocks
3901 */
3902 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003903 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003905 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 if (error) {
3907 goto error0;
3908 }
3909
3910 /*
3911 * complete the transaction
3912 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003913 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 if (error) {
3915 goto error0;
3916 }
3917
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003918 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3920 }
3921
3922 out_unlock_iolock:
3923 if (need_iolock)
3924 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3925 return error;
3926
3927 error0:
3928 xfs_bmap_cancel(&free_list);
3929 error1:
3930 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3931 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3932 XFS_ILOCK_EXCL);
3933 return error;
3934}
3935
3936/*
3937 * xfs_change_file_space()
3938 * This routine allocates or frees disk space for the given file.
3939 * The user specified parameters are checked for alignment and size
3940 * limitations.
3941 *
3942 * RETURNS:
3943 * 0 on success
3944 * errno on error
3945 *
3946 */
3947int
3948xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003949 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 int cmd,
3951 xfs_flock64_t *bf,
3952 xfs_off_t offset,
3953 cred_t *credp,
3954 int attr_flags)
3955{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003956 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 int clrprealloc;
3958 int error;
3959 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 int setprealloc;
3961 xfs_off_t startoffset;
3962 xfs_off_t llen;
3963 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10003964 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003966 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
Christoph Hellwig993386c2007-08-28 16:12:30 +10003968 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return XFS_ERROR(EINVAL);
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 switch (bf->l_whence) {
3972 case 0: /*SEEK_SET*/
3973 break;
3974 case 1: /*SEEK_CUR*/
3975 bf->l_start += offset;
3976 break;
3977 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003978 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 break;
3980 default:
3981 return XFS_ERROR(EINVAL);
3982 }
3983
3984 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3985
3986 if ( (bf->l_start < 0)
3987 || (bf->l_start > XFS_MAXIOFFSET(mp))
3988 || (bf->l_start + llen < 0)
3989 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3990 return XFS_ERROR(EINVAL);
3991
3992 bf->l_whence = 0;
3993
3994 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003995 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 /*
3998 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3999 * file space.
4000 * These calls do NOT zero the data space allocated to the file,
4001 * nor do they change the file size.
4002 *
4003 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4004 * space.
4005 * These calls cause the new file data to be zeroed and the file
4006 * size to be changed.
4007 */
4008 setprealloc = clrprealloc = 0;
4009
4010 switch (cmd) {
4011 case XFS_IOC_RESVSP:
4012 case XFS_IOC_RESVSP64:
4013 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4014 1, attr_flags);
4015 if (error)
4016 return error;
4017 setprealloc = 1;
4018 break;
4019
4020 case XFS_IOC_UNRESVSP:
4021 case XFS_IOC_UNRESVSP64:
4022 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4023 attr_flags)))
4024 return error;
4025 break;
4026
4027 case XFS_IOC_ALLOCSP:
4028 case XFS_IOC_ALLOCSP64:
4029 case XFS_IOC_FREESP:
4030 case XFS_IOC_FREESP64:
4031 if (startoffset > fsize) {
4032 error = xfs_alloc_file_space(ip, fsize,
4033 startoffset - fsize, 0, attr_flags);
4034 if (error)
4035 break;
4036 }
4037
4038 va.va_mask = XFS_AT_SIZE;
4039 va.va_size = startoffset;
4040
Christoph Hellwig993386c2007-08-28 16:12:30 +10004041 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
4043 if (error)
4044 return error;
4045
4046 clrprealloc = 1;
4047 break;
4048
4049 default:
4050 ASSERT(0);
4051 return XFS_ERROR(EINVAL);
4052 }
4053
4054 /*
4055 * update the inode timestamp, mode, and prealloc flag bits
4056 */
4057 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4058
4059 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4060 0, 0, 0))) {
4061 /* ASSERT(0); */
4062 xfs_trans_cancel(tp, 0);
4063 return error;
4064 }
4065
4066 xfs_ilock(ip, XFS_ILOCK_EXCL);
4067
4068 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4069 xfs_trans_ihold(tp, ip);
4070
4071 if ((attr_flags & ATTR_DMI) == 0) {
4072 ip->i_d.di_mode &= ~S_ISUID;
4073
4074 /*
4075 * Note that we don't have to worry about mandatory
4076 * file locking being disabled here because we only
4077 * clear the S_ISGID bit if the Group execute bit is
4078 * on, but if it was on then mandatory locking wouldn't
4079 * have been enabled.
4080 */
4081 if (ip->i_d.di_mode & S_IXGRP)
4082 ip->i_d.di_mode &= ~S_ISGID;
4083
4084 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4085 }
4086 if (setprealloc)
4087 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4088 else if (clrprealloc)
4089 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4090
4091 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4092 xfs_trans_set_sync(tp);
4093
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004094 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4097
4098 return error;
4099}