blob: 9b8b87fcd4ec5538eb6d6a8d2174f515e571279f [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 Roehrich5fcbab32005-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 Roehrich5fcbab32005-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) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000447 /*
448 * Do the first part of growing a file: zero any data
449 * in the last block that is beyond the old EOF. We
450 * need to do this before the inode is joined to the
451 * transaction to modify the i_size.
452 */
453 code = xfs_zero_eof(ip, vap->va_size, ip->i_size);
Eric Sandeen374e2ac2005-11-02 15:07:34 +1100454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnerc32676e2007-07-19 16:28:58 +1000456
457 /*
458 * We are going to log the inode size change in this
459 * transaction so any previous writes that are beyond the on
460 * disk EOF and the new EOF that have not been written out need
461 * to be written here. If we do not write the data out, we
462 * expose ourselves to the null files problem.
463 *
464 * Only flush from the on disk size to the smaller of the in
465 * memory file size or the new size as that's the range we
466 * really care about here and prevents waiting for other data
467 * not within the range we care about here.
468 */
469 if (!code &&
470 (ip->i_size != ip->i_d.di_size) &&
471 (vap->va_size > ip->i_d.di_size)) {
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000472 code = xfs_flush_pages(ip,
David Chinnerc32676e2007-07-19 16:28:58 +1000473 ip->i_d.di_size, vap->va_size,
474 XFS_B_ASYNC, FI_NONE);
475 }
476
477 /* wait for all I/O to complete */
Christoph Hellwigb677c212007-08-29 11:46:28 +1000478 vn_iowait(ip);
David Chinnerc32676e2007-07-19 16:28:58 +1000479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!code)
481 code = xfs_itruncate_data(ip, vap->va_size);
482 if (code) {
483 ASSERT(tp == NULL);
484 lock_flags &= ~XFS_ILOCK_EXCL;
485 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
486 goto error_return;
487 }
488 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
489 if ((code = xfs_trans_reserve(tp, 0,
490 XFS_ITRUNCATE_LOG_RES(mp), 0,
491 XFS_TRANS_PERM_LOG_RES,
492 XFS_ITRUNCATE_LOG_COUNT))) {
493 xfs_trans_cancel(tp, 0);
494 if (need_iolock)
495 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
496 return code;
497 }
498 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
499 xfs_ilock(ip, XFS_ILOCK_EXCL);
500 }
501
502 if (tp) {
503 xfs_trans_ijoin(tp, ip, lock_flags);
504 xfs_trans_ihold(tp, ip);
505 }
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /*
508 * Truncate file. Must have write permission and not be a directory.
509 */
510 if (mask & XFS_AT_SIZE) {
David Chinner44d814c2008-03-06 13:45:29 +1100511 /*
512 * Only change the c/mtime if we are changing the size
513 * or we are explicitly asked to change it. This handles
514 * the semantic difference between truncate() and ftruncate()
515 * as implemented in the VFS.
516 */
517 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
518 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
519
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000520 if (vap->va_size > ip->i_size) {
Christoph Hellwig61436fe2008-05-20 11:30:46 +1000521 ip->i_d.di_size = vap->va_size;
522 ip->i_size = vap->va_size;
523 if (!(flags & ATTR_DMI))
524 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
525 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000526 } else if ((vap->va_size <= ip->i_size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
528 /*
529 * signal a sync transaction unless
530 * we're truncating an already unlinked
531 * file on a wsync filesystem
532 */
533 code = xfs_itruncate_finish(&tp, ip,
534 (xfs_fsize_t)vap->va_size,
535 XFS_DATA_FORK,
536 ((ip->i_d.di_nlink != 0 ||
537 !(mp->m_flags & XFS_MOUNT_WSYNC))
538 ? 1 : 0));
Nathan Scott7d4fb402006-06-09 15:27:16 +1000539 if (code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto abort_return;
Nathan Scott7d4fb402006-06-09 15:27:16 +1000541 /*
542 * Truncated "down", so we're removing references
543 * to old data here - if we now delay flushing for
544 * a long time, we expose ourselves unduly to the
545 * notorious NULL files problem. So, we mark this
546 * vnode and flush it when the file is closed, and
547 * do not wait the usual (long) time for writeout.
548 */
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +1000549 xfs_iflags_set(ip, XFS_ITRUNCATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
553 /*
554 * Change file access modes.
555 */
556 if (mask & XFS_AT_MODE) {
557 ip->i_d.di_mode &= S_IFMT;
558 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
559
560 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
561 timeflags |= XFS_ICHGTIME_CHG;
562 }
563
564 /*
565 * Change file ownership. Must be the owner or privileged.
566 * If the system was configured with the "restricted_chown"
567 * option, the owner is not permitted to give away the file,
568 * and can change the group id only to a group of which he
569 * or she is a member.
570 */
571 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
572 /*
573 * CAP_FSETID overrides the following restrictions:
574 *
575 * The set-user-ID and set-group-ID bits of a file will be
576 * cleared upon successful return from chown()
577 */
578 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
579 !capable(CAP_FSETID)) {
580 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
581 }
582
583 /*
584 * Change the ownerships and register quota modifications
585 * in the transaction.
586 */
587 if (iuid != uid) {
588 if (XFS_IS_UQUOTA_ON(mp)) {
589 ASSERT(mask & XFS_AT_UID);
590 ASSERT(udqp);
591 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
592 &ip->i_udquot, udqp);
593 }
594 ip->i_d.di_uid = uid;
595 }
596 if (igid != gid) {
597 if (XFS_IS_GQUOTA_ON(mp)) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000598 ASSERT(!XFS_IS_PQUOTA_ON(mp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ASSERT(mask & XFS_AT_GID);
600 ASSERT(gdqp);
601 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
602 &ip->i_gdquot, gdqp);
603 }
604 ip->i_d.di_gid = gid;
605 }
606 if (iprojid != projid) {
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000607 if (XFS_IS_PQUOTA_ON(mp)) {
608 ASSERT(!XFS_IS_GQUOTA_ON(mp));
609 ASSERT(mask & XFS_AT_PROJID);
610 ASSERT(gdqp);
611 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
612 &ip->i_gdquot, gdqp);
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ip->i_d.di_projid = projid;
615 /*
616 * We may have to rev the inode as well as
617 * the superblock version number since projids didn't
618 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
619 */
620 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
621 xfs_bump_ino_vers2(tp, ip);
622 }
623
624 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
625 timeflags |= XFS_ICHGTIME_CHG;
626 }
627
628
629 /*
630 * Change file access or modified times.
631 */
632 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
633 if (mask & XFS_AT_ATIME) {
634 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
635 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
636 ip->i_update_core = 1;
637 timeflags &= ~XFS_ICHGTIME_ACC;
638 }
639 if (mask & XFS_AT_MTIME) {
640 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
641 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
642 timeflags &= ~XFS_ICHGTIME_MOD;
643 timeflags |= XFS_ICHGTIME_CHG;
644 }
645 if (tp && (flags & ATTR_UTIME))
646 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
647 }
648
649 /*
650 * Change XFS-added attributes.
651 */
652 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
653 if (mask & XFS_AT_EXTSIZE) {
654 /*
655 * Converting bytes to fs blocks.
656 */
657 ip->i_d.di_extsize = vap->va_extsize >>
658 mp->m_sb.sb_blocklog;
659 }
660 if (mask & XFS_AT_XFLAGS) {
661 uint di_flags;
662
663 /* can't set PREALLOC this way, just preserve it */
664 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
665 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
666 di_flags |= XFS_DIFLAG_IMMUTABLE;
667 if (vap->va_xflags & XFS_XFLAG_APPEND)
668 di_flags |= XFS_DIFLAG_APPEND;
669 if (vap->va_xflags & XFS_XFLAG_SYNC)
670 di_flags |= XFS_DIFLAG_SYNC;
671 if (vap->va_xflags & XFS_XFLAG_NOATIME)
672 di_flags |= XFS_DIFLAG_NOATIME;
673 if (vap->va_xflags & XFS_XFLAG_NODUMP)
674 di_flags |= XFS_DIFLAG_NODUMP;
Nathan Scott365ca832005-06-21 15:39:12 +1000675 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
676 di_flags |= XFS_DIFLAG_PROJINHERIT;
Barry Naujokd3446ea2006-06-09 14:54:19 +1000677 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
678 di_flags |= XFS_DIFLAG_NODEFRAG;
David Chinner2a82b8b2007-07-11 11:09:12 +1000679 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
680 di_flags |= XFS_DIFLAG_FILESTREAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
682 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
683 di_flags |= XFS_DIFLAG_RTINHERIT;
684 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
685 di_flags |= XFS_DIFLAG_NOSYMLINKS;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100686 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
687 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
688 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
Christoph Hellwig613d7042007-10-11 17:44:08 +1000689 if (vap->va_xflags & XFS_XFLAG_REALTIME)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 di_flags |= XFS_DIFLAG_REALTIME;
Nathan Scottdd9f4382006-01-11 15:28:28 +1100691 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
692 di_flags |= XFS_DIFLAG_EXTSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 ip->i_d.di_flags = di_flags;
695 }
696 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
697 timeflags |= XFS_ICHGTIME_CHG;
698 }
699
700 /*
701 * Change file inode change time only if XFS_AT_CTIME set
702 * AND we have been called by a DMI function.
703 */
704
705 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
706 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
707 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
708 ip->i_update_core = 1;
709 timeflags &= ~XFS_ICHGTIME_CHG;
710 }
711
712 /*
713 * Send out timestamp changes that need to be set to the
714 * current time. Not done when called by a DMI function.
715 */
716 if (timeflags && !(flags & ATTR_DMI))
717 xfs_ichgtime(ip, timeflags);
718
719 XFS_STATS_INC(xs_ig_attrchg);
720
721 /*
722 * If this is a synchronous mount, make sure that the
723 * transaction goes to disk before returning to the user.
724 * This is slightly sub-optimal in that truncates require
Nathan Scottc41564b2006-03-29 08:55:14 +1000725 * two sync transactions instead of one for wsync filesystems.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * One for the truncate and one for the timestamps since we
727 * don't want to change the timestamps unless we're sure the
728 * truncate worked. Truncates are less than 1% of the laddis
729 * mix so this probably isn't worth the trouble to optimize.
730 */
731 code = 0;
732 if (tp) {
733 if (mp->m_flags & XFS_MOUNT_WSYNC)
734 xfs_trans_set_sync(tp);
735
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000736 code = xfs_trans_commit(tp, commit_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 xfs_iunlock(ip, lock_flags);
740
741 /*
742 * Release any dquot(s) the inode had kept before chown.
743 */
744 XFS_QM_DQRELE(mp, olddquot1);
745 XFS_QM_DQRELE(mp, olddquot2);
746 XFS_QM_DQRELE(mp, udqp);
747 XFS_QM_DQRELE(mp, gdqp);
748
749 if (code) {
750 return code;
751 }
752
Christoph Hellwigeb9df392007-08-16 18:42:07 +1000753 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 !(flags & ATTR_DMI)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +1100755 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 NULL, DM_RIGHT_NULL, NULL, NULL,
757 0, 0, AT_DELAY_FLAG(flags));
758 }
759 return 0;
760
761 abort_return:
762 commit_flags |= XFS_TRANS_ABORT;
763 /* FALLTHROUGH */
764 error_return:
765 XFS_QM_DQRELE(mp, udqp);
766 XFS_QM_DQRELE(mp, gdqp);
767 if (tp) {
768 xfs_trans_cancel(tp, commit_flags);
769 }
770 if (lock_flags != 0) {
771 xfs_iunlock(ip, lock_flags);
772 }
773 return code;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
Nathan Scott7d4fb402006-06-09 15:27:16 +1000777 * The maximum pathlen is 1024 bytes. Since the minimum file system
778 * blocksize is 512 bytes, we can get a max of 2 extents back from
779 * bmapi.
780 */
781#define SYMLINK_MAPS 2
782
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000783STATIC int
784xfs_readlink_bmap(
785 xfs_inode_t *ip,
786 char *link)
787{
788 xfs_mount_t *mp = ip->i_mount;
789 int pathlen = ip->i_d.di_size;
790 int nmaps = SYMLINK_MAPS;
791 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
792 xfs_daddr_t d;
793 int byte_cnt;
794 int n;
795 xfs_buf_t *bp;
796 int error = 0;
797
798 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
799 mval, &nmaps, NULL, NULL);
800 if (error)
801 goto out;
802
803 for (n = 0; n < nmaps; n++) {
804 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
805 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
806
807 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
808 error = XFS_BUF_GETERROR(bp);
809 if (error) {
810 xfs_ioerror_alert("xfs_readlink",
811 ip->i_mount, bp, XFS_BUF_ADDR(bp));
812 xfs_buf_relse(bp);
813 goto out;
814 }
815 if (pathlen < byte_cnt)
816 byte_cnt = pathlen;
817 pathlen -= byte_cnt;
818
819 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
820 xfs_buf_relse(bp);
821 }
822
823 link[ip->i_d.di_size] = '\0';
824 error = 0;
825
826 out:
827 return error;
828}
829
Christoph Hellwig993386c2007-08-28 16:12:30 +1000830int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831xfs_readlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +1000832 xfs_inode_t *ip,
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000833 char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000835 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 int pathlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100839 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (XFS_FORCED_SHUTDOWN(mp))
842 return XFS_ERROR(EIO);
843
844 xfs_ilock(ip, XFS_ILOCK_SHARED);
845
846 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000847 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000849 pathlen = ip->i_d.di_size;
850 if (!pathlen)
851 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (ip->i_df.if_flags & XFS_IFINLINE) {
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000854 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
855 link[pathlen] = '\0';
856 } else {
857 error = xfs_readlink_bmap(ip, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Christoph Hellwig804c83c2007-08-28 13:59:03 +1000860 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return error;
863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/*
866 * xfs_fsync
867 *
David Chinner978b7232008-05-19 16:29:46 +1000868 * This is called to sync the inode and its data out to disk. We need to hold
869 * the I/O lock while flushing the data, and the inode lock while flushing the
870 * inode. The inode lock CANNOT be held while flushing the data, so acquire
871 * after we're done with that.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 */
Christoph Hellwig993386c2007-08-28 16:12:30 +1000873int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874xfs_fsync(
David Chinner978b7232008-05-19 16:29:46 +1000875 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 xfs_trans_t *tp;
878 int error;
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100879 int log_flushed = 0, changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Lachlan McIlroycf441ee2008-02-07 16:42:19 +1100881 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
884 return XFS_ERROR(EIO);
885
David Chinner978b7232008-05-19 16:29:46 +1000886 /* capture size updates in I/O completion before writing the inode. */
887 error = filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
888 if (error)
889 return XFS_ERROR(error);
Lachlan McIlroy776a75f2007-09-14 15:22:50 +1000890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
David Chinner978b7232008-05-19 16:29:46 +1000892 * We always need to make sure that the required inode state is safe on
893 * disk. The vnode might be clean but we still might need to force the
894 * log because of committed transactions that haven't hit the disk yet.
895 * Likewise, there could be unflushed non-transactional changes to the
896 * inode core that have to go to disk and this requires us to issue
897 * a synchronous transaction to capture these changes correctly.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *
David Chinner978b7232008-05-19 16:29:46 +1000899 * This code relies on the assumption that if the update_* fields
900 * of the inode are clear and the inode is unpinned then it is clean
901 * and no action is required.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 */
903 xfs_ilock(ip, XFS_ILOCK_SHARED);
904
David Chinner978b7232008-05-19 16:29:46 +1000905 if (!(ip->i_update_size || ip->i_update_core)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
David Chinner978b7232008-05-19 16:29:46 +1000907 * Timestamps/size haven't changed since last inode flush or
908 * inode transaction commit. That means either nothing got
909 * written or a transaction committed which caught the updates.
910 * If the latter happened and the transaction hasn't hit the
911 * disk yet, the inode will be still be pinned. If it is,
912 * force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 */
914
915 xfs_iunlock(ip, XFS_ILOCK_SHARED);
916
917 if (xfs_ipincount(ip)) {
David Chinner978b7232008-05-19 16:29:46 +1000918 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
919 XFS_LOG_FORCE | XFS_LOG_SYNC,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100920 &log_flushed);
921 } else {
922 /*
David Chinner978b7232008-05-19 16:29:46 +1000923 * If the inode is not pinned and nothing has changed
924 * we don't need to flush the cache.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100925 */
926 changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else {
929 /*
David Chinner978b7232008-05-19 16:29:46 +1000930 * Kick off a transaction to log the inode core to get the
931 * updates. The sync transaction will also force the log.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933 xfs_iunlock(ip, XFS_ILOCK_SHARED);
934 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
David Chinner978b7232008-05-19 16:29:46 +1000935 error = xfs_trans_reserve(tp, 0,
936 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
937 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 xfs_trans_cancel(tp, 0);
939 return error;
940 }
941 xfs_ilock(ip, XFS_ILOCK_EXCL);
942
943 /*
David Chinner978b7232008-05-19 16:29:46 +1000944 * Note - it's possible that we might have pushed ourselves out
945 * of the way during trans_reserve which would flush the inode.
946 * But there's no guarantee that the inode buffer has actually
947 * gone out yet (it's delwri). Plus the buffer could be pinned
948 * anyway if it's part of an inode in another recent
949 * transaction. So we play it safe and fire off the
950 * transaction anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
952 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
953 xfs_trans_ihold(tp, ip);
954 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
David Chinner978b7232008-05-19 16:29:46 +1000955 xfs_trans_set_sync(tp);
Eric Sandeen1c72bf92007-05-08 13:48:42 +1000956 error = _xfs_trans_commit(tp, 0, &log_flushed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 xfs_iunlock(ip, XFS_ILOCK_EXCL);
959 }
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100960
961 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
962 /*
963 * If the log write didn't issue an ordered tag we need
964 * to flush the disk cache for the data device now.
965 */
966 if (!log_flushed)
967 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
968
969 /*
970 * If this inode is on the RT dev we need to flush that
Nathan Scottc41564b2006-03-29 08:55:14 +1000971 * cache as well.
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100972 */
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100973 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100974 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
975 }
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 return error;
978}
979
980/*
David Chinner92dfe8d2007-05-24 15:22:19 +1000981 * This is called by xfs_inactive to free any blocks beyond eof
982 * when the link count isn't zero and by xfs_dm_punch_hole() when
983 * punching a hole to EOF.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
David Chinner92dfe8d2007-05-24 15:22:19 +1000985int
986xfs_free_eofblocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 xfs_mount_t *mp,
David Chinner92dfe8d2007-05-24 15:22:19 +1000988 xfs_inode_t *ip,
989 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
991 xfs_trans_t *tp;
992 int error;
993 xfs_fileoff_t end_fsb;
994 xfs_fileoff_t last_fsb;
995 xfs_filblks_t map_len;
996 int nimaps;
997 xfs_bmbt_irec_t imap;
David Chinner92dfe8d2007-05-24 15:22:19 +1000998 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
1000 /*
1001 * Figure out if there are any blocks beyond the end
1002 * of the file. If not, then there is nothing to do.
1003 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001004 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1006 map_len = last_fsb - end_fsb;
1007 if (map_len <= 0)
Jesper Juhl014c2542006-01-15 02:37:08 +01001008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 nimaps = 1;
1011 xfs_ilock(ip, XFS_ILOCK_SHARED);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001012 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001013 NULL, 0, &imap, &nimaps, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1015
1016 if (!error && (nimaps != 0) &&
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001017 (imap.br_startblock != HOLESTARTBLOCK ||
1018 ip->i_delayed_blks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /*
1020 * Attach the dquots to the inode up front.
1021 */
1022 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001023 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /*
1026 * There are blocks after the end of file.
1027 * Free them up now by truncating the file to
1028 * its current size.
1029 */
1030 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1031
1032 /*
1033 * Do the xfs_itruncate_start() call before
1034 * reserving any log space because
1035 * itruncate_start will call into the buffer
1036 * cache and we can't
1037 * do that within a transaction.
1038 */
David Chinner92dfe8d2007-05-24 15:22:19 +10001039 if (use_iolock)
1040 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001041 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001042 ip->i_size);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001043 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001044 xfs_trans_cancel(tp, 0);
David Chinner92dfe8d2007-05-24 15:22:19 +10001045 if (use_iolock)
1046 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001047 return error;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 error = xfs_trans_reserve(tp, 0,
1051 XFS_ITRUNCATE_LOG_RES(mp),
1052 0, XFS_TRANS_PERM_LOG_RES,
1053 XFS_ITRUNCATE_LOG_COUNT);
1054 if (error) {
1055 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1056 xfs_trans_cancel(tp, 0);
1057 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001058 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
1061 xfs_ilock(ip, XFS_ILOCK_EXCL);
1062 xfs_trans_ijoin(tp, ip,
1063 XFS_IOLOCK_EXCL |
1064 XFS_ILOCK_EXCL);
1065 xfs_trans_ihold(tp, ip);
1066
1067 error = xfs_itruncate_finish(&tp, ip,
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001068 ip->i_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 XFS_DATA_FORK,
1070 0);
1071 /*
1072 * If we get an error at this point we
1073 * simply don't bother truncating the file.
1074 */
1075 if (error) {
1076 xfs_trans_cancel(tp,
1077 (XFS_TRANS_RELEASE_LOG_RES |
1078 XFS_TRANS_ABORT));
1079 } else {
1080 error = xfs_trans_commit(tp,
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001081 XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
David Chinner92dfe8d2007-05-24 15:22:19 +10001083 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1084 : XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001086 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * Free a symlink that has blocks associated with it.
1091 */
1092STATIC int
1093xfs_inactive_symlink_rmt(
1094 xfs_inode_t *ip,
1095 xfs_trans_t **tpp)
1096{
1097 xfs_buf_t *bp;
1098 int committed;
1099 int done;
1100 int error;
1101 xfs_fsblock_t first_block;
1102 xfs_bmap_free_t free_list;
1103 int i;
1104 xfs_mount_t *mp;
1105 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1106 int nmaps;
1107 xfs_trans_t *ntp;
1108 int size;
1109 xfs_trans_t *tp;
1110
1111 tp = *tpp;
1112 mp = ip->i_mount;
1113 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1114 /*
1115 * We're freeing a symlink that has some
1116 * blocks allocated to it. Free the
1117 * blocks here. We know that we've got
1118 * either 1 or 2 extents and that we can
1119 * free them all in one bunmapi call.
1120 */
1121 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1122 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1123 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1124 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1125 xfs_trans_cancel(tp, 0);
1126 *tpp = NULL;
1127 return error;
1128 }
1129 /*
1130 * Lock the inode, fix the size, and join it to the transaction.
1131 * Hold it so in the normal path, we still have it locked for
1132 * the second transaction. In the error paths we need it
1133 * held so the cancel won't rele it, see below.
1134 */
1135 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1136 size = (int)ip->i_d.di_size;
1137 ip->i_d.di_size = 0;
1138 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1139 xfs_trans_ihold(tp, ip);
1140 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1141 /*
1142 * Find the block(s) so we can inval and unmap them.
1143 */
1144 done = 0;
1145 XFS_BMAP_INIT(&free_list, &first_block);
Tobias Klausere8c96f82006-03-24 03:15:34 -08001146 nmaps = ARRAY_SIZE(mval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1148 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001149 &free_list, NULL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 goto error0;
1151 /*
1152 * Invalidate the block(s).
1153 */
1154 for (i = 0; i < nmaps; i++) {
1155 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1156 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1157 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1158 xfs_trans_binval(tp, bp);
1159 }
1160 /*
1161 * Unmap the dead block(s) to the free_list.
1162 */
1163 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10001164 &first_block, &free_list, NULL, &done)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto error1;
1166 ASSERT(done);
1167 /*
1168 * Commit the first transaction. This logs the EFI and the inode.
1169 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001170 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto error1;
1172 /*
1173 * The transaction must have been committed, since there were
1174 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1175 * The new tp has the extent freeing and EFDs.
1176 */
1177 ASSERT(committed);
1178 /*
1179 * The first xact was committed, so add the inode to the new one.
1180 * Mark it dirty so it will be logged and moved forward in the log as
1181 * part of every commit.
1182 */
1183 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1184 xfs_trans_ihold(tp, ip);
1185 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1186 /*
1187 * Get a new, empty transaction to return to our caller.
1188 */
1189 ntp = xfs_trans_dup(tp);
1190 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001191 * Commit the transaction containing extent freeing and EFDs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 * If we get an error on the commit here or on the reserve below,
1193 * we need to unlock the inode since the new transaction doesn't
1194 * have the inode attached.
1195 */
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001196 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 tp = ntp;
1198 if (error) {
1199 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1200 goto error0;
1201 }
1202 /*
1203 * Remove the memory for extent descriptions (just bookkeeping).
1204 */
1205 if (ip->i_df.if_bytes)
1206 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1207 ASSERT(ip->i_df.if_bytes == 0);
1208 /*
1209 * Put an itruncate log reservation in the new transaction
1210 * for our caller.
1211 */
1212 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1213 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1214 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1215 goto error0;
1216 }
1217 /*
1218 * Return with the inode locked but not joined to the transaction.
1219 */
1220 *tpp = tp;
1221 return 0;
1222
1223 error1:
1224 xfs_bmap_cancel(&free_list);
1225 error0:
1226 /*
1227 * Have to come here with the inode locked and either
1228 * (held and in the transaction) or (not in the transaction).
1229 * If the inode isn't held then cancel would iput it, but
1230 * that's wrong since this is inactive and the vnode ref
1231 * count is 0 already.
1232 * Cancel won't do anything to the inode if held, but it still
1233 * needs to be locked until the cancel is done, if it was
1234 * joined to the transaction.
1235 */
1236 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1237 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1238 *tpp = NULL;
1239 return error;
1240
1241}
1242
1243STATIC int
1244xfs_inactive_symlink_local(
1245 xfs_inode_t *ip,
1246 xfs_trans_t **tpp)
1247{
1248 int error;
1249
1250 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1251 /*
1252 * We're freeing a symlink which fit into
1253 * the inode. Just free the memory used
1254 * to hold the old symlink.
1255 */
1256 error = xfs_trans_reserve(*tpp, 0,
1257 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1258 0, XFS_TRANS_PERM_LOG_RES,
1259 XFS_ITRUNCATE_LOG_COUNT);
1260
1261 if (error) {
1262 xfs_trans_cancel(*tpp, 0);
1263 *tpp = NULL;
Jesper Juhl014c2542006-01-15 02:37:08 +01001264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1267
1268 /*
1269 * Zero length symlinks _can_ exist.
1270 */
1271 if (ip->i_df.if_bytes > 0) {
1272 xfs_idata_realloc(ip,
1273 -(ip->i_df.if_bytes),
1274 XFS_DATA_FORK);
1275 ASSERT(ip->i_df.if_bytes == 0);
1276 }
Jesper Juhl014c2542006-01-15 02:37:08 +01001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280STATIC int
1281xfs_inactive_attrs(
1282 xfs_inode_t *ip,
1283 xfs_trans_t **tpp)
1284{
1285 xfs_trans_t *tp;
1286 int error;
1287 xfs_mount_t *mp;
1288
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001289 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 tp = *tpp;
1291 mp = ip->i_mount;
1292 ASSERT(ip->i_d.di_forkoff != 0);
David Chinnere5720ee2008-04-10 12:21:18 +10001293 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 xfs_iunlock(ip, XFS_ILOCK_EXCL);
David Chinnere5720ee2008-04-10 12:21:18 +10001295 if (error)
1296 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 error = xfs_attr_inactive(ip);
David Chinnere5720ee2008-04-10 12:21:18 +10001299 if (error)
1300 goto error_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1303 error = xfs_trans_reserve(tp, 0,
1304 XFS_IFREE_LOG_RES(mp),
1305 0, XFS_TRANS_PERM_LOG_RES,
1306 XFS_INACTIVE_LOG_COUNT);
David Chinnere5720ee2008-04-10 12:21:18 +10001307 if (error)
1308 goto error_cancel;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 xfs_ilock(ip, XFS_ILOCK_EXCL);
1311 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1312 xfs_trans_ihold(tp, ip);
1313 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1314
1315 ASSERT(ip->i_d.di_anextents == 0);
1316
1317 *tpp = tp;
Jesper Juhl014c2542006-01-15 02:37:08 +01001318 return 0;
David Chinnere5720ee2008-04-10 12:21:18 +10001319
1320error_cancel:
1321 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1322 xfs_trans_cancel(tp, 0);
1323error_unlock:
1324 *tpp = NULL;
1325 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1326 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Christoph Hellwig993386c2007-08-28 16:12:30 +10001329int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330xfs_release(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001331 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001333 bhv_vnode_t *vp = XFS_ITOV(ip);
1334 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 int error;
1336
Christoph Hellwig42173f62008-04-22 17:33:25 +10001337 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001341 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343
David Chinner2a82b8b2007-07-11 11:09:12 +10001344 if (!XFS_FORCED_SHUTDOWN(mp)) {
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001345 int truncated;
1346
David Chinner2a82b8b2007-07-11 11:09:12 +10001347 /*
1348 * If we are using filestreams, and we have an unlinked
1349 * file that we are processing the last close on, then nothing
1350 * will be able to reopen and write to this file. Purge this
1351 * inode from the filestreams cache so that it doesn't delay
1352 * teardown of the inode.
1353 */
1354 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1355 xfs_filestream_deassociate(ip);
1356
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001357 /*
1358 * If we previously truncated this file and removed old data
1359 * in the process, we want to initiate "early" writeout on
1360 * the last close. This is an attempt to combat the notorious
1361 * NULL files problem which is particularly noticable from a
1362 * truncate down, buffered (re-)write (delalloc), followed by
1363 * a crash. What we are effectively doing here is
1364 * significantly reducing the time window where we'd otherwise
1365 * be exposed to that problem.
1366 */
Christoph Hellwig09262b42007-08-29 11:44:50 +10001367 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001368 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001369 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
Christoph Hellwigfbf3ce82007-06-28 16:46:47 +10001370 }
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (ip->i_d.di_nlink != 0) {
1373 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001374 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001375 ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001377 (!(ip->i_d.di_flags &
1378 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001379 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1380 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 }
1384
1385 return 0;
1386}
1387
1388/*
1389 * xfs_inactive
1390 *
1391 * This is called when the vnode reference count for the vnode
1392 * goes to zero. If the file has been unlinked, then it must
1393 * now be truncated. Also, we clear all of the read-ahead state
1394 * kept for the inode here since the file is now closed.
1395 */
Christoph Hellwig993386c2007-08-28 16:12:30 +10001396int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397xfs_inactive(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001398 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Christoph Hellwig993386c2007-08-28 16:12:30 +10001400 bhv_vnode_t *vp = XFS_ITOV(ip);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001401 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 xfs_fsblock_t first_block;
1403 int committed;
1404 xfs_trans_t *tp;
1405 xfs_mount_t *mp;
1406 int error;
1407 int truncate;
1408
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001409 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /*
1412 * If the inode is already free, then there can be nothing
1413 * to clean up here.
1414 */
1415 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1416 ASSERT(ip->i_df.if_real_bytes == 0);
1417 ASSERT(ip->i_df.if_broot_bytes == 0);
1418 return VN_INACTIVE_CACHE;
1419 }
1420
1421 /*
1422 * Only do a truncate if it's a regular file with
1423 * some actual space in it. It's OK to look at the
1424 * inode's fields without the lock because we're the
1425 * only one with a reference to the inode.
1426 */
1427 truncate = ((ip->i_d.di_nlink == 0) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001428 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1429 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1431
1432 mp = ip->i_mount;
1433
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001434 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1435 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 error = 0;
1438
1439 /* If this is a read-only mount, don't do this (would generate I/O) */
Christoph Hellwigbd186aa2007-08-30 17:21:12 +10001440 if (mp->m_flags & XFS_MOUNT_RDONLY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto out;
1442
1443 if (ip->i_d.di_nlink != 0) {
1444 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001445 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
Yingping Lu68bdb6e2006-01-11 15:38:31 +11001446 ip->i_delayed_blks > 0)) &&
Nathan Scottdd9f4382006-01-11 15:28:28 +11001447 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1448 (!(ip->i_d.di_flags &
1449 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1450 (ip->i_delayed_blks != 0)))) {
David Chinner92dfe8d2007-05-24 15:22:19 +10001451 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1452 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001453 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 goto out;
1456 }
1457
1458 ASSERT(ip->i_d.di_nlink == 0);
1459
1460 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
Jesper Juhl014c2542006-01-15 02:37:08 +01001461 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1464 if (truncate) {
1465 /*
1466 * Do the xfs_itruncate_start() call before
1467 * reserving any log space because itruncate_start
1468 * will call into the buffer cache and we can't
1469 * do that within a transaction.
1470 */
1471 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1472
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001473 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1474 if (error) {
Jesper Juhl87ae3c22007-06-28 16:43:14 +10001475 xfs_trans_cancel(tp, 0);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10001476 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1477 return VN_INACTIVE_CACHE;
1478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 error = xfs_trans_reserve(tp, 0,
1481 XFS_ITRUNCATE_LOG_RES(mp),
1482 0, XFS_TRANS_PERM_LOG_RES,
1483 XFS_ITRUNCATE_LOG_COUNT);
1484 if (error) {
1485 /* Don't call itruncate_cleanup */
1486 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1487 xfs_trans_cancel(tp, 0);
1488 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001489 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
1491
1492 xfs_ilock(ip, XFS_ILOCK_EXCL);
1493 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1494 xfs_trans_ihold(tp, ip);
1495
1496 /*
1497 * normally, we have to run xfs_itruncate_finish sync.
1498 * But if filesystem is wsync and we're in the inactive
1499 * path, then we know that nlink == 0, and that the
1500 * xaction that made nlink == 0 is permanently committed
1501 * since xfs_remove runs as a synchronous transaction.
1502 */
1503 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1504 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1505
1506 if (error) {
1507 xfs_trans_cancel(tp,
1508 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1509 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001510 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1513
1514 /*
1515 * If we get an error while cleaning up a
1516 * symlink we bail out.
1517 */
1518 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1519 xfs_inactive_symlink_rmt(ip, &tp) :
1520 xfs_inactive_symlink_local(ip, &tp);
1521
1522 if (error) {
1523 ASSERT(tp == NULL);
Jesper Juhl014c2542006-01-15 02:37:08 +01001524 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526
1527 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1528 xfs_trans_ihold(tp, ip);
1529 } else {
1530 error = xfs_trans_reserve(tp, 0,
1531 XFS_IFREE_LOG_RES(mp),
1532 0, XFS_TRANS_PERM_LOG_RES,
1533 XFS_INACTIVE_LOG_COUNT);
1534 if (error) {
1535 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1536 xfs_trans_cancel(tp, 0);
Jesper Juhl014c2542006-01-15 02:37:08 +01001537 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1541 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1542 xfs_trans_ihold(tp, ip);
1543 }
1544
1545 /*
1546 * If there are attributes associated with the file
1547 * then blow them away now. The code calls a routine
1548 * that recursively deconstructs the attribute fork.
1549 * We need to just commit the current transaction
1550 * because we can't use it for xfs_attr_inactive().
1551 */
1552 if (ip->i_d.di_anextents > 0) {
1553 error = xfs_inactive_attrs(ip, &tp);
1554 /*
1555 * If we got an error, the transaction is already
1556 * cancelled, and the inode is unlocked. Just get out.
1557 */
1558 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01001559 return VN_INACTIVE_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 } else if (ip->i_afp) {
1561 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1562 }
1563
1564 /*
1565 * Free the inode.
1566 */
1567 XFS_BMAP_INIT(&free_list, &first_block);
1568 error = xfs_ifree(tp, ip, &free_list);
1569 if (error) {
1570 /*
1571 * If we fail to free the inode, shut down. The cancel
1572 * might do that, we need to make sure. Otherwise the
1573 * inode might be lost for a long time or forever.
1574 */
1575 if (!XFS_FORCED_SHUTDOWN(mp)) {
1576 cmn_err(CE_NOTE,
1577 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1578 error, mp->m_fsname);
Nathan Scott7d04a332006-06-09 14:58:38 +10001579 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1582 } else {
1583 /*
1584 * Credit the quota account(s). The inode is gone.
1585 */
1586 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1587
1588 /*
David Chinner78e9da72008-04-10 12:24:17 +10001589 * Just ignore errors at this point. There is nothing we can
1590 * do except to try to keep going. Make sure it's not a silent
1591 * error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
David Chinner78e9da72008-04-10 12:24:17 +10001593 error = xfs_bmap_finish(&tp, &free_list, &committed);
1594 if (error)
1595 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1596 "xfs_bmap_finish() returned error %d", error);
1597 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1598 if (error)
1599 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1600 "xfs_trans_commit() returned error %d", error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602 /*
1603 * Release the dquots held by inode, if any.
1604 */
1605 XFS_QM_DQDETACH(mp, ip);
1606
1607 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1608
1609 out:
1610 return VN_INACTIVE_CACHE;
1611}
1612
1613
Christoph Hellwig993386c2007-08-28 16:12:30 +10001614int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615xfs_lookup(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001616 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001617 struct xfs_name *name,
Christoph Hellwigef1f5e72008-03-06 13:46:25 +11001618 xfs_inode_t **ipp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001620 xfs_ino_t inum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int error;
1622 uint lock_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001624 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1627 return XFS_ERROR(EIO);
1628
1629 lock_mode = xfs_ilock_map_shared(dp);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001630 error = xfs_dir_lookup(NULL, dp, name, &inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 xfs_iunlock_map_shared(dp, lock_mode);
Christoph Hellwigeca450b2008-04-22 17:33:52 +10001632
1633 if (error)
1634 goto out;
1635
1636 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1637 if (error)
1638 goto out;
1639
1640 xfs_itrace_ref(*ipp);
1641 return 0;
1642
1643 out:
1644 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return error;
1646}
1647
Christoph Hellwig993386c2007-08-28 16:12:30 +10001648int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649xfs_create(
Christoph Hellwig993386c2007-08-28 16:12:30 +10001650 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10001651 struct xfs_name *name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001652 mode_t mode,
1653 xfs_dev_t rdev,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001654 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 cred_t *credp)
1656{
Barry Naujok556b8b12008-04-10 12:22:07 +10001657 xfs_mount_t *mp = dp->i_mount;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001658 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 xfs_trans_t *tp;
Barry Naujok556b8b12008-04-10 12:22:07 +10001660 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 xfs_bmap_free_t free_list;
1662 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10001663 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int dm_event_sent = 0;
1665 uint cancel_flags;
1666 int committed;
1667 xfs_prid_t prid;
1668 struct xfs_dquot *udqp, *gdqp;
1669 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001671 ASSERT(!*ipp);
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001672 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Christoph Hellwigeb9df392007-08-16 18:42:07 +10001674 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001676 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001677 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001678 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (error)
1681 return error;
1682 dm_event_sent = 1;
1683 }
1684
1685 if (XFS_FORCED_SHUTDOWN(mp))
1686 return XFS_ERROR(EIO);
1687
1688 /* Return through std_return after this point. */
1689
1690 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10001691 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1692 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 else
1694 prid = (xfs_prid_t)dfltprid;
1695
1696 /*
1697 * Make sure that we have allocated dquot(s) on disk.
1698 */
1699 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10001700 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1702 if (error)
1703 goto std_return;
1704
1705 ip = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1708 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10001709 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /*
1711 * Initially assume that the file does not exist and
1712 * reserve the resources for that case. If that is not
1713 * the case we'll drop the one we have and get a more
1714 * appropriate transaction later.
1715 */
1716 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1717 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1718 if (error == ENOSPC) {
1719 resblks = 0;
1720 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1721 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1722 }
1723 if (error) {
1724 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto error_return;
1726 }
1727
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001728 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001729 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 XFS_BMAP_INIT(&free_list, &first_block);
1732
1733 ASSERT(ip == NULL);
1734
1735 /*
1736 * Reserve disk quota and the inode.
1737 */
1738 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1739 if (error)
1740 goto error_return;
1741
Barry Naujok556b8b12008-04-10 12:22:07 +10001742 error = xfs_dir_canenter(tp, dp, name, resblks);
1743 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 goto error_return;
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001745 error = xfs_dir_ialloc(&tp, dp, mode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 rdev, credp, prid, resblks > 0,
1747 &ip, &committed);
1748 if (error) {
1749 if (error == ENOSPC)
1750 goto error_return;
1751 goto abort_return;
1752 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001753 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /*
1756 * At this point, we've gotten a newly allocated inode.
1757 * It is locked (and joined to the transaction).
1758 */
1759
Christoph Hellwig579aa9c2008-04-22 17:34:00 +10001760 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
Christoph Hellwig993386c2007-08-28 16:12:30 +10001763 * Now we join the directory inode to the transaction. We do not do it
1764 * earlier because xfs_dir_ialloc might commit the previous transaction
1765 * (and release all the locks). An error from here on will result in
1766 * the transaction cancel unlocking dp so don't do it explicitly in the
1767 * error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001769 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10001771 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Barry Naujok556b8b12008-04-10 12:22:07 +10001773 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10001774 &first_block, &free_list, resblks ?
1775 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (error) {
1777 ASSERT(error != ENOSPC);
1778 goto abort_return;
1779 }
1780 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1781 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1782
1783 /*
1784 * If this is a synchronous mount, make sure that the
1785 * create transaction goes to disk before returning to
1786 * the user.
1787 */
1788 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1789 xfs_trans_set_sync(tp);
1790 }
1791
1792 dp->i_gen++;
1793
1794 /*
1795 * Attach the dquot(s) to the inodes and modify them incore.
1796 * These ids of the inode couldn't have changed since the new
1797 * inode has been locked ever since it was created.
1798 */
1799 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1800
1801 /*
1802 * xfs_trans_commit normally decrements the vnode ref count
1803 * when it unlocks the inode. Since we want to return the
1804 * vnode to the caller, we bump the vnode ref count now.
1805 */
1806 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Eric Sandeenf7c99b62007-02-10 18:37:16 +11001808 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (error) {
1810 xfs_bmap_cancel(&free_list);
1811 goto abort_rele;
1812 }
1813
Eric Sandeen1c72bf92007-05-08 13:48:42 +10001814 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (error) {
1816 IRELE(ip);
1817 tp = NULL;
1818 goto error_return;
1819 }
1820
1821 XFS_QM_DQRELE(mp, udqp);
1822 XFS_QM_DQRELE(mp, gdqp);
1823
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001824 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Fallthrough to std_return with error = 0 */
1827
1828std_return:
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001829 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10001830 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11001832 dp, DM_RIGHT_NULL,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11001833 *ipp ? ip : NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10001834 DM_RIGHT_NULL, name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10001835 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
1837 return error;
1838
1839 abort_return:
1840 cancel_flags |= XFS_TRANS_ABORT;
1841 /* FALLTHROUGH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jesper Juhl014c2542006-01-15 02:37:08 +01001843 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 if (tp != NULL)
1845 xfs_trans_cancel(tp, cancel_flags);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 XFS_QM_DQRELE(mp, udqp);
1848 XFS_QM_DQRELE(mp, gdqp);
1849
Christoph Hellwig993386c2007-08-28 16:12:30 +10001850 if (unlock_dp_on_error)
1851 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto std_return;
1854
1855 abort_rele:
1856 /*
1857 * Wait until after the current transaction is aborted to
1858 * release the inode. This prevents recursive transactions
1859 * and deadlocks from xfs_inactive.
1860 */
1861 cancel_flags |= XFS_TRANS_ABORT;
1862 xfs_trans_cancel(tp, cancel_flags);
1863 IRELE(ip);
1864
1865 XFS_QM_DQRELE(mp, udqp);
1866 XFS_QM_DQRELE(mp, gdqp);
1867
1868 goto std_return;
1869}
1870
1871#ifdef DEBUG
1872/*
1873 * Some counters to see if (and how often) we are hitting some deadlock
1874 * prevention code paths.
1875 */
1876
1877int xfs_rm_locks;
1878int xfs_rm_lock_delays;
1879int xfs_rm_attempts;
1880#endif
1881
1882/*
1883 * The following routine will lock the inodes associated with the
1884 * directory and the named entry in the directory. The locks are
1885 * acquired in increasing inode number.
1886 *
1887 * If the entry is "..", then only the directory is locked. The
1888 * vnode ref count will still include that from the .. entry in
1889 * this case.
1890 *
1891 * There is a deadlock we need to worry about. If the locked directory is
1892 * in the AIL, it might be blocking up the log. The next inode we lock
1893 * could be already locked by another thread waiting for log space (e.g
1894 * a permanent log reservation with a long running transaction (see
1895 * xfs_itruncate_finish)). To solve this, we must check if the directory
1896 * is in the ail and use lock_nowait. If we can't lock, we need to
1897 * drop the inode lock on the directory and try again. xfs_iunlock will
1898 * potentially push the tail if we were holding up the log.
1899 */
1900STATIC int
1901xfs_lock_dir_and_entry(
1902 xfs_inode_t *dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 xfs_inode_t *ip) /* inode of entry 'name' */
1904{
1905 int attempts;
1906 xfs_ino_t e_inum;
1907 xfs_inode_t *ips[2];
1908 xfs_log_item_t *lp;
1909
1910#ifdef DEBUG
1911 xfs_rm_locks++;
1912#endif
1913 attempts = 0;
1914
1915again:
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001916 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 e_inum = ip->i_ino;
1919
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001920 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /*
1923 * We want to lock in increasing inum. Since we've already
1924 * acquired the lock on the directory, we may need to release
1925 * if if the inum of the entry turns out to be less.
1926 */
1927 if (e_inum > dp->i_ino) {
1928 /*
1929 * We are already in the right order, so just
1930 * lock on the inode of the entry.
1931 * We need to use nowait if dp is in the AIL.
1932 */
1933
1934 lp = (xfs_log_item_t *)dp->i_itemp;
1935 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1936 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
1937 attempts++;
1938#ifdef DEBUG
1939 xfs_rm_attempts++;
1940#endif
1941
1942 /*
1943 * Unlock dp and try again.
1944 * xfs_iunlock will try to push the tail
1945 * if the inode is in the AIL.
1946 */
1947
1948 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1949
1950 if ((attempts % 5) == 0) {
1951 delay(1); /* Don't just spin the CPU */
1952#ifdef DEBUG
1953 xfs_rm_lock_delays++;
1954#endif
1955 }
1956 goto again;
1957 }
1958 } else {
1959 xfs_ilock(ip, XFS_ILOCK_EXCL);
1960 }
1961 } else if (e_inum < dp->i_ino) {
1962 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1963
1964 ips[0] = ip;
1965 ips[1] = dp;
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10001966 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 /* else e_inum == dp->i_ino */
1969 /* This can happen if we're asked to lock /x/..
1970 * the entry is "..", which is also the parent directory.
1971 */
1972
1973 return 0;
1974}
1975
1976#ifdef DEBUG
1977int xfs_locked_n;
1978int xfs_small_retries;
1979int xfs_middle_retries;
1980int xfs_lots_retries;
1981int xfs_lock_delays;
1982#endif
1983
1984/*
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001985 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1986 * a different value
1987 */
1988static inline int
1989xfs_lock_inumorder(int lock_mode, int subclass)
1990{
1991 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001992 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001993 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
David Chinner0f1145c2007-06-29 17:26:09 +10001994 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10001995
1996 return lock_mode;
1997}
1998
1999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * The following routine will lock n inodes in exclusive mode.
2001 * We assume the caller calls us with the inodes in i_ino order.
2002 *
2003 * We need to detect deadlock where an inode that we lock
2004 * is in the AIL and we start waiting for another inode that is locked
2005 * by a thread in a long running transaction (such as truncate). This can
2006 * result in deadlock since the long running trans might need to wait
2007 * for the inode we just locked in order to push the tail and free space
2008 * in the log.
2009 */
2010void
2011xfs_lock_inodes(
2012 xfs_inode_t **ips,
2013 int inodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 uint lock_mode)
2015{
2016 int attempts = 0, i, j, try_lock;
2017 xfs_log_item_t *lp;
2018
2019 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2020
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10002021 try_lock = 0;
2022 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024again:
2025 for (; i < inodes; i++) {
2026 ASSERT(ips[i]);
2027
2028 if (i && (ips[i] == ips[i-1])) /* Already locked */
2029 continue;
2030
2031 /*
2032 * If try_lock is not set yet, make sure all locked inodes
2033 * are not in the AIL.
2034 * If any are, set try_lock to be used later.
2035 */
2036
2037 if (!try_lock) {
2038 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2039 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2040 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2041 try_lock++;
2042 }
2043 }
2044 }
2045
2046 /*
2047 * If any of the previous locks we have locked is in the AIL,
2048 * we must TRY to get the second and subsequent locks. If
2049 * we can't get any, we must release all we have
2050 * and try again.
2051 */
2052
2053 if (try_lock) {
2054 /* try_lock must be 0 if i is 0. */
2055 /*
2056 * try_lock means we have an inode locked
2057 * that is in the AIL.
2058 */
2059 ASSERT(i != 0);
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002060 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 attempts++;
2062
2063 /*
2064 * Unlock all previous guys and try again.
2065 * xfs_iunlock will try to push the tail
2066 * if the inode is in the AIL.
2067 */
2068
2069 for(j = i - 1; j >= 0; j--) {
2070
2071 /*
2072 * Check to see if we've already
2073 * unlocked this one.
2074 * Not the first one going back,
2075 * and the inode ptr is the same.
2076 */
2077 if ((j != (i - 1)) && ips[j] ==
2078 ips[j+1])
2079 continue;
2080
2081 xfs_iunlock(ips[j], lock_mode);
2082 }
2083
2084 if ((attempts % 5) == 0) {
2085 delay(1); /* Don't just spin the CPU */
2086#ifdef DEBUG
2087 xfs_lock_delays++;
2088#endif
2089 }
2090 i = 0;
2091 try_lock = 0;
2092 goto again;
2093 }
2094 } else {
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002095 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 }
2098
2099#ifdef DEBUG
2100 if (attempts) {
2101 if (attempts < 5) xfs_small_retries++;
2102 else if (attempts < 100) xfs_middle_retries++;
2103 else xfs_lots_retries++;
2104 } else {
2105 xfs_locked_n++;
2106 }
2107#endif
2108}
2109
2110#ifdef DEBUG
2111#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2112int remove_which_error_return = 0;
2113#else /* ! DEBUG */
2114#define REMOVE_DEBUG_TRACE(x)
2115#endif /* ! DEBUG */
2116
Christoph Hellwig993386c2007-08-28 16:12:30 +10002117int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118xfs_remove(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002119 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002120 struct xfs_name *name,
2121 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002123 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 xfs_trans_t *tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int error = 0;
2126 xfs_bmap_free_t free_list;
2127 xfs_fsblock_t first_block;
2128 int cancel_flags;
2129 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 int link_zero;
2131 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002133 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (XFS_FORCED_SHUTDOWN(mp))
2136 return XFS_ERROR(EIO);
2137
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002138 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Barry Naujok556b8b12008-04-10 12:22:07 +10002139 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
2140 NULL, DM_RIGHT_NULL, name->name, NULL,
2141 ip->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (error)
2143 return error;
2144 }
2145
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002146 xfs_itrace_entry(ip);
2147 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig82dab942008-04-22 17:34:18 +10002150 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 error = XFS_QM_DQATTACH(mp, ip, 0);
2152 if (error) {
2153 REMOVE_DEBUG_TRACE(__LINE__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 goto std_return;
2155 }
2156
2157 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2158 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2159 /*
2160 * We try to get the real space reservation first,
2161 * allowing for directory btree deletion(s) implying
2162 * possible bmap insert(s). If we can't get the space
2163 * reservation then we use 0 instead, and avoid the bmap
2164 * btree insert(s) in the directory code by, if the bmap
2165 * insert tries to happen, instead trimming the LAST
2166 * block from the directory.
2167 */
2168 resblks = XFS_REMOVE_SPACE_RES(mp);
2169 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2170 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2171 if (error == ENOSPC) {
2172 resblks = 0;
2173 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2174 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2175 }
2176 if (error) {
2177 ASSERT(error != ENOSPC);
2178 REMOVE_DEBUG_TRACE(__LINE__);
2179 xfs_trans_cancel(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return error;
2181 }
2182
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002183 error = xfs_lock_dir_and_entry(dp, ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (error) {
2185 REMOVE_DEBUG_TRACE(__LINE__);
2186 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 goto std_return;
2188 }
2189
2190 /*
2191 * At this point, we've gotten both the directory and the entry
2192 * inodes locked.
2193 */
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002194 IHOLD(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig82dab942008-04-22 17:34:18 +10002196
2197 IHOLD(dp);
2198 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200 /*
2201 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2202 */
2203 XFS_BMAP_INIT(&free_list, &first_block);
Barry Naujok556b8b12008-04-10 12:22:07 +10002204 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
Christoph Hellwigd4377d82008-04-22 17:33:46 +10002205 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (error) {
2207 ASSERT(error != ENOENT);
2208 REMOVE_DEBUG_TRACE(__LINE__);
2209 goto error1;
2210 }
2211 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2212
2213 dp->i_gen++;
2214 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2215
2216 error = xfs_droplink(tp, ip);
2217 if (error) {
2218 REMOVE_DEBUG_TRACE(__LINE__);
2219 goto error1;
2220 }
2221
2222 /* Determine if this is the last link while
2223 * we are in the transaction.
2224 */
2225 link_zero = (ip)->i_d.di_nlink==0;
2226
2227 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 * If this is a synchronous mount, make sure that the
2229 * remove transaction goes to disk before returning to
2230 * the user.
2231 */
2232 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2233 xfs_trans_set_sync(tp);
2234 }
2235
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002236 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (error) {
2238 REMOVE_DEBUG_TRACE(__LINE__);
2239 goto error_rele;
2240 }
2241
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002242 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 /*
David Chinner2a82b8b2007-07-11 11:09:12 +10002247 * If we are using filestreams, kill the stream association.
2248 * If the file is still open it may get a new one but that
2249 * will get killed on last close in xfs_close() so we don't
2250 * have to worry about that.
2251 */
2252 if (link_zero && xfs_inode_is_filestream(ip))
2253 xfs_filestream_deassociate(ip);
2254
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002255 xfs_itrace_exit(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257/* Fall through to std_return with error = 0 */
2258 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002259 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002261 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002263 name->name, NULL, ip->i_d.di_mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 return error;
2266
2267 error1:
2268 xfs_bmap_cancel(&free_list);
2269 cancel_flags |= XFS_TRANS_ABORT;
2270 xfs_trans_cancel(tp, cancel_flags);
2271 goto std_return;
2272
2273 error_rele:
2274 /*
2275 * In this case make sure to not release the inode until after
2276 * the current transaction is aborted. Releasing it beforehand
2277 * can cause us to go to xfs_inactive and start a recursive
2278 * transaction which can easily deadlock with the current one.
2279 */
2280 xfs_bmap_cancel(&free_list);
2281 cancel_flags |= XFS_TRANS_ABORT;
2282 xfs_trans_cancel(tp, cancel_flags);
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto std_return;
2285}
2286
Christoph Hellwig993386c2007-08-28 16:12:30 +10002287int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288xfs_link(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002289 xfs_inode_t *tdp,
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002290 xfs_inode_t *sip,
Barry Naujok556b8b12008-04-10 12:22:07 +10002291 struct xfs_name *target_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002293 xfs_mount_t *mp = tdp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 xfs_inode_t *ips[2];
2296 int error;
2297 xfs_bmap_free_t free_list;
2298 xfs_fsblock_t first_block;
2299 int cancel_flags;
2300 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 int resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002303 xfs_itrace_entry(tdp);
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002304 xfs_itrace_entry(sip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002306 ASSERT(!S_ISDIR(sip->i_d.di_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (XFS_FORCED_SHUTDOWN(mp))
2309 return XFS_ERROR(EIO);
2310
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002311 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002313 tdp, DM_RIGHT_NULL,
2314 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002315 target_name->name, NULL, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (error)
2317 return error;
2318 }
2319
2320 /* Return through std_return after this point. */
2321
2322 error = XFS_QM_DQATTACH(mp, sip, 0);
2323 if (!error && sip != tdp)
2324 error = XFS_QM_DQATTACH(mp, tdp, 0);
2325 if (error)
2326 goto std_return;
2327
2328 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2329 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002330 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2332 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2333 if (error == ENOSPC) {
2334 resblks = 0;
2335 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2336 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2337 }
2338 if (error) {
2339 cancel_flags = 0;
2340 goto error_return;
2341 }
2342
2343 if (sip->i_ino < tdp->i_ino) {
2344 ips[0] = sip;
2345 ips[1] = tdp;
2346 } else {
2347 ips[0] = tdp;
2348 ips[1] = sip;
2349 }
2350
Christoph Hellwigcfa853e2008-04-22 17:34:06 +10002351 xfs_lock_inodes(ips, 2, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 /*
2354 * Increment vnode ref counts since xfs_trans_commit &
2355 * xfs_trans_cancel will both unlock the inodes and
2356 * decrement the associated ref counts.
2357 */
Christoph Hellwiga3da7892008-03-06 13:46:12 +11002358 IHOLD(sip);
2359 IHOLD(tdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2361 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2362
2363 /*
2364 * If the source has too many links, we can't make any more to it.
2365 */
2366 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2367 error = XFS_ERROR(EMLINK);
2368 goto error_return;
2369 }
2370
Nathan Scott365ca832005-06-21 15:39:12 +10002371 /*
2372 * If we are using project inheritance, we only allow hard link
2373 * creation in our tree when the project IDs are the same; else
2374 * the tree quota mechanism could be circumvented.
2375 */
2376 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2377 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
Nathan Scottb1ecdda2006-05-08 19:51:42 +10002378 error = XFS_ERROR(EXDEV);
Nathan Scott365ca832005-06-21 15:39:12 +10002379 goto error_return;
2380 }
2381
Barry Naujok556b8b12008-04-10 12:22:07 +10002382 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2383 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 goto error_return;
2385
2386 XFS_BMAP_INIT(&free_list, &first_block);
2387
Barry Naujok556b8b12008-04-10 12:22:07 +10002388 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2389 &first_block, &free_list, resblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (error)
2391 goto abort_return;
2392 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2393 tdp->i_gen++;
2394 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2395
2396 error = xfs_bumplink(tp, sip);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002397 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto abort_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 /*
2401 * If this is a synchronous mount, make sure that the
2402 * link transaction goes to disk before returning to
2403 * the user.
2404 */
2405 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2406 xfs_trans_set_sync(tp);
2407 }
2408
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002409 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 if (error) {
2411 xfs_bmap_cancel(&free_list);
2412 goto abort_return;
2413 }
2414
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002415 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002416 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto std_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 /* Fall through to std_return with error = 0. */
2420std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002421 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002423 tdp, DM_RIGHT_NULL,
2424 sip, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002425 target_name->name, NULL, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 return error;
2428
2429 abort_return:
2430 cancel_flags |= XFS_TRANS_ABORT;
2431 /* FALLTHROUGH */
Jesper Juhl014c2542006-01-15 02:37:08 +01002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 error_return:
2434 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 goto std_return;
2436}
Alexey Dobriyanb71d3002006-06-27 12:45:17 +10002437
2438
Christoph Hellwig993386c2007-08-28 16:12:30 +10002439int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440xfs_mkdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002441 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002442 struct xfs_name *dir_name,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002443 mode_t mode,
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002444 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 cred_t *credp)
2446{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002447 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 xfs_inode_t *cdp; /* inode of created dir */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 int cancel_flags;
2451 int error;
2452 int committed;
2453 xfs_bmap_free_t free_list;
2454 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002455 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 boolean_t created = B_FALSE;
2457 int dm_event_sent = 0;
2458 xfs_prid_t prid;
2459 struct xfs_dquot *udqp, *gdqp;
2460 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 if (XFS_FORCED_SHUTDOWN(mp))
2463 return XFS_ERROR(EIO);
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 tp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002467 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002469 dp, DM_RIGHT_NULL, NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002470 DM_RIGHT_NULL, dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002471 mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (error)
2473 return error;
2474 dm_event_sent = 1;
2475 }
2476
2477 /* Return through std_return after this point. */
2478
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002479 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 mp = dp->i_mount;
2482 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002483 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2484 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 else
2486 prid = (xfs_prid_t)dfltprid;
2487
2488 /*
2489 * Make sure that we have allocated dquot(s) on disk.
2490 */
2491 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002492 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2494 if (error)
2495 goto std_return;
2496
2497 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2498 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Barry Naujok556b8b12008-04-10 12:22:07 +10002499 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2501 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2502 if (error == ENOSPC) {
2503 resblks = 0;
2504 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2505 XFS_TRANS_PERM_LOG_RES,
2506 XFS_MKDIR_LOG_COUNT);
2507 }
2508 if (error) {
2509 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 goto error_return;
2511 }
2512
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002513 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002514 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /*
2517 * Check for directory link count overflow.
2518 */
2519 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2520 error = XFS_ERROR(EMLINK);
2521 goto error_return;
2522 }
2523
2524 /*
2525 * Reserve disk quota and the inode.
2526 */
2527 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2528 if (error)
2529 goto error_return;
2530
Barry Naujok556b8b12008-04-10 12:22:07 +10002531 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2532 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto error_return;
2534 /*
2535 * create the directory inode.
2536 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002537 error = xfs_dir_ialloc(&tp, dp, mode, 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 0, credp, prid, resblks > 0,
2539 &cdp, NULL);
2540 if (error) {
2541 if (error == ENOSPC)
2542 goto error_return;
2543 goto abort_return;
2544 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002545 xfs_itrace_ref(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
2547 /*
2548 * Now we add the directory inode to the transaction.
2549 * We waited until now since xfs_dir_ialloc might start
2550 * a new transaction. Had we joined the transaction
Christoph Hellwig993386c2007-08-28 16:12:30 +10002551 * earlier, the locks might have gotten released. An error
2552 * from here on will result in the transaction cancel
2553 * unlocking dp so don't do it explicitly in the error path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002555 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002557 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 XFS_BMAP_INIT(&free_list, &first_block);
2560
Barry Naujok556b8b12008-04-10 12:22:07 +10002561 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2562 &first_block, &free_list, resblks ?
2563 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (error) {
2565 ASSERT(error != ENOSPC);
2566 goto error1;
2567 }
2568 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2569
2570 /*
2571 * Bump the in memory version number of the parent directory
2572 * so that other processes accessing it will recognize that
2573 * the directory has changed.
2574 */
2575 dp->i_gen++;
2576
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002577 error = xfs_dir_init(tp, cdp, dp);
2578 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 cdp->i_gen = 1;
2582 error = xfs_bumplink(tp, dp);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002583 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 created = B_TRUE;
2587
Christoph Hellwig979ebab2008-03-06 13:46:05 +11002588 *ipp = cdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 IHOLD(cdp);
2590
2591 /*
2592 * Attach the dquots to the new inode and modify the icount incore.
2593 */
2594 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2595
2596 /*
2597 * If this is a synchronous mount, make sure that the
2598 * mkdir transaction goes to disk before returning to
2599 * the user.
2600 */
2601 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2602 xfs_trans_set_sync(tp);
2603 }
2604
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002605 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (error) {
2607 IRELE(cdp);
2608 goto error2;
2609 }
2610
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002611 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 XFS_QM_DQRELE(mp, udqp);
2613 XFS_QM_DQRELE(mp, gdqp);
2614 if (error) {
2615 IRELE(cdp);
2616 }
2617
2618 /* Fall through to std_return with error = 0 or errno from
2619 * xfs_trans_commit. */
2620
2621std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002622 if ((created || (error != 0 && dm_event_sent != 0)) &&
Christoph Hellwig993386c2007-08-28 16:12:30 +10002623 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002625 dp, DM_RIGHT_NULL,
2626 created ? cdp : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002628 dir_name->name, NULL,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002629 mode, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631 return error;
2632
2633 error2:
2634 error1:
2635 xfs_bmap_cancel(&free_list);
2636 abort_return:
2637 cancel_flags |= XFS_TRANS_ABORT;
2638 error_return:
2639 xfs_trans_cancel(tp, cancel_flags);
2640 XFS_QM_DQRELE(mp, udqp);
2641 XFS_QM_DQRELE(mp, gdqp);
2642
Christoph Hellwig993386c2007-08-28 16:12:30 +10002643 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 goto std_return;
2647}
2648
Christoph Hellwig993386c2007-08-28 16:12:30 +10002649int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650xfs_rmdir(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002651 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002652 struct xfs_name *name,
2653 xfs_inode_t *cdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002655 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int error;
2658 xfs_bmap_free_t free_list;
2659 xfs_fsblock_t first_block;
2660 int cancel_flags;
2661 int committed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 int last_cdp_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 uint resblks;
2664
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002665 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Christoph Hellwig993386c2007-08-28 16:12:30 +10002667 if (XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 return XFS_ERROR(EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002670 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002672 dp, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002673 NULL, DM_RIGHT_NULL, name->name,
2674 NULL, cdp->i_d.di_mode, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (error)
2676 return XFS_ERROR(error);
2677 }
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 * Get the dquots for the inodes.
2681 */
2682 error = XFS_QM_DQATTACH(mp, dp, 0);
Christoph Hellwig82dab942008-04-22 17:34:18 +10002683 if (!error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 error = XFS_QM_DQATTACH(mp, cdp, 0);
2685 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 REMOVE_DEBUG_TRACE(__LINE__);
2687 goto std_return;
2688 }
2689
2690 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2691 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2692 /*
2693 * We try to get the real space reservation first,
2694 * allowing for directory btree deletion(s) implying
2695 * possible bmap insert(s). If we can't get the space
2696 * reservation then we use 0 instead, and avoid the bmap
2697 * btree insert(s) in the directory code by, if the bmap
2698 * insert tries to happen, instead trimming the LAST
2699 * block from the directory.
2700 */
2701 resblks = XFS_REMOVE_SPACE_RES(mp);
2702 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2703 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2704 if (error == ENOSPC) {
2705 resblks = 0;
2706 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2707 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2708 }
2709 if (error) {
2710 ASSERT(error != ENOSPC);
2711 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 goto error_return;
2713 }
2714 XFS_BMAP_INIT(&free_list, &first_block);
2715
2716 /*
2717 * Now lock the child directory inode and the parent directory
2718 * inode in the proper order. This will take care of validating
2719 * that the directory entry for the child directory inode has
2720 * not changed while we were obtaining a log reservation.
2721 */
Eric Sandeene9ed9d22007-05-08 13:48:56 +10002722 error = xfs_lock_dir_and_entry(dp, cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (error) {
2724 xfs_trans_cancel(tp, cancel_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 goto std_return;
2726 }
2727
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002728 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Christoph Hellwig5df78e72008-04-22 17:34:24 +10002731 IHOLD(cdp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2733
2734 ASSERT(cdp->i_d.di_nlink >= 2);
2735 if (cdp->i_d.di_nlink != 2) {
2736 error = XFS_ERROR(ENOTEMPTY);
2737 goto error_return;
2738 }
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002739 if (!xfs_dir_isempty(cdp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 error = XFS_ERROR(ENOTEMPTY);
2741 goto error_return;
2742 }
2743
Barry Naujok556b8b12008-04-10 12:22:07 +10002744 error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10002745 &first_block, &free_list, resblks);
2746 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2750
2751 /*
2752 * Bump the in memory generation count on the parent
2753 * directory so that other can know that it has changed.
2754 */
2755 dp->i_gen++;
2756
2757 /*
2758 * Drop the link from cdp's "..".
2759 */
2760 error = xfs_droplink(tp, dp);
2761 if (error) {
2762 goto error1;
2763 }
2764
2765 /*
2766 * Drop the link from dp to cdp.
2767 */
2768 error = xfs_droplink(tp, cdp);
2769 if (error) {
2770 goto error1;
2771 }
2772
2773 /*
2774 * Drop the "." link from cdp to self.
2775 */
2776 error = xfs_droplink(tp, cdp);
2777 if (error) {
2778 goto error1;
2779 }
2780
2781 /* Determine these before committing transaction */
2782 last_cdp_link = (cdp)->i_d.di_nlink==0;
2783
2784 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 * If this is a synchronous mount, make sure that the
2786 * rmdir transaction goes to disk before returning to
2787 * the user.
2788 */
2789 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2790 xfs_trans_set_sync(tp);
2791 }
2792
Eric Sandeenf7c99b62007-02-10 18:37:16 +11002793 error = xfs_bmap_finish (&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (error) {
2795 xfs_bmap_cancel(&free_list);
2796 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
2797 XFS_TRANS_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 goto std_return;
2799 }
2800
Eric Sandeen1c72bf92007-05-08 13:48:42 +10002801 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 goto std_return;
2804 }
2805
2806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /* Fall through to std_return with error = 0 or the errno
2808 * from xfs_trans_commit. */
Nathan Scottbb19fba2006-03-22 14:12:12 +11002809 std_return:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002810 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002812 dp, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002814 name->name, NULL, cdp->i_d.di_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 error, 0);
2816 }
2817 return error;
2818
Nathan Scottbb19fba2006-03-22 14:12:12 +11002819 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 xfs_bmap_cancel(&free_list);
2821 cancel_flags |= XFS_TRANS_ABORT;
Jesper Juhl014c2542006-01-15 02:37:08 +01002822 /* FALLTHROUGH */
2823
Nathan Scottbb19fba2006-03-22 14:12:12 +11002824 error_return:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 xfs_trans_cancel(tp, cancel_flags);
2826 goto std_return;
2827}
2828
Christoph Hellwig993386c2007-08-28 16:12:30 +10002829int
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830xfs_symlink(
Christoph Hellwig993386c2007-08-28 16:12:30 +10002831 xfs_inode_t *dp,
Barry Naujok556b8b12008-04-10 12:22:07 +10002832 struct xfs_name *link_name,
2833 const char *target_path,
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002834 mode_t mode,
Christoph Hellwig3937be52008-03-06 13:46:19 +11002835 xfs_inode_t **ipp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 cred_t *credp)
2837{
Christoph Hellwig993386c2007-08-28 16:12:30 +10002838 xfs_mount_t *mp = dp->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 xfs_inode_t *ip;
2841 int error;
2842 int pathlen;
2843 xfs_bmap_free_t free_list;
2844 xfs_fsblock_t first_block;
Christoph Hellwig993386c2007-08-28 16:12:30 +10002845 boolean_t unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 uint cancel_flags;
2847 int committed;
2848 xfs_fileoff_t first_fsb;
2849 xfs_filblks_t fs_blocks;
2850 int nmaps;
2851 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2852 xfs_daddr_t d;
Barry Naujok556b8b12008-04-10 12:22:07 +10002853 const char *cur_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int byte_cnt;
2855 int n;
2856 xfs_buf_t *bp;
2857 xfs_prid_t prid;
2858 struct xfs_dquot *udqp, *gdqp;
2859 uint resblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Christoph Hellwig3937be52008-03-06 13:46:19 +11002861 *ipp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 error = 0;
2863 ip = NULL;
2864 tp = NULL;
2865
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002866 xfs_itrace_entry(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (XFS_FORCED_SHUTDOWN(mp))
2869 return XFS_ERROR(EIO);
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 /*
2872 * Check component lengths of the target path name.
2873 */
2874 pathlen = strlen(target_path);
2875 if (pathlen >= MAXPATHLEN) /* total string too long */
2876 return XFS_ERROR(ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Christoph Hellwigeb9df392007-08-16 18:42:07 +10002878 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11002879 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
Barry Naujok556b8b12008-04-10 12:22:07 +10002881 link_name->name, target_path, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (error)
2883 return error;
2884 }
2885
2886 /* Return through std_return after this point. */
2887
2888 udqp = gdqp = NULL;
Nathan Scott365ca832005-06-21 15:39:12 +10002889 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2890 prid = dp->i_d.di_projid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 else
2892 prid = (xfs_prid_t)dfltprid;
2893
2894 /*
2895 * Make sure that we have allocated dquot(s) on disk.
2896 */
2897 error = XFS_QM_DQVOPALLOC(mp, dp,
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002898 current_fsuid(credp), current_fsgid(credp), prid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2900 if (error)
2901 goto std_return;
2902
2903 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2904 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2905 /*
2906 * The symlink will fit into the inode data fork?
2907 * There can't be any attributes so we get the whole variable part.
2908 */
2909 if (pathlen <= XFS_LITINO(mp))
2910 fs_blocks = 0;
2911 else
2912 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
Barry Naujok556b8b12008-04-10 12:22:07 +10002913 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2915 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2916 if (error == ENOSPC && fs_blocks == 0) {
2917 resblks = 0;
2918 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2919 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2920 }
2921 if (error) {
2922 cancel_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 goto error_return;
2924 }
2925
Lachlan McIlroyf7c66ce2007-05-08 13:50:19 +10002926 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002927 unlock_dp_on_error = B_TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 /*
2930 * Check whether the directory allows new symlinks or not.
2931 */
2932 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2933 error = XFS_ERROR(EPERM);
2934 goto error_return;
2935 }
2936
2937 /*
2938 * Reserve disk quota : blocks and inode.
2939 */
2940 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2941 if (error)
2942 goto error_return;
2943
2944 /*
2945 * Check for ability to enter directory entry, if no space reserved.
2946 */
Barry Naujok556b8b12008-04-10 12:22:07 +10002947 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto error_return;
2950 /*
2951 * Initialize the bmap freelist prior to calling either
2952 * bmapi or the directory create code.
2953 */
2954 XFS_BMAP_INIT(&free_list, &first_block);
2955
2956 /*
2957 * Allocate an inode for the symlink.
2958 */
Christoph Hellwig3e5daf02007-10-11 18:09:12 +10002959 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 1, 0, credp, prid, resblks > 0, &ip, NULL);
2961 if (error) {
2962 if (error == ENOSPC)
2963 goto error_return;
2964 goto error1;
2965 }
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11002966 xfs_itrace_ref(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Christoph Hellwig993386c2007-08-28 16:12:30 +10002968 /*
2969 * An error after we've joined dp to the transaction will result in the
2970 * transaction cancel unlocking dp so don't do it explicitly in the
2971 * error path.
2972 */
Christoph Hellwig3937be52008-03-06 13:46:19 +11002973 IHOLD(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
Christoph Hellwig993386c2007-08-28 16:12:30 +10002975 unlock_dp_on_error = B_FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 /*
2978 * Also attach the dquot(s) to it, if applicable.
2979 */
2980 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2981
2982 if (resblks)
2983 resblks -= XFS_IALLOC_SPACE_RES(mp);
2984 /*
2985 * If the symlink will fit into the inode, write it inline.
2986 */
2987 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2988 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2989 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2990 ip->i_d.di_size = pathlen;
2991
2992 /*
2993 * The inode was initially created in extent format.
2994 */
2995 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2996 ip->i_df.if_flags |= XFS_IFINLINE;
2997
2998 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2999 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3000
3001 } else {
3002 first_fsb = 0;
3003 nmaps = SYMLINK_MAPS;
3004
3005 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3006 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3007 &first_block, resblks, mval, &nmaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003008 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 if (error) {
3010 goto error1;
3011 }
3012
3013 if (resblks)
3014 resblks -= fs_blocks;
3015 ip->i_d.di_size = pathlen;
3016 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3017
3018 cur_chunk = target_path;
3019 for (n = 0; n < nmaps; n++) {
3020 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3021 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3022 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3023 BTOBB(byte_cnt), 0);
3024 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3025 if (pathlen < byte_cnt) {
3026 byte_cnt = pathlen;
3027 }
3028 pathlen -= byte_cnt;
3029
3030 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3031 cur_chunk += byte_cnt;
3032
3033 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3034 }
3035 }
3036
3037 /*
3038 * Create the directory entry for the symlink.
3039 */
Barry Naujok556b8b12008-04-10 12:22:07 +10003040 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
3041 &first_block, &free_list, resblks);
Nathan Scottf6c2d1f2006-06-20 13:04:51 +10003042 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 goto error1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3045 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3046
3047 /*
3048 * Bump the in memory version number of the parent directory
3049 * so that other processes accessing it will recognize that
3050 * the directory has changed.
3051 */
3052 dp->i_gen++;
3053
3054 /*
3055 * If this is a synchronous mount, make sure that the
3056 * symlink transaction goes to disk before returning to
3057 * the user.
3058 */
3059 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3060 xfs_trans_set_sync(tp);
3061 }
3062
3063 /*
3064 * xfs_trans_commit normally decrements the vnode ref count
3065 * when it unlocks the inode. Since we want to return the
3066 * vnode to the caller, we bump the vnode ref count now.
3067 */
3068 IHOLD(ip);
3069
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003070 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (error) {
3072 goto error2;
3073 }
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003074 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 XFS_QM_DQRELE(mp, udqp);
3076 XFS_QM_DQRELE(mp, gdqp);
3077
3078 /* Fall through to std_return with error = 0 or errno from
3079 * xfs_trans_commit */
3080std_return:
Christoph Hellwig993386c2007-08-28 16:12:30 +10003081 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003083 dp, DM_RIGHT_NULL,
3084 error ? NULL : ip,
Barry Naujok556b8b12008-04-10 12:22:07 +10003085 DM_RIGHT_NULL, link_name->name,
3086 target_path, 0, error, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
3088
Christoph Hellwig3937be52008-03-06 13:46:19 +11003089 if (!error)
3090 *ipp = ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return error;
3092
3093 error2:
3094 IRELE(ip);
3095 error1:
3096 xfs_bmap_cancel(&free_list);
3097 cancel_flags |= XFS_TRANS_ABORT;
3098 error_return:
3099 xfs_trans_cancel(tp, cancel_flags);
3100 XFS_QM_DQRELE(mp, udqp);
3101 XFS_QM_DQRELE(mp, gdqp);
3102
Christoph Hellwig993386c2007-08-28 16:12:30 +10003103 if (unlock_dp_on_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 xfs_iunlock(dp, XFS_ILOCK_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
3106 goto std_return;
3107}
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110xfs_inode_flush(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003111 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 int flags)
3113{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003114 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 int error = 0;
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (XFS_FORCED_SHUTDOWN(mp))
3118 return XFS_ERROR(EIO);
3119
3120 /*
3121 * Bypass inodes which have already been cleaned by
3122 * the inode flush clustering code inside xfs_iflush
3123 */
David Chinner33540402008-03-06 13:43:59 +11003124 if (xfs_inode_clean(ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 return 0;
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 /*
3128 * We make this non-blocking if the inode is contended,
3129 * return EAGAIN to indicate to the caller that they
3130 * did not succeed. This prevents the flush path from
3131 * blocking on inodes inside another operation right
3132 * now, they get caught later by xfs_sync.
3133 */
David Chinnera3f74ff2008-03-06 13:43:42 +11003134 if (flags & FLUSH_SYNC) {
3135 xfs_ilock(ip, XFS_ILOCK_SHARED);
3136 xfs_iflock(ip);
3137 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3138 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3139 xfs_iunlock(ip, XFS_ILOCK_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return EAGAIN;
3141 }
David Chinnera3f74ff2008-03-06 13:43:42 +11003142 } else {
3143 return EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145
David Chinnera3f74ff2008-03-06 13:43:42 +11003146 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3147 : XFS_IFLUSH_ASYNC_NOBLOCK);
3148 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return error;
3151}
3152
Christoph Hellwig993386c2007-08-28 16:12:30 +10003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154int
Christoph Hellwig993386c2007-08-28 16:12:30 +10003155xfs_set_dmattrs(
3156 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 u_int evmask,
Christoph Hellwig993386c2007-08-28 16:12:30 +10003158 u_int16_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003160 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 xfs_trans_t *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 int error;
3163
3164 if (!capable(CAP_SYS_ADMIN))
3165 return XFS_ERROR(EPERM);
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (XFS_FORCED_SHUTDOWN(mp))
3168 return XFS_ERROR(EIO);
3169
3170 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3171 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3172 if (error) {
3173 xfs_trans_cancel(tp, 0);
3174 return error;
3175 }
3176 xfs_ilock(ip, XFS_ILOCK_EXCL);
3177 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3178
Christoph Hellwig613d7042007-10-11 17:44:08 +10003179 ip->i_d.di_dmevmask = evmask;
3180 ip->i_d.di_dmstate = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3183 IHOLD(ip);
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003184 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
3186 return error;
3187}
3188
Christoph Hellwig993386c2007-08-28 16:12:30 +10003189int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190xfs_reclaim(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003191 xfs_inode_t *ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003193 bhv_vnode_t *vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003195 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 ASSERT(!VN_MAPPED(vp));
3198
3199 /* bad inode, get out here ASAP */
3200 if (VN_BAD(vp)) {
3201 xfs_ireclaim(ip);
3202 return 0;
3203 }
3204
Christoph Hellwigb677c212007-08-29 11:46:28 +10003205 vn_iowait(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Christoph Hellwig51c91ed2005-09-02 16:58:38 +10003207 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Christoph Hellwig42fe2b12006-01-11 15:35:17 +11003209 /*
3210 * Make sure the atime in the XFS inode is correct before freeing the
3211 * Linux inode.
3212 */
3213 xfs_synchronize_atime(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
David Chinner4c606582006-11-11 18:05:00 +11003215 /*
3216 * If we have nothing to flush with this inode then complete the
3217 * teardown now, otherwise break the link between the xfs inode and the
3218 * linux inode and clean up the xfs inode later. This avoids flushing
3219 * the inode to disk during the delete operation itself.
3220 *
3221 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3222 * first to ensure that xfs_iunpin() will never see an xfs inode
3223 * that has a linux inode being reclaimed. Synchronisation is provided
3224 * by the i_flags_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 */
3226 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3227 xfs_ilock(ip, XFS_ILOCK_EXCL);
3228 xfs_iflock(ip);
3229 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3230 } else {
3231 xfs_mount_t *mp = ip->i_mount;
3232
David Chinner4c606582006-11-11 18:05:00 +11003233 /* Protect sync and unpin from us */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 XFS_MOUNT_ILOCK(mp);
David Chinner4c606582006-11-11 18:05:00 +11003235 spin_lock(&ip->i_flags_lock);
3236 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10003237 vn_to_inode(vp)->i_private = NULL;
3238 ip->i_vnode = NULL;
David Chinner4c606582006-11-11 18:05:00 +11003239 spin_unlock(&ip->i_flags_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 XFS_MOUNT_IUNLOCK(mp);
3242 }
3243 return 0;
3244}
3245
3246int
3247xfs_finish_reclaim(
3248 xfs_inode_t *ip,
3249 int locked,
3250 int sync_mode)
3251{
David Chinnerda353b02007-08-28 14:00:13 +10003252 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
Nathan Scott67fcaa72006-06-09 17:00:52 +10003253 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 int error;
3255
3256 if (vp && VN_BAD(vp))
3257 goto reclaim;
3258
3259 /* The hash lock here protects a thread in xfs_iget_core from
3260 * racing with us on linking the inode back with a vnode.
3261 * Once we have the XFS_IRECLAIM flag set it will not touch
3262 * us.
3263 */
David Chinnerda353b02007-08-28 14:00:13 +10003264 write_lock(&pag->pag_ici_lock);
David Chinnerf273ab82006-09-28 11:06:03 +10003265 spin_lock(&ip->i_flags_lock);
David Chinner7a18c382006-11-11 18:04:54 +11003266 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3267 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
David Chinnerf273ab82006-09-28 11:06:03 +10003268 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003269 write_unlock(&pag->pag_ici_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (locked) {
3271 xfs_ifunlock(ip);
3272 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3273 }
Jesper Juhl014c2542006-01-15 02:37:08 +01003274 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
David Chinner7a18c382006-11-11 18:04:54 +11003276 __xfs_iflags_set(ip, XFS_IRECLAIM);
David Chinnerf273ab82006-09-28 11:06:03 +10003277 spin_unlock(&ip->i_flags_lock);
David Chinnerda353b02007-08-28 14:00:13 +10003278 write_unlock(&pag->pag_ici_lock);
3279 xfs_put_perag(ip->i_mount, pag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 /*
3282 * If the inode is still dirty, then flush it out. If the inode
3283 * is not in the AIL, then it will be OK to flush it delwri as
3284 * long as xfs_iflush() does not keep any references to the inode.
3285 * We leave that decision up to xfs_iflush() since it has the
3286 * knowledge of whether it's OK to simply do a delwri flush of
3287 * the inode or whether we need to wait until the inode is
3288 * pulled from the AIL.
3289 * We get the flush lock regardless, though, just to make sure
3290 * we don't free it while it is being flushed.
3291 */
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003292 if (!locked) {
3293 xfs_ilock(ip, XFS_ILOCK_EXCL);
3294 xfs_iflock(ip);
3295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003297 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (ip->i_update_core ||
3299 ((ip->i_itemp != NULL) &&
3300 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3301 error = xfs_iflush(ip, sync_mode);
3302 /*
3303 * If we hit an error, typically because of filesystem
3304 * shutdown, we don't need to let vn_reclaim to know
3305 * because we're gonna reclaim the inode anyway.
3306 */
3307 if (error) {
3308 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3309 goto reclaim;
3310 }
3311 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3312 }
3313
3314 ASSERT(ip->i_update_core == 0);
3315 ASSERT(ip->i_itemp == NULL ||
3316 ip->i_itemp->ili_format.ilf_fields == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
3318
Lachlan McIlroy461aa8a2008-03-06 13:43:11 +11003319 xfs_ifunlock(ip);
3320 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3321
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 reclaim:
3323 xfs_ireclaim(ip);
3324 return 0;
3325}
3326
3327int
3328xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3329{
3330 int purged;
3331 xfs_inode_t *ip, *n;
3332 int done = 0;
3333
3334 while (!done) {
3335 purged = 0;
3336 XFS_MOUNT_ILOCK(mp);
3337 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3338 if (noblock) {
3339 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3340 continue;
3341 if (xfs_ipincount(ip) ||
3342 !xfs_iflock_nowait(ip)) {
3343 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3344 continue;
3345 }
3346 }
3347 XFS_MOUNT_IUNLOCK(mp);
Felix Blyakher6b2cf612005-11-25 16:42:13 +11003348 if (xfs_finish_reclaim(ip, noblock,
3349 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3350 delay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 purged = 1;
3352 break;
3353 }
3354
3355 done = !purged;
3356 }
3357
3358 XFS_MOUNT_IUNLOCK(mp);
3359 return 0;
3360}
3361
3362/*
3363 * xfs_alloc_file_space()
3364 * This routine allocates disk space for the given file.
3365 *
3366 * If alloc_type == 0, this request is for an ALLOCSP type
3367 * request which will change the file size. In this case, no
3368 * DMAPI event will be generated by the call. A TRUNCATE event
3369 * will be generated later by xfs_setattr.
3370 *
3371 * If alloc_type != 0, this request is for a RESVSP type
3372 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3373 * lower block boundary byte address is less than the file's
3374 * length.
3375 *
3376 * RETURNS:
3377 * 0 on success
3378 * errno on error
3379 *
3380 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +10003381STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382xfs_alloc_file_space(
3383 xfs_inode_t *ip,
3384 xfs_off_t offset,
3385 xfs_off_t len,
3386 int alloc_type,
3387 int attr_flags)
3388{
Nathan Scottdd9f4382006-01-11 15:28:28 +11003389 xfs_mount_t *mp = ip->i_mount;
3390 xfs_off_t count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 xfs_filblks_t allocated_fsb;
3392 xfs_filblks_t allocatesize_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003393 xfs_extlen_t extsz, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 xfs_fileoff_t startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003395 xfs_fsblock_t firstfsb;
3396 int nimaps;
3397 int bmapi_flag;
3398 int quota_flag;
3399 int rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 xfs_trans_t *tp;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003401 xfs_bmbt_irec_t imaps[1], *imapp;
3402 xfs_bmap_free_t free_list;
3403 uint qblocks, resblks, resrtextents;
3404 int committed;
3405 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003407 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 if (XFS_FORCED_SHUTDOWN(mp))
3410 return XFS_ERROR(EIO);
3411
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3413 return error;
3414
3415 if (len <= 0)
3416 return XFS_ERROR(EINVAL);
3417
David Chinner957d0eb2007-06-18 16:50:37 +10003418 rt = XFS_IS_REALTIME_INODE(ip);
3419 extsz = xfs_get_extsz_hint(ip);
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 count = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 imapp = &imaps[0];
Nathan Scottdd9f4382006-01-11 15:28:28 +11003423 nimaps = 1;
3424 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3426 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3427
3428 /* Generate a DMAPI event if needed. */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003429 if (alloc_type != 0 && offset < ip->i_size &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 (attr_flags&ATTR_DMI) == 0 &&
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003431 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 xfs_off_t end_dmi_offset;
3433
3434 end_dmi_offset = offset+len;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003435 if (end_dmi_offset > ip->i_size)
3436 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003437 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3438 end_dmi_offset - offset, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 }
3442
3443 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003444 * Allocate file space until done or until there is an error
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 */
3446retry:
3447 while (allocatesize_fsb && !error) {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003448 xfs_fileoff_t s, e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Nathan Scottdd9f4382006-01-11 15:28:28 +11003450 /*
Nathan Scott3ddb8fa2006-01-11 15:33:02 +11003451 * Determine space reservations for data/realtime.
Nathan Scottdd9f4382006-01-11 15:28:28 +11003452 */
3453 if (unlikely(extsz)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 s = startoffset_fsb;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003455 do_div(s, extsz);
3456 s *= extsz;
3457 e = startoffset_fsb + allocatesize_fsb;
3458 if ((temp = do_mod(startoffset_fsb, extsz)))
3459 e += temp;
3460 if ((temp = do_mod(e, extsz)))
3461 e += extsz - temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 } else {
Nathan Scottdd9f4382006-01-11 15:28:28 +11003463 s = 0;
3464 e = allocatesize_fsb;
3465 }
3466
3467 if (unlikely(rt)) {
3468 resrtextents = qblocks = (uint)(e - s);
3469 resrtextents /= mp->m_sb.sb_rextsize;
3470 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3471 quota_flag = XFS_QMOPT_RES_RTBLKS;
3472 } else {
3473 resrtextents = 0;
3474 resblks = qblocks = \
3475 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3476 quota_flag = XFS_QMOPT_RES_REGBLKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 }
3478
3479 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003480 * Allocate and setup the transaction.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 */
3482 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003483 error = xfs_trans_reserve(tp, resblks,
3484 XFS_WRITE_LOG_RES(mp), resrtextents,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 XFS_TRANS_PERM_LOG_RES,
3486 XFS_WRITE_LOG_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003488 * Check for running out of space
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 */
3490 if (error) {
3491 /*
3492 * Free the transaction structure.
3493 */
3494 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3495 xfs_trans_cancel(tp, 0);
3496 break;
3497 }
3498 xfs_ilock(ip, XFS_ILOCK_EXCL);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003499 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3500 qblocks, 0, quota_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 if (error)
3502 goto error1;
3503
3504 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3505 xfs_trans_ihold(tp, ip);
3506
3507 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003508 * Issue the xfs_bmapi() call to allocate the blocks
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 */
3510 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003511 error = xfs_bmapi(tp, ip, startoffset_fsb,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003512 allocatesize_fsb, bmapi_flag,
3513 &firstfsb, 0, imapp, &nimaps,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003514 &free_list, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 if (error) {
3516 goto error0;
3517 }
3518
3519 /*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003520 * Complete the transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003522 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (error) {
3524 goto error0;
3525 }
3526
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003527 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3529 if (error) {
3530 break;
3531 }
3532
3533 allocated_fsb = imapp->br_blockcount;
3534
Nathan Scottdd9f4382006-01-11 15:28:28 +11003535 if (nimaps == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 error = XFS_ERROR(ENOSPC);
3537 break;
3538 }
3539
3540 startoffset_fsb += allocated_fsb;
3541 allocatesize_fsb -= allocated_fsb;
3542 }
3543dmapi_enospc_check:
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003544 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3545 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003547 ip, DM_RIGHT_NULL,
3548 ip, DM_RIGHT_NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3550 if (error == 0)
3551 goto retry; /* Maybe DMAPI app. has made space */
3552 /* else fall through with error from XFS_SEND_DATA */
3553 }
3554
3555 return error;
3556
Nathan Scottdd9f4382006-01-11 15:28:28 +11003557error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 xfs_bmap_cancel(&free_list);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003559 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3560
3561error1: /* Just cancel transaction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3563 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3564 goto dmapi_enospc_check;
3565}
3566
3567/*
3568 * Zero file bytes between startoff and endoff inclusive.
3569 * The iolock is held exclusive and no blocks are buffered.
3570 */
3571STATIC int
3572xfs_zero_remaining_bytes(
3573 xfs_inode_t *ip,
3574 xfs_off_t startoff,
3575 xfs_off_t endoff)
3576{
3577 xfs_bmbt_irec_t imap;
3578 xfs_fileoff_t offset_fsb;
3579 xfs_off_t lastoffset;
3580 xfs_off_t offset;
3581 xfs_buf_t *bp;
3582 xfs_mount_t *mp = ip->i_mount;
3583 int nimap;
3584 int error = 0;
3585
3586 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003587 XFS_IS_REALTIME_INODE(ip) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 mp->m_rtdev_targp : mp->m_ddev_targp);
3589
3590 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3591 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3592 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003593 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003594 NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (error || nimap < 1)
3596 break;
3597 ASSERT(imap.br_blockcount >= 1);
3598 ASSERT(imap.br_startoff == offset_fsb);
3599 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3600 if (lastoffset > endoff)
3601 lastoffset = endoff;
3602 if (imap.br_startblock == HOLESTARTBLOCK)
3603 continue;
3604 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3605 if (imap.br_state == XFS_EXT_UNWRITTEN)
3606 continue;
3607 XFS_BUF_UNDONE(bp);
3608 XFS_BUF_UNWRITE(bp);
3609 XFS_BUF_READ(bp);
3610 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3611 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003612 error = xfs_iowait(bp);
3613 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3615 mp, bp, XFS_BUF_ADDR(bp));
3616 break;
3617 }
3618 memset(XFS_BUF_PTR(bp) +
3619 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3620 0, lastoffset - offset + 1);
3621 XFS_BUF_UNDONE(bp);
3622 XFS_BUF_UNREAD(bp);
3623 XFS_BUF_WRITE(bp);
3624 xfsbdstrat(mp, bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10003625 error = xfs_iowait(bp);
3626 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3628 mp, bp, XFS_BUF_ADDR(bp));
3629 break;
3630 }
3631 }
3632 xfs_buf_free(bp);
3633 return error;
3634}
3635
3636/*
3637 * xfs_free_file_space()
3638 * This routine frees disk space for the given file.
3639 *
3640 * This routine is only called by xfs_change_file_space
3641 * for an UNRESVSP type call.
3642 *
3643 * RETURNS:
3644 * 0 on success
3645 * errno on error
3646 *
3647 */
3648STATIC int
3649xfs_free_file_space(
3650 xfs_inode_t *ip,
3651 xfs_off_t offset,
3652 xfs_off_t len,
3653 int attr_flags)
3654{
Nathan Scott67fcaa72006-06-09 17:00:52 +10003655 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 int committed;
3657 int done;
3658 xfs_off_t end_dmi_offset;
3659 xfs_fileoff_t endoffset_fsb;
3660 int error;
3661 xfs_fsblock_t firstfsb;
3662 xfs_bmap_free_t free_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 xfs_bmbt_irec_t imap;
3664 xfs_off_t ioffset;
3665 xfs_extlen_t mod=0;
3666 xfs_mount_t *mp;
3667 int nimap;
3668 uint resblks;
Nathan Scott673cdf52006-09-28 10:56:26 +10003669 uint rounding;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 int rt;
3671 xfs_fileoff_t startoffset_fsb;
3672 xfs_trans_t *tp;
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003673 int need_iolock = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003675 vp = XFS_ITOV(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 mp = ip->i_mount;
3677
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003678 xfs_itrace_entry(ip);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3681 return error;
3682
3683 error = 0;
3684 if (len <= 0) /* if nothing being freed */
3685 return error;
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003686 rt = XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3688 end_dmi_offset = offset + len;
3689 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3690
Christoph Hellwigeb9df392007-08-16 18:42:07 +10003691 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3692 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003693 if (end_dmi_offset > ip->i_size)
3694 end_dmi_offset = ip->i_size;
Christoph Hellwigbc4ac742008-03-06 13:45:58 +11003695 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 offset, end_dmi_offset - offset,
3697 AT_DELAY_FLAG(attr_flags), NULL);
3698 if (error)
Jesper Juhl014c2542006-01-15 02:37:08 +01003699 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 }
3701
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003702 if (attr_flags & ATTR_NOLOCK)
3703 need_iolock = 0;
Yingping Lu9fa80462006-03-22 12:44:35 +11003704 if (need_iolock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 xfs_ilock(ip, XFS_IOLOCK_EXCL);
Christoph Hellwigb677c212007-08-29 11:46:28 +10003706 vn_iowait(ip); /* wait for the completion of any pending DIOs */
Yingping Lu9fa80462006-03-22 12:44:35 +11003707 }
Dean Roehrich5fcbab32005-05-05 13:27:19 -07003708
Tim Shimmine6a4b372007-11-23 16:30:42 +11003709 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 ioffset = offset & ~(rounding - 1);
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003711
3712 if (VN_CACHED(vp) != 0) {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003713 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3714 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
Lachlan McIlroyd3cf2092007-05-08 13:49:27 +10003715 if (error)
3716 goto out_unlock_iolock;
Christoph Hellwigbd5a8762005-06-21 15:47:39 +10003717 }
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 /*
3720 * Need to zero the stuff we're not freeing, on disk.
3721 * If its a realtime file & can't use unwritten extents then we
3722 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3723 * will take care of it for us.
3724 */
Eric Sandeen62118702008-03-06 13:44:28 +11003725 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003727 error = xfs_bmapi(NULL, ip, startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003728 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (error)
3730 goto out_unlock_iolock;
3731 ASSERT(nimap == 0 || nimap == 1);
3732 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3733 xfs_daddr_t block;
3734
3735 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3736 block = imap.br_startblock;
3737 mod = do_div(block, mp->m_sb.sb_rextsize);
3738 if (mod)
3739 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3740 }
3741 nimap = 1;
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003742 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003743 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 if (error)
3745 goto out_unlock_iolock;
3746 ASSERT(nimap == 0 || nimap == 1);
3747 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3748 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3749 mod++;
3750 if (mod && (mod != mp->m_sb.sb_rextsize))
3751 endoffset_fsb -= mod;
3752 }
3753 }
3754 if ((done = (endoffset_fsb <= startoffset_fsb)))
3755 /*
3756 * One contiguous piece to clear
3757 */
3758 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3759 else {
3760 /*
3761 * Some full blocks, possibly two pieces to clear
3762 */
3763 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3764 error = xfs_zero_remaining_bytes(ip, offset,
3765 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3766 if (!error &&
3767 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3768 error = xfs_zero_remaining_bytes(ip,
3769 XFS_FSB_TO_B(mp, endoffset_fsb),
3770 offset + len - 1);
3771 }
3772
3773 /*
3774 * free file space until done or until there is an error
3775 */
3776 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3777 while (!error && !done) {
3778
3779 /*
David Chinner91ebecc2007-07-19 16:28:30 +10003780 * allocate and setup the transaction. Allow this
3781 * transaction to dip into the reserve blocks to ensure
3782 * the freeing of the space succeeds at ENOSPC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 */
3784 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
David Chinner91ebecc2007-07-19 16:28:30 +10003785 tp->t_flags |= XFS_TRANS_RESERVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 error = xfs_trans_reserve(tp,
3787 resblks,
3788 XFS_WRITE_LOG_RES(mp),
3789 0,
3790 XFS_TRANS_PERM_LOG_RES,
3791 XFS_WRITE_LOG_COUNT);
3792
3793 /*
3794 * check for running out of space
3795 */
3796 if (error) {
3797 /*
3798 * Free the transaction structure.
3799 */
3800 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3801 xfs_trans_cancel(tp, 0);
3802 break;
3803 }
3804 xfs_ilock(ip, XFS_ILOCK_EXCL);
3805 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
Nathan Scottdd9f4382006-01-11 15:28:28 +11003806 ip->i_udquot, ip->i_gdquot, resblks, 0,
3807 XFS_QMOPT_RES_REGBLKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 if (error)
3809 goto error1;
3810
3811 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3812 xfs_trans_ihold(tp, ip);
3813
3814 /*
3815 * issue the bunmapi() call to free the blocks
3816 */
3817 XFS_BMAP_INIT(&free_list, &firstfsb);
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10003818 error = xfs_bunmapi(tp, ip, startoffset_fsb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 endoffset_fsb - startoffset_fsb,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003820 0, 2, &firstfsb, &free_list, NULL, &done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (error) {
3822 goto error0;
3823 }
3824
3825 /*
3826 * complete the transaction
3827 */
Eric Sandeenf7c99b62007-02-10 18:37:16 +11003828 error = xfs_bmap_finish(&tp, &free_list, &committed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (error) {
3830 goto error0;
3831 }
3832
Eric Sandeen1c72bf92007-05-08 13:48:42 +10003833 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3835 }
3836
3837 out_unlock_iolock:
3838 if (need_iolock)
3839 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3840 return error;
3841
3842 error0:
3843 xfs_bmap_cancel(&free_list);
3844 error1:
3845 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3846 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3847 XFS_ILOCK_EXCL);
3848 return error;
3849}
3850
3851/*
3852 * xfs_change_file_space()
3853 * This routine allocates or frees disk space for the given file.
3854 * The user specified parameters are checked for alignment and size
3855 * limitations.
3856 *
3857 * RETURNS:
3858 * 0 on success
3859 * errno on error
3860 *
3861 */
3862int
3863xfs_change_file_space(
Christoph Hellwig993386c2007-08-28 16:12:30 +10003864 xfs_inode_t *ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 int cmd,
3866 xfs_flock64_t *bf,
3867 xfs_off_t offset,
3868 cred_t *credp,
3869 int attr_flags)
3870{
Christoph Hellwig993386c2007-08-28 16:12:30 +10003871 xfs_mount_t *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 int clrprealloc;
3873 int error;
3874 xfs_fsize_t fsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 int setprealloc;
3876 xfs_off_t startoffset;
3877 xfs_off_t llen;
3878 xfs_trans_t *tp;
Nathan Scott8285fb52006-06-09 17:07:12 +10003879 bhv_vattr_t va;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11003881 xfs_itrace_entry(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882
Christoph Hellwig993386c2007-08-28 16:12:30 +10003883 if (!S_ISREG(ip->i_d.di_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return XFS_ERROR(EINVAL);
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 switch (bf->l_whence) {
3887 case 0: /*SEEK_SET*/
3888 break;
3889 case 1: /*SEEK_CUR*/
3890 bf->l_start += offset;
3891 break;
3892 case 2: /*SEEK_END*/
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003893 bf->l_start += ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 break;
3895 default:
3896 return XFS_ERROR(EINVAL);
3897 }
3898
3899 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3900
3901 if ( (bf->l_start < 0)
3902 || (bf->l_start > XFS_MAXIOFFSET(mp))
3903 || (bf->l_start + llen < 0)
3904 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3905 return XFS_ERROR(EINVAL);
3906
3907 bf->l_whence = 0;
3908
3909 startoffset = bf->l_start;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10003910 fsize = ip->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 /*
3913 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3914 * file space.
3915 * These calls do NOT zero the data space allocated to the file,
3916 * nor do they change the file size.
3917 *
3918 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3919 * space.
3920 * These calls cause the new file data to be zeroed and the file
3921 * size to be changed.
3922 */
3923 setprealloc = clrprealloc = 0;
3924
3925 switch (cmd) {
3926 case XFS_IOC_RESVSP:
3927 case XFS_IOC_RESVSP64:
3928 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3929 1, attr_flags);
3930 if (error)
3931 return error;
3932 setprealloc = 1;
3933 break;
3934
3935 case XFS_IOC_UNRESVSP:
3936 case XFS_IOC_UNRESVSP64:
3937 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3938 attr_flags)))
3939 return error;
3940 break;
3941
3942 case XFS_IOC_ALLOCSP:
3943 case XFS_IOC_ALLOCSP64:
3944 case XFS_IOC_FREESP:
3945 case XFS_IOC_FREESP64:
3946 if (startoffset > fsize) {
3947 error = xfs_alloc_file_space(ip, fsize,
3948 startoffset - fsize, 0, attr_flags);
3949 if (error)
3950 break;
3951 }
3952
3953 va.va_mask = XFS_AT_SIZE;
3954 va.va_size = startoffset;
3955
Christoph Hellwig993386c2007-08-28 16:12:30 +10003956 error = xfs_setattr(ip, &va, attr_flags, credp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 if (error)
3959 return error;
3960
3961 clrprealloc = 1;
3962 break;
3963
3964 default:
3965 ASSERT(0);
3966 return XFS_ERROR(EINVAL);
3967 }
3968
3969 /*
3970 * update the inode timestamp, mode, and prealloc flag bits
3971 */
3972 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3973
3974 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3975 0, 0, 0))) {
3976 /* ASSERT(0); */
3977 xfs_trans_cancel(tp, 0);
3978 return error;
3979 }
3980
3981 xfs_ilock(ip, XFS_ILOCK_EXCL);
3982
3983 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3984 xfs_trans_ihold(tp, ip);
3985
3986 if ((attr_flags & ATTR_DMI) == 0) {
3987 ip->i_d.di_mode &= ~S_ISUID;
3988
3989 /*
3990 * Note that we don't have to worry about mandatory
3991 * file locking being disabled here because we only
3992 * clear the S_ISGID bit if the Group execute bit is
3993 * on, but if it was on then mandatory locking wouldn't
3994 * have been enabled.
3995 */
3996 if (ip->i_d.di_mode & S_IXGRP)
3997 ip->i_d.di_mode &= ~S_ISGID;
3998
3999 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4000 }
4001 if (setprealloc)
4002 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4003 else if (clrprealloc)
4004 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4005
4006 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4007 xfs_trans_set_sync(tp);
4008
Eric Sandeen1c72bf92007-05-08 13:48:42 +10004009 error = xfs_trans_commit(tp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
4011 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4012
4013 return error;
4014}