[XFS] Use power-of-2 sized buffers to reduce overhead
[deliverable/linux.git] / fs / xfs / xfs_vnodeops.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
16f7e0fe 18
1da177e4 19#include "xfs.h"
a844f451 20#include "xfs_fs.h"
1da177e4 21#include "xfs_types.h"
a844f451 22#include "xfs_bit.h"
1da177e4 23#include "xfs_log.h"
a844f451 24#include "xfs_inum.h"
1da177e4
LT
25#include "xfs_trans.h"
26#include "xfs_sb.h"
27#include "xfs_ag.h"
1da177e4
LT
28#include "xfs_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
a844f451 31#include "xfs_da_btree.h"
1da177e4 32#include "xfs_bmap_btree.h"
a844f451 33#include "xfs_alloc_btree.h"
1da177e4 34#include "xfs_ialloc_btree.h"
1da177e4 35#include "xfs_dir2_sf.h"
a844f451 36#include "xfs_attr_sf.h"
1da177e4 37#include "xfs_dinode.h"
1da177e4 38#include "xfs_inode.h"
a844f451 39#include "xfs_inode_item.h"
a844f451
NS
40#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
1da177e4 44#include "xfs_bmap.h"
1da177e4
LT
45#include "xfs_attr.h"
46#include "xfs_rw.h"
1da177e4 47#include "xfs_error.h"
1da177e4
LT
48#include "xfs_quota.h"
49#include "xfs_utils.h"
a844f451 50#include "xfs_rtalloc.h"
1da177e4 51#include "xfs_trans_space.h"
1da177e4 52#include "xfs_log_priv.h"
2a82b8be 53#include "xfs_filestream.h"
993386c1 54#include "xfs_vnodeops.h"
1da177e4 55
993386c1 56int
1da177e4 57xfs_open(
993386c1 58 xfs_inode_t *ip)
1da177e4
LT
59{
60 int mode;
1da177e4
LT
61
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 */
993386c1 69 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
1da177e4
LT
70 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
1da177e4
LT
78/*
79 * xfs_getattr
80 */
993386c1 81int
1da177e4 82xfs_getattr(
993386c1 83 xfs_inode_t *ip,
8285fb58 84 bhv_vattr_t *vap,
993386c1 85 int flags)
1da177e4 86{
993386c1
CH
87 bhv_vnode_t *vp = XFS_ITOV(ip);
88 xfs_mount_t *mp = ip->i_mount;
1da177e4 89
cf441eeb 90 xfs_itrace_entry(ip);
1da177e4 91
1da177e4
LT
92 if (XFS_FORCED_SHUTDOWN(mp))
93 return XFS_ERROR(EIO);
94
95 if (!(flags & ATTR_LAZY))
96 xfs_ilock(ip, XFS_ILOCK_SHARED);
97
ba87ea69 98 vap->va_size = XFS_ISIZE(ip);
1da177e4
LT
99 if (vap->va_mask == XFS_AT_SIZE)
100 goto all_done;
101
102 vap->va_nblocks =
103 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
104 vap->va_nodeid = ip->i_ino;
105#if XFS_BIG_INUMS
106 vap->va_nodeid += mp->m_inoadd;
107#endif
108 vap->va_nlink = ip->i_d.di_nlink;
109
110 /*
111 * Quick exit for non-stat callers
112 */
113 if ((vap->va_mask &
114 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
115 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
116 goto all_done;
117
118 /*
119 * Copy from in-core inode.
120 */
0432dab2 121 vap->va_mode = ip->i_d.di_mode;
1da177e4
LT
122 vap->va_uid = ip->i_d.di_uid;
123 vap->va_gid = ip->i_d.di_gid;
124 vap->va_projid = ip->i_d.di_projid;
125
126 /*
127 * Check vnode type block/char vs. everything else.
1da177e4 128 */
0432dab2
CH
129 switch (ip->i_d.di_mode & S_IFMT) {
130 case S_IFBLK:
131 case S_IFCHR:
132 vap->va_rdev = ip->i_df.if_u2.if_rdev;
133 vap->va_blocksize = BLKDEV_IOSIZE;
134 break;
135 default:
1da177e4
LT
136 vap->va_rdev = 0;
137
71ddabb9 138 if (!(XFS_IS_REALTIME_INODE(ip))) {
e8c8b3a7 139 vap->va_blocksize = xfs_preferred_iosize(mp);
1da177e4
LT
140 } else {
141
142 /*
143 * If the file blocks are being allocated from a
144 * realtime partition, then return the inode's
145 * realtime extent size or the realtime volume's
146 * extent size.
147 */
957d0ebe
DC
148 vap->va_blocksize =
149 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
1da177e4 150 }
0432dab2 151 break;
1da177e4
LT
152 }
153
ca5ccbf9 154 vn_atime_to_timespec(vp, &vap->va_atime);
1da177e4
LT
155 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
156 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
157 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
158 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
159
160 /*
161 * Exit for stat callers. See if any of the rest of the fields
162 * to be filled in are needed.
163 */
164 if ((vap->va_mask &
165 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
166 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
167 goto all_done;
168
169 /*
170 * Convert di_flags to xflags.
171 */
172 vap->va_xflags = xfs_ip2xflags(ip);
173
174 /*
175 * Exit for inode revalidate. See if any of the rest of
176 * the fields to be filled in are needed.
177 */
178 if ((vap->va_mask &
179 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
180 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
181 goto all_done;
182
183 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
184 vap->va_nextents =
185 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
186 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
187 ip->i_d.di_nextents;
188 if (ip->i_afp)
189 vap->va_anextents =
190 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
191 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
192 ip->i_d.di_anextents;
193 else
194 vap->va_anextents = 0;
195 vap->va_gen = ip->i_d.di_gen;
196
197 all_done:
198 if (!(flags & ATTR_LAZY))
199 xfs_iunlock(ip, XFS_ILOCK_SHARED);
200 return 0;
201}
202
203
204/*
205 * xfs_setattr
206 */
207int
208xfs_setattr(
993386c1 209 xfs_inode_t *ip,
8285fb58 210 bhv_vattr_t *vap,
1da177e4
LT
211 int flags,
212 cred_t *credp)
213{
993386c1
CH
214 bhv_vnode_t *vp = XFS_ITOV(ip);
215 xfs_mount_t *mp = ip->i_mount;
1da177e4 216 xfs_trans_t *tp;
1da177e4
LT
217 int mask;
218 int code;
219 uint lock_flags;
220 uint commit_flags=0;
221 uid_t uid=0, iuid=0;
222 gid_t gid=0, igid=0;
223 int timeflags = 0;
1da177e4
LT
224 xfs_prid_t projid=0, iprojid=0;
225 int mandlock_before, mandlock_after;
226 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
227 int file_owner;
5fcbab35 228 int need_iolock = 1;
1da177e4 229
cf441eeb 230 xfs_itrace_entry(ip);
1da177e4 231
bd186aa9 232 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
233 return XFS_ERROR(EROFS);
234
235 /*
236 * Cannot set certain attributes.
237 */
238 mask = vap->va_mask;
239 if (mask & XFS_AT_NOSET) {
240 return XFS_ERROR(EINVAL);
241 }
242
1da177e4
LT
243 if (XFS_FORCED_SHUTDOWN(mp))
244 return XFS_ERROR(EIO);
245
246 /*
247 * Timestamps do not need to be logged and hence do not
248 * need to be done within a transaction.
249 */
250 if (mask & XFS_AT_UPDTIMES) {
251 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
252 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
253 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
254 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
255 xfs_ichgtime(ip, timeflags);
256 return 0;
257 }
258
259 olddquot1 = olddquot2 = NULL;
260 udqp = gdqp = NULL;
261
262 /*
263 * If disk quotas is on, we make sure that the dquots do exist on disk,
264 * before we start any other transactions. Trying to do this later
265 * is messy. We don't care to take a readlock to look at the ids
266 * in inode here, because we can't hold it across the trans_reserve.
267 * If the IDs do change before we take the ilock, we're covered
268 * because the i_*dquot fields will get updated anyway.
269 */
c8ad20ff
NS
270 if (XFS_IS_QUOTA_ON(mp) &&
271 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
1da177e4
LT
272 uint qflags = 0;
273
c8ad20ff 274 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
1da177e4
LT
275 uid = vap->va_uid;
276 qflags |= XFS_QMOPT_UQUOTA;
277 } else {
278 uid = ip->i_d.di_uid;
279 }
c8ad20ff 280 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
1da177e4
LT
281 gid = vap->va_gid;
282 qflags |= XFS_QMOPT_GQUOTA;
283 } else {
284 gid = ip->i_d.di_gid;
285 }
c8ad20ff
NS
286 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
287 projid = vap->va_projid;
288 qflags |= XFS_QMOPT_PQUOTA;
289 } else {
290 projid = ip->i_d.di_projid;
291 }
1da177e4
LT
292 /*
293 * We take a reference when we initialize udqp and gdqp,
294 * so it is important that we never blindly double trip on
295 * the same variable. See xfs_create() for an example.
296 */
297 ASSERT(udqp == NULL);
298 ASSERT(gdqp == NULL);
c8ad20ff
NS
299 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
300 &udqp, &gdqp);
1da177e4 301 if (code)
014c2544 302 return code;
1da177e4
LT
303 }
304
305 /*
306 * For the other attributes, we acquire the inode lock and
307 * first do an error checking pass.
308 */
309 tp = NULL;
310 lock_flags = XFS_ILOCK_EXCL;
5fcbab35
DR
311 if (flags & ATTR_NOLOCK)
312 need_iolock = 0;
1da177e4
LT
313 if (!(mask & XFS_AT_SIZE)) {
314 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
315 (mp->m_flags & XFS_MOUNT_WSYNC)) {
316 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
317 commit_flags = 0;
318 if ((code = xfs_trans_reserve(tp, 0,
319 XFS_ICHANGE_LOG_RES(mp), 0,
320 0, 0))) {
321 lock_flags = 0;
322 goto error_return;
323 }
324 }
325 } else {
eb9df39d 326 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
1da177e4
LT
327 !(flags & ATTR_DMI)) {
328 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
329 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
330 vap->va_size, 0, dmflags, NULL);
331 if (code) {
332 lock_flags = 0;
333 goto error_return;
334 }
335 }
336 if (need_iolock)
337 lock_flags |= XFS_IOLOCK_EXCL;
338 }
339
340 xfs_ilock(ip, lock_flags);
341
342 /* boolean: are we the file owner? */
343 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
344
345 /*
346 * Change various properties of a file.
347 * Only the owner or users with CAP_FOWNER
348 * capability may do these things.
349 */
350 if (mask &
351 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
352 XFS_AT_GID|XFS_AT_PROJID)) {
353 /*
354 * CAP_FOWNER overrides the following restrictions:
355 *
356 * The user ID of the calling process must be equal
357 * to the file owner ID, except in cases where the
358 * CAP_FSETID capability is applicable.
359 */
360 if (!file_owner && !capable(CAP_FOWNER)) {
361 code = XFS_ERROR(EPERM);
362 goto error_return;
363 }
364
365 /*
366 * CAP_FSETID overrides the following restrictions:
367 *
368 * The effective user ID of the calling process shall match
369 * the file owner when setting the set-user-ID and
370 * set-group-ID bits on that file.
371 *
372 * The effective group ID or one of the supplementary group
373 * IDs of the calling process shall match the group owner of
374 * the file when setting the set-group-ID bit on that file
375 */
376 if (mask & XFS_AT_MODE) {
377 mode_t m = 0;
378
379 if ((vap->va_mode & S_ISUID) && !file_owner)
380 m |= S_ISUID;
381 if ((vap->va_mode & S_ISGID) &&
382 !in_group_p((gid_t)ip->i_d.di_gid))
383 m |= S_ISGID;
384#if 0
385 /* Linux allows this, Irix doesn't. */
0432dab2 386 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
1da177e4
LT
387 m |= S_ISVTX;
388#endif
389 if (m && !capable(CAP_FSETID))
390 vap->va_mode &= ~m;
391 }
392 }
393
394 /*
395 * Change file ownership. Must be the owner or privileged.
396 * If the system was configured with the "restricted_chown"
397 * option, the owner is not permitted to give away the file,
398 * and can change the group id only to a group of which he
399 * or she is a member.
400 */
401 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
402 /*
403 * These IDs could have changed since we last looked at them.
404 * But, we're assured that if the ownership did change
405 * while we didn't have the inode locked, inode's dquot(s)
406 * would have changed also.
407 */
408 iuid = ip->i_d.di_uid;
409 iprojid = ip->i_d.di_projid;
410 igid = ip->i_d.di_gid;
411 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
412 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
413 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
414 iprojid;
415
416 /*
417 * CAP_CHOWN overrides the following restrictions:
418 *
419 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
420 * shall override the restriction that a process cannot
421 * change the user ID of a file it owns and the restriction
422 * that the group ID supplied to the chown() function
423 * shall be equal to either the group ID or one of the
424 * supplementary group IDs of the calling process.
1da177e4
LT
425 */
426 if (restricted_chown &&
427 (iuid != uid || (igid != gid &&
428 !in_group_p((gid_t)gid))) &&
429 !capable(CAP_CHOWN)) {
430 code = XFS_ERROR(EPERM);
431 goto error_return;
432 }
433 /*
c8ad20ff 434 * Do a quota reservation only if uid/projid/gid is actually
1da177e4
LT
435 * going to change.
436 */
437 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
c8ad20ff 438 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
1da177e4
LT
439 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
440 ASSERT(tp);
441 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
442 capable(CAP_FOWNER) ?
443 XFS_QMOPT_FORCE_RES : 0);
444 if (code) /* out of quota */
445 goto error_return;
446 }
447 }
448
449 /*
450 * Truncate file. Must have write permission and not be a directory.
451 */
452 if (mask & XFS_AT_SIZE) {
453 /* Short circuit the truncate case for zero length files */
454 if ((vap->va_size == 0) &&
ba87ea69 455 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
1da177e4
LT
456 xfs_iunlock(ip, XFS_ILOCK_EXCL);
457 lock_flags &= ~XFS_ILOCK_EXCL;
458 if (mask & XFS_AT_CTIME)
459 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
460 code = 0;
461 goto error_return;
462 }
463
0432dab2 464 if (VN_ISDIR(vp)) {
1da177e4
LT
465 code = XFS_ERROR(EISDIR);
466 goto error_return;
0432dab2 467 } else if (!VN_ISREG(vp)) {
1da177e4
LT
468 code = XFS_ERROR(EINVAL);
469 goto error_return;
470 }
471 /*
472 * Make sure that the dquots are attached to the inode.
473 */
474 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
475 goto error_return;
476 }
477
478 /*
479 * Change file access or modified times.
480 */
481 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
482 if (!file_owner) {
483 if ((flags & ATTR_UTIME) &&
484 !capable(CAP_FOWNER)) {
485 code = XFS_ERROR(EPERM);
486 goto error_return;
487 }
488 }
489 }
490
491 /*
492 * Change extent size or realtime flag.
493 */
494 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
495 /*
496 * Can't change extent size if any extents are allocated.
497 */
e94af02a 498 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
1da177e4
LT
499 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
500 vap->va_extsize) ) {
501 code = XFS_ERROR(EINVAL); /* EFBIG? */
502 goto error_return;
503 }
504
1da177e4
LT
505 /*
506 * Can't change realtime flag if any extents are allocated.
507 */
e94af02a
ES
508 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
509 (mask & XFS_AT_XFLAGS) &&
71ddabb9 510 (XFS_IS_REALTIME_INODE(ip)) !=
1da177e4
LT
511 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
512 code = XFS_ERROR(EINVAL); /* EFBIG? */
513 goto error_return;
514 }
515 /*
516 * Extent size must be a multiple of the appropriate block
517 * size, if set at all.
518 */
519 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
520 xfs_extlen_t size;
521
71ddabb9 522 if (XFS_IS_REALTIME_INODE(ip) ||
1da177e4
LT
523 ((mask & XFS_AT_XFLAGS) &&
524 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
525 size = mp->m_sb.sb_rextsize <<
526 mp->m_sb.sb_blocklog;
527 } else {
528 size = mp->m_sb.sb_blocksize;
529 }
530 if (vap->va_extsize % size) {
531 code = XFS_ERROR(EINVAL);
532 goto error_return;
533 }
534 }
535 /*
536 * If realtime flag is set then must have realtime data.
537 */
538 if ((mask & XFS_AT_XFLAGS) &&
539 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
540 if ((mp->m_sb.sb_rblocks == 0) ||
541 (mp->m_sb.sb_rextsize == 0) ||
542 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
543 code = XFS_ERROR(EINVAL);
544 goto error_return;
545 }
546 }
547
548 /*
549 * Can't modify an immutable/append-only file unless
550 * we have appropriate permission.
551 */
552 if ((mask & XFS_AT_XFLAGS) &&
553 (ip->i_d.di_flags &
554 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
555 (vap->va_xflags &
556 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
557 !capable(CAP_LINUX_IMMUTABLE)) {
558 code = XFS_ERROR(EPERM);
559 goto error_return;
560 }
561 }
562
563 /*
564 * Now we can make the changes. Before we join the inode
565 * to the transaction, if XFS_AT_SIZE is set then take care of
566 * the part of the truncation that must be done without the
567 * inode lock. This needs to be done before joining the inode
568 * to the transaction, because the inode cannot be unlocked
569 * once it is a part of the transaction.
570 */
571 if (mask & XFS_AT_SIZE) {
572 code = 0;
ba87ea69 573 if ((vap->va_size > ip->i_size) &&
374e2ac3 574 (flags & ATTR_NOSIZETOK) == 0) {
1da177e4 575 code = xfs_igrow_start(ip, vap->va_size, credp);
374e2ac3 576 }
1da177e4 577 xfs_iunlock(ip, XFS_ILOCK_EXCL);
c32676ee
DC
578
579 /*
580 * We are going to log the inode size change in this
581 * transaction so any previous writes that are beyond the on
582 * disk EOF and the new EOF that have not been written out need
583 * to be written here. If we do not write the data out, we
584 * expose ourselves to the null files problem.
585 *
586 * Only flush from the on disk size to the smaller of the in
587 * memory file size or the new size as that's the range we
588 * really care about here and prevents waiting for other data
589 * not within the range we care about here.
590 */
591 if (!code &&
592 (ip->i_size != ip->i_d.di_size) &&
593 (vap->va_size > ip->i_d.di_size)) {
739bfb2a 594 code = xfs_flush_pages(ip,
c32676ee
DC
595 ip->i_d.di_size, vap->va_size,
596 XFS_B_ASYNC, FI_NONE);
597 }
598
599 /* wait for all I/O to complete */
b677c210 600 vn_iowait(ip);
c32676ee 601
1da177e4
LT
602 if (!code)
603 code = xfs_itruncate_data(ip, vap->va_size);
604 if (code) {
605 ASSERT(tp == NULL);
606 lock_flags &= ~XFS_ILOCK_EXCL;
607 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
608 goto error_return;
609 }
610 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
611 if ((code = xfs_trans_reserve(tp, 0,
612 XFS_ITRUNCATE_LOG_RES(mp), 0,
613 XFS_TRANS_PERM_LOG_RES,
614 XFS_ITRUNCATE_LOG_COUNT))) {
615 xfs_trans_cancel(tp, 0);
616 if (need_iolock)
617 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
618 return code;
619 }
620 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
621 xfs_ilock(ip, XFS_ILOCK_EXCL);
622 }
623
624 if (tp) {
625 xfs_trans_ijoin(tp, ip, lock_flags);
626 xfs_trans_ihold(tp, ip);
627 }
628
629 /* determine whether mandatory locking mode changes */
630 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
631
632 /*
633 * Truncate file. Must have write permission and not be a directory.
634 */
635 if (mask & XFS_AT_SIZE) {
44d814ce
DC
636 /*
637 * Only change the c/mtime if we are changing the size
638 * or we are explicitly asked to change it. This handles
639 * the semantic difference between truncate() and ftruncate()
640 * as implemented in the VFS.
641 */
642 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
643 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
644
ba87ea69 645 if (vap->va_size > ip->i_size) {
1da177e4
LT
646 xfs_igrow_finish(tp, ip, vap->va_size,
647 !(flags & ATTR_DMI));
ba87ea69 648 } else if ((vap->va_size <= ip->i_size) ||
1da177e4
LT
649 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
650 /*
651 * signal a sync transaction unless
652 * we're truncating an already unlinked
653 * file on a wsync filesystem
654 */
655 code = xfs_itruncate_finish(&tp, ip,
656 (xfs_fsize_t)vap->va_size,
657 XFS_DATA_FORK,
658 ((ip->i_d.di_nlink != 0 ||
659 !(mp->m_flags & XFS_MOUNT_WSYNC))
660 ? 1 : 0));
7d4fb40a 661 if (code)
1da177e4 662 goto abort_return;
7d4fb40a
NS
663 /*
664 * Truncated "down", so we're removing references
665 * to old data here - if we now delay flushing for
666 * a long time, we expose ourselves unduly to the
667 * notorious NULL files problem. So, we mark this
668 * vnode and flush it when the file is closed, and
669 * do not wait the usual (long) time for writeout.
670 */
b3aea4ed 671 xfs_iflags_set(ip, XFS_ITRUNCATED);
1da177e4 672 }
1da177e4
LT
673 }
674
675 /*
676 * Change file access modes.
677 */
678 if (mask & XFS_AT_MODE) {
679 ip->i_d.di_mode &= S_IFMT;
680 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
681
682 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
683 timeflags |= XFS_ICHGTIME_CHG;
684 }
685
686 /*
687 * Change file ownership. Must be the owner or privileged.
688 * If the system was configured with the "restricted_chown"
689 * option, the owner is not permitted to give away the file,
690 * and can change the group id only to a group of which he
691 * or she is a member.
692 */
693 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
694 /*
695 * CAP_FSETID overrides the following restrictions:
696 *
697 * The set-user-ID and set-group-ID bits of a file will be
698 * cleared upon successful return from chown()
699 */
700 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
701 !capable(CAP_FSETID)) {
702 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
703 }
704
705 /*
706 * Change the ownerships and register quota modifications
707 * in the transaction.
708 */
709 if (iuid != uid) {
710 if (XFS_IS_UQUOTA_ON(mp)) {
711 ASSERT(mask & XFS_AT_UID);
712 ASSERT(udqp);
713 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
714 &ip->i_udquot, udqp);
715 }
716 ip->i_d.di_uid = uid;
717 }
718 if (igid != gid) {
719 if (XFS_IS_GQUOTA_ON(mp)) {
c8ad20ff 720 ASSERT(!XFS_IS_PQUOTA_ON(mp));
1da177e4
LT
721 ASSERT(mask & XFS_AT_GID);
722 ASSERT(gdqp);
723 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
724 &ip->i_gdquot, gdqp);
725 }
726 ip->i_d.di_gid = gid;
727 }
728 if (iprojid != projid) {
c8ad20ff
NS
729 if (XFS_IS_PQUOTA_ON(mp)) {
730 ASSERT(!XFS_IS_GQUOTA_ON(mp));
731 ASSERT(mask & XFS_AT_PROJID);
732 ASSERT(gdqp);
733 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
734 &ip->i_gdquot, gdqp);
735 }
1da177e4
LT
736 ip->i_d.di_projid = projid;
737 /*
738 * We may have to rev the inode as well as
739 * the superblock version number since projids didn't
740 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
741 */
742 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
743 xfs_bump_ino_vers2(tp, ip);
744 }
745
746 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
747 timeflags |= XFS_ICHGTIME_CHG;
748 }
749
750
751 /*
752 * Change file access or modified times.
753 */
754 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
755 if (mask & XFS_AT_ATIME) {
756 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
757 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
758 ip->i_update_core = 1;
759 timeflags &= ~XFS_ICHGTIME_ACC;
760 }
761 if (mask & XFS_AT_MTIME) {
762 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
763 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
764 timeflags &= ~XFS_ICHGTIME_MOD;
765 timeflags |= XFS_ICHGTIME_CHG;
766 }
767 if (tp && (flags & ATTR_UTIME))
768 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
769 }
770
771 /*
772 * Change XFS-added attributes.
773 */
774 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
775 if (mask & XFS_AT_EXTSIZE) {
776 /*
777 * Converting bytes to fs blocks.
778 */
779 ip->i_d.di_extsize = vap->va_extsize >>
780 mp->m_sb.sb_blocklog;
781 }
782 if (mask & XFS_AT_XFLAGS) {
783 uint di_flags;
784
785 /* can't set PREALLOC this way, just preserve it */
786 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
787 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
788 di_flags |= XFS_DIFLAG_IMMUTABLE;
789 if (vap->va_xflags & XFS_XFLAG_APPEND)
790 di_flags |= XFS_DIFLAG_APPEND;
791 if (vap->va_xflags & XFS_XFLAG_SYNC)
792 di_flags |= XFS_DIFLAG_SYNC;
793 if (vap->va_xflags & XFS_XFLAG_NOATIME)
794 di_flags |= XFS_DIFLAG_NOATIME;
795 if (vap->va_xflags & XFS_XFLAG_NODUMP)
796 di_flags |= XFS_DIFLAG_NODUMP;
365ca83d
NS
797 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
798 di_flags |= XFS_DIFLAG_PROJINHERIT;
d3446eac
BN
799 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
800 di_flags |= XFS_DIFLAG_NODEFRAG;
2a82b8be
DC
801 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
802 di_flags |= XFS_DIFLAG_FILESTREAM;
1da177e4
LT
803 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
804 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
805 di_flags |= XFS_DIFLAG_RTINHERIT;
806 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
807 di_flags |= XFS_DIFLAG_NOSYMLINKS;
dd9f438e
NS
808 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
809 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
810 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
613d7043 811 if (vap->va_xflags & XFS_XFLAG_REALTIME)
1da177e4 812 di_flags |= XFS_DIFLAG_REALTIME;
dd9f438e
NS
813 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
814 di_flags |= XFS_DIFLAG_EXTSIZE;
1da177e4
LT
815 }
816 ip->i_d.di_flags = di_flags;
817 }
818 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
819 timeflags |= XFS_ICHGTIME_CHG;
820 }
821
822 /*
823 * Change file inode change time only if XFS_AT_CTIME set
824 * AND we have been called by a DMI function.
825 */
826
827 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
828 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
829 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
830 ip->i_update_core = 1;
831 timeflags &= ~XFS_ICHGTIME_CHG;
832 }
833
834 /*
835 * Send out timestamp changes that need to be set to the
836 * current time. Not done when called by a DMI function.
837 */
838 if (timeflags && !(flags & ATTR_DMI))
839 xfs_ichgtime(ip, timeflags);
840
841 XFS_STATS_INC(xs_ig_attrchg);
842
843 /*
844 * If this is a synchronous mount, make sure that the
845 * transaction goes to disk before returning to the user.
846 * This is slightly sub-optimal in that truncates require
c41564b5 847 * two sync transactions instead of one for wsync filesystems.
1da177e4
LT
848 * One for the truncate and one for the timestamps since we
849 * don't want to change the timestamps unless we're sure the
850 * truncate worked. Truncates are less than 1% of the laddis
851 * mix so this probably isn't worth the trouble to optimize.
852 */
853 code = 0;
854 if (tp) {
855 if (mp->m_flags & XFS_MOUNT_WSYNC)
856 xfs_trans_set_sync(tp);
857
1c72bf90 858 code = xfs_trans_commit(tp, commit_flags);
1da177e4
LT
859 }
860
861 /*
862 * If the (regular) file's mandatory locking mode changed, then
863 * notify the vnode. We do this under the inode lock to prevent
864 * racing calls to vop_vnode_change.
865 */
866 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
1da177e4
LT
867
868 xfs_iunlock(ip, lock_flags);
869
870 /*
871 * Release any dquot(s) the inode had kept before chown.
872 */
873 XFS_QM_DQRELE(mp, olddquot1);
874 XFS_QM_DQRELE(mp, olddquot2);
875 XFS_QM_DQRELE(mp, udqp);
876 XFS_QM_DQRELE(mp, gdqp);
877
878 if (code) {
879 return code;
880 }
881
eb9df39d 882 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
1da177e4
LT
883 !(flags & ATTR_DMI)) {
884 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
885 NULL, DM_RIGHT_NULL, NULL, NULL,
886 0, 0, AT_DELAY_FLAG(flags));
887 }
888 return 0;
889
890 abort_return:
891 commit_flags |= XFS_TRANS_ABORT;
892 /* FALLTHROUGH */
893 error_return:
894 XFS_QM_DQRELE(mp, udqp);
895 XFS_QM_DQRELE(mp, gdqp);
896 if (tp) {
897 xfs_trans_cancel(tp, commit_flags);
898 }
899 if (lock_flags != 0) {
900 xfs_iunlock(ip, lock_flags);
901 }
902 return code;
903}
904
7d4fb40a
NS
905/*
906 * The maximum pathlen is 1024 bytes. Since the minimum file system
907 * blocksize is 512 bytes, we can get a max of 2 extents back from
908 * bmapi.
909 */
910#define SYMLINK_MAPS 2
911
804c83c3
CH
912STATIC int
913xfs_readlink_bmap(
914 xfs_inode_t *ip,
915 char *link)
916{
917 xfs_mount_t *mp = ip->i_mount;
918 int pathlen = ip->i_d.di_size;
919 int nmaps = SYMLINK_MAPS;
920 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
921 xfs_daddr_t d;
922 int byte_cnt;
923 int n;
924 xfs_buf_t *bp;
925 int error = 0;
926
927 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
928 mval, &nmaps, NULL, NULL);
929 if (error)
930 goto out;
931
932 for (n = 0; n < nmaps; n++) {
933 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
934 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
935
936 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
937 error = XFS_BUF_GETERROR(bp);
938 if (error) {
939 xfs_ioerror_alert("xfs_readlink",
940 ip->i_mount, bp, XFS_BUF_ADDR(bp));
941 xfs_buf_relse(bp);
942 goto out;
943 }
944 if (pathlen < byte_cnt)
945 byte_cnt = pathlen;
946 pathlen -= byte_cnt;
947
948 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
949 xfs_buf_relse(bp);
950 }
951
952 link[ip->i_d.di_size] = '\0';
953 error = 0;
954
955 out:
956 return error;
957}
958
993386c1 959int
1da177e4 960xfs_readlink(
993386c1 961 xfs_inode_t *ip,
804c83c3 962 char *link)
1da177e4 963{
804c83c3 964 xfs_mount_t *mp = ip->i_mount;
1da177e4 965 int pathlen;
1da177e4 966 int error = 0;
1da177e4 967
cf441eeb 968 xfs_itrace_entry(ip);
1da177e4
LT
969
970 if (XFS_FORCED_SHUTDOWN(mp))
971 return XFS_ERROR(EIO);
972
973 xfs_ilock(ip, XFS_ILOCK_SHARED);
974
975 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
804c83c3 976 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
1da177e4 977
804c83c3
CH
978 pathlen = ip->i_d.di_size;
979 if (!pathlen)
980 goto out;
1da177e4
LT
981
982 if (ip->i_df.if_flags & XFS_IFINLINE) {
804c83c3
CH
983 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
984 link[pathlen] = '\0';
985 } else {
986 error = xfs_readlink_bmap(ip, link);
1da177e4
LT
987 }
988
804c83c3 989 out:
1da177e4 990 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1da177e4
LT
991 return error;
992}
993
1da177e4
LT
994/*
995 * xfs_fsync
996 *
997 * This is called to sync the inode and its data out to disk.
998 * We need to hold the I/O lock while flushing the data, and
999 * the inode lock while flushing the inode. The inode lock CANNOT
1000 * be held while flushing the data, so acquire after we're done
1001 * with that.
1002 */
993386c1 1003int
1da177e4 1004xfs_fsync(
993386c1 1005 xfs_inode_t *ip,
1da177e4 1006 int flag,
1da177e4
LT
1007 xfs_off_t start,
1008 xfs_off_t stop)
1009{
1da177e4
LT
1010 xfs_trans_t *tp;
1011 int error;
f538d4da 1012 int log_flushed = 0, changed = 1;
1da177e4 1013
cf441eeb 1014 xfs_itrace_entry(ip);
1da177e4
LT
1015
1016 ASSERT(start >= 0 && stop >= -1);
1017
1018 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1019 return XFS_ERROR(EIO);
1020
776a75fa
LM
1021 if (flag & FSYNC_DATA)
1022 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1023
1da177e4
LT
1024 /*
1025 * We always need to make sure that the required inode state
1026 * is safe on disk. The vnode might be clean but because
1027 * of committed transactions that haven't hit the disk yet.
1028 * Likewise, there could be unflushed non-transactional
1029 * changes to the inode core that have to go to disk.
1030 *
1031 * The following code depends on one assumption: that
1032 * any transaction that changes an inode logs the core
1033 * because it has to change some field in the inode core
1034 * (typically nextents or nblocks). That assumption
1035 * implies that any transactions against an inode will
1036 * catch any non-transactional updates. If inode-altering
1037 * transactions exist that violate this assumption, the
1038 * code breaks. Right now, it figures that if the involved
1039 * update_* field is clear and the inode is unpinned, the
1040 * inode is clean. Either it's been flushed or it's been
1041 * committed and the commit has hit the disk unpinning the inode.
1042 * (Note that xfs_inode_item_format() called at commit clears
1043 * the update_* fields.)
1044 */
1045 xfs_ilock(ip, XFS_ILOCK_SHARED);
1046
1047 /* If we are flushing data then we care about update_size
1048 * being set, otherwise we care about update_core
1049 */
1050 if ((flag & FSYNC_DATA) ?
1051 (ip->i_update_size == 0) :
1052 (ip->i_update_core == 0)) {
1053 /*
1054 * Timestamps/size haven't changed since last inode
1055 * flush or inode transaction commit. That means
1056 * either nothing got written or a transaction
1057 * committed which caught the updates. If the
1058 * latter happened and the transaction hasn't
1059 * hit the disk yet, the inode will be still
1060 * be pinned. If it is, force the log.
1061 */
1062
1063 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1064
1065 if (xfs_ipincount(ip)) {
f538d4da 1066 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1da177e4
LT
1067 XFS_LOG_FORCE |
1068 ((flag & FSYNC_WAIT)
f538d4da
CH
1069 ? XFS_LOG_SYNC : 0),
1070 &log_flushed);
1071 } else {
1072 /*
1073 * If the inode is not pinned and nothing
1074 * has changed we don't need to flush the
1075 * cache.
1076 */
1077 changed = 0;
1da177e4
LT
1078 }
1079 error = 0;
1080 } else {
1081 /*
1082 * Kick off a transaction to log the inode
1083 * core to get the updates. Make it
1084 * sync if FSYNC_WAIT is passed in (which
1085 * is done by everybody but specfs). The
1086 * sync transaction will also force the log.
1087 */
1088 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1089 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1090 if ((error = xfs_trans_reserve(tp, 0,
1091 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1092 0, 0, 0))) {
1093 xfs_trans_cancel(tp, 0);
1094 return error;
1095 }
1096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1097
1098 /*
1099 * Note - it's possible that we might have pushed
1100 * ourselves out of the way during trans_reserve
1101 * which would flush the inode. But there's no
1102 * guarantee that the inode buffer has actually
1103 * gone out yet (it's delwri). Plus the buffer
1104 * could be pinned anyway if it's part of an
1105 * inode in another recent transaction. So we
1106 * play it safe and fire off the transaction anyway.
1107 */
1108 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1109 xfs_trans_ihold(tp, ip);
1110 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1111 if (flag & FSYNC_WAIT)
1112 xfs_trans_set_sync(tp);
1c72bf90 1113 error = _xfs_trans_commit(tp, 0, &log_flushed);
1da177e4
LT
1114
1115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1116 }
f538d4da
CH
1117
1118 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1119 /*
1120 * If the log write didn't issue an ordered tag we need
1121 * to flush the disk cache for the data device now.
1122 */
1123 if (!log_flushed)
1124 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1125
1126 /*
1127 * If this inode is on the RT dev we need to flush that
c41564b5 1128 * cache as well.
f538d4da 1129 */
71ddabb9 1130 if (XFS_IS_REALTIME_INODE(ip))
f538d4da
CH
1131 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1132 }
1133
1da177e4
LT
1134 return error;
1135}
1136
1137/*
92dfe8d2
DC
1138 * This is called by xfs_inactive to free any blocks beyond eof
1139 * when the link count isn't zero and by xfs_dm_punch_hole() when
1140 * punching a hole to EOF.
1da177e4 1141 */
92dfe8d2
DC
1142int
1143xfs_free_eofblocks(
1da177e4 1144 xfs_mount_t *mp,
92dfe8d2
DC
1145 xfs_inode_t *ip,
1146 int flags)
1da177e4
LT
1147{
1148 xfs_trans_t *tp;
1149 int error;
1150 xfs_fileoff_t end_fsb;
1151 xfs_fileoff_t last_fsb;
1152 xfs_filblks_t map_len;
1153 int nimaps;
1154 xfs_bmbt_irec_t imap;
92dfe8d2 1155 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1da177e4
LT
1156
1157 /*
1158 * Figure out if there are any blocks beyond the end
1159 * of the file. If not, then there is nothing to do.
1160 */
ba87ea69 1161 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1da177e4
LT
1162 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1163 map_len = last_fsb - end_fsb;
1164 if (map_len <= 0)
014c2544 1165 return 0;
1da177e4
LT
1166
1167 nimaps = 1;
1168 xfs_ilock(ip, XFS_ILOCK_SHARED);
541d7d3c 1169 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
3e57ecf6 1170 NULL, 0, &imap, &nimaps, NULL, NULL);
1da177e4
LT
1171 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1172
1173 if (!error && (nimaps != 0) &&
68bdb6ea
YL
1174 (imap.br_startblock != HOLESTARTBLOCK ||
1175 ip->i_delayed_blks)) {
1da177e4
LT
1176 /*
1177 * Attach the dquots to the inode up front.
1178 */
1179 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
014c2544 1180 return error;
1da177e4
LT
1181
1182 /*
1183 * There are blocks after the end of file.
1184 * Free them up now by truncating the file to
1185 * its current size.
1186 */
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1188
1189 /*
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because
1192 * itruncate_start will call into the buffer
1193 * cache and we can't
1194 * do that within a transaction.
1195 */
92dfe8d2
DC
1196 if (use_iolock)
1197 xfs_ilock(ip, XFS_IOLOCK_EXCL);
d3cf2094 1198 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
ba87ea69 1199 ip->i_size);
d3cf2094 1200 if (error) {
87ae3c24 1201 xfs_trans_cancel(tp, 0);
92dfe8d2
DC
1202 if (use_iolock)
1203 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
d3cf2094
LM
1204 return error;
1205 }
1da177e4
LT
1206
1207 error = xfs_trans_reserve(tp, 0,
1208 XFS_ITRUNCATE_LOG_RES(mp),
1209 0, XFS_TRANS_PERM_LOG_RES,
1210 XFS_ITRUNCATE_LOG_COUNT);
1211 if (error) {
1212 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1213 xfs_trans_cancel(tp, 0);
1214 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 1215 return error;
1da177e4
LT
1216 }
1217
1218 xfs_ilock(ip, XFS_ILOCK_EXCL);
1219 xfs_trans_ijoin(tp, ip,
1220 XFS_IOLOCK_EXCL |
1221 XFS_ILOCK_EXCL);
1222 xfs_trans_ihold(tp, ip);
1223
1224 error = xfs_itruncate_finish(&tp, ip,
ba87ea69 1225 ip->i_size,
1da177e4
LT
1226 XFS_DATA_FORK,
1227 0);
1228 /*
1229 * If we get an error at this point we
1230 * simply don't bother truncating the file.
1231 */
1232 if (error) {
1233 xfs_trans_cancel(tp,
1234 (XFS_TRANS_RELEASE_LOG_RES |
1235 XFS_TRANS_ABORT));
1236 } else {
1237 error = xfs_trans_commit(tp,
1c72bf90 1238 XFS_TRANS_RELEASE_LOG_RES);
1da177e4 1239 }
92dfe8d2
DC
1240 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1241 : XFS_ILOCK_EXCL));
1da177e4 1242 }
014c2544 1243 return error;
1da177e4
LT
1244}
1245
1246/*
1247 * Free a symlink that has blocks associated with it.
1248 */
1249STATIC int
1250xfs_inactive_symlink_rmt(
1251 xfs_inode_t *ip,
1252 xfs_trans_t **tpp)
1253{
1254 xfs_buf_t *bp;
1255 int committed;
1256 int done;
1257 int error;
1258 xfs_fsblock_t first_block;
1259 xfs_bmap_free_t free_list;
1260 int i;
1261 xfs_mount_t *mp;
1262 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1263 int nmaps;
1264 xfs_trans_t *ntp;
1265 int size;
1266 xfs_trans_t *tp;
1267
1268 tp = *tpp;
1269 mp = ip->i_mount;
1270 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1271 /*
1272 * We're freeing a symlink that has some
1273 * blocks allocated to it. Free the
1274 * blocks here. We know that we've got
1275 * either 1 or 2 extents and that we can
1276 * free them all in one bunmapi call.
1277 */
1278 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1279 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1280 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1281 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1282 xfs_trans_cancel(tp, 0);
1283 *tpp = NULL;
1284 return error;
1285 }
1286 /*
1287 * Lock the inode, fix the size, and join it to the transaction.
1288 * Hold it so in the normal path, we still have it locked for
1289 * the second transaction. In the error paths we need it
1290 * held so the cancel won't rele it, see below.
1291 */
1292 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1293 size = (int)ip->i_d.di_size;
1294 ip->i_d.di_size = 0;
1295 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1296 xfs_trans_ihold(tp, ip);
1297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1298 /*
1299 * Find the block(s) so we can inval and unmap them.
1300 */
1301 done = 0;
1302 XFS_BMAP_INIT(&free_list, &first_block);
e8c96f8c 1303 nmaps = ARRAY_SIZE(mval);
1da177e4
LT
1304 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1305 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
3e57ecf6 1306 &free_list, NULL)))
1da177e4
LT
1307 goto error0;
1308 /*
1309 * Invalidate the block(s).
1310 */
1311 for (i = 0; i < nmaps; i++) {
1312 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1313 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1314 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1315 xfs_trans_binval(tp, bp);
1316 }
1317 /*
1318 * Unmap the dead block(s) to the free_list.
1319 */
1320 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
3e57ecf6 1321 &first_block, &free_list, NULL, &done)))
1da177e4
LT
1322 goto error1;
1323 ASSERT(done);
1324 /*
1325 * Commit the first transaction. This logs the EFI and the inode.
1326 */
f7c99b6f 1327 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1da177e4
LT
1328 goto error1;
1329 /*
1330 * The transaction must have been committed, since there were
1331 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1332 * The new tp has the extent freeing and EFDs.
1333 */
1334 ASSERT(committed);
1335 /*
1336 * The first xact was committed, so add the inode to the new one.
1337 * Mark it dirty so it will be logged and moved forward in the log as
1338 * part of every commit.
1339 */
1340 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1341 xfs_trans_ihold(tp, ip);
1342 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1343 /*
1344 * Get a new, empty transaction to return to our caller.
1345 */
1346 ntp = xfs_trans_dup(tp);
1347 /*
c41564b5 1348 * Commit the transaction containing extent freeing and EFDs.
1da177e4
LT
1349 * If we get an error on the commit here or on the reserve below,
1350 * we need to unlock the inode since the new transaction doesn't
1351 * have the inode attached.
1352 */
1c72bf90 1353 error = xfs_trans_commit(tp, 0);
1da177e4
LT
1354 tp = ntp;
1355 if (error) {
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1357 goto error0;
1358 }
1359 /*
1360 * Remove the memory for extent descriptions (just bookkeeping).
1361 */
1362 if (ip->i_df.if_bytes)
1363 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1364 ASSERT(ip->i_df.if_bytes == 0);
1365 /*
1366 * Put an itruncate log reservation in the new transaction
1367 * for our caller.
1368 */
1369 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1370 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1371 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1372 goto error0;
1373 }
1374 /*
1375 * Return with the inode locked but not joined to the transaction.
1376 */
1377 *tpp = tp;
1378 return 0;
1379
1380 error1:
1381 xfs_bmap_cancel(&free_list);
1382 error0:
1383 /*
1384 * Have to come here with the inode locked and either
1385 * (held and in the transaction) or (not in the transaction).
1386 * If the inode isn't held then cancel would iput it, but
1387 * that's wrong since this is inactive and the vnode ref
1388 * count is 0 already.
1389 * Cancel won't do anything to the inode if held, but it still
1390 * needs to be locked until the cancel is done, if it was
1391 * joined to the transaction.
1392 */
1393 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1394 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1395 *tpp = NULL;
1396 return error;
1397
1398}
1399
1400STATIC int
1401xfs_inactive_symlink_local(
1402 xfs_inode_t *ip,
1403 xfs_trans_t **tpp)
1404{
1405 int error;
1406
1407 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1408 /*
1409 * We're freeing a symlink which fit into
1410 * the inode. Just free the memory used
1411 * to hold the old symlink.
1412 */
1413 error = xfs_trans_reserve(*tpp, 0,
1414 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1415 0, XFS_TRANS_PERM_LOG_RES,
1416 XFS_ITRUNCATE_LOG_COUNT);
1417
1418 if (error) {
1419 xfs_trans_cancel(*tpp, 0);
1420 *tpp = NULL;
014c2544 1421 return error;
1da177e4
LT
1422 }
1423 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1424
1425 /*
1426 * Zero length symlinks _can_ exist.
1427 */
1428 if (ip->i_df.if_bytes > 0) {
1429 xfs_idata_realloc(ip,
1430 -(ip->i_df.if_bytes),
1431 XFS_DATA_FORK);
1432 ASSERT(ip->i_df.if_bytes == 0);
1433 }
014c2544 1434 return 0;
1da177e4
LT
1435}
1436
1da177e4
LT
1437STATIC int
1438xfs_inactive_attrs(
1439 xfs_inode_t *ip,
1440 xfs_trans_t **tpp)
1441{
1442 xfs_trans_t *tp;
1443 int error;
1444 xfs_mount_t *mp;
1445
1446 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1447 tp = *tpp;
1448 mp = ip->i_mount;
1449 ASSERT(ip->i_d.di_forkoff != 0);
1c72bf90 1450 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1451 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1452
1453 error = xfs_attr_inactive(ip);
1454 if (error) {
1455 *tpp = NULL;
1456 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 1457 return error; /* goto out */
1da177e4
LT
1458 }
1459
1460 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1461 error = xfs_trans_reserve(tp, 0,
1462 XFS_IFREE_LOG_RES(mp),
1463 0, XFS_TRANS_PERM_LOG_RES,
1464 XFS_INACTIVE_LOG_COUNT);
1465 if (error) {
1466 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1467 xfs_trans_cancel(tp, 0);
1468 *tpp = NULL;
1469 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 1470 return error;
1da177e4
LT
1471 }
1472
1473 xfs_ilock(ip, XFS_ILOCK_EXCL);
1474 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1475 xfs_trans_ihold(tp, ip);
1476 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1477
1478 ASSERT(ip->i_d.di_anextents == 0);
1479
1480 *tpp = tp;
014c2544 1481 return 0;
1da177e4
LT
1482}
1483
993386c1 1484int
1da177e4 1485xfs_release(
993386c1 1486 xfs_inode_t *ip)
1da177e4 1487{
993386c1
CH
1488 bhv_vnode_t *vp = XFS_ITOV(ip);
1489 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
1490 int error;
1491
7d4fb40a 1492 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1da177e4 1493 return 0;
1da177e4
LT
1494
1495 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 1496 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
1497 return 0;
1498
2a82b8be 1499 if (!XFS_FORCED_SHUTDOWN(mp)) {
b3aea4ed
CH
1500 int truncated;
1501
2a82b8be
DC
1502 /*
1503 * If we are using filestreams, and we have an unlinked
1504 * file that we are processing the last close on, then nothing
1505 * will be able to reopen and write to this file. Purge this
1506 * inode from the filestreams cache so that it doesn't delay
1507 * teardown of the inode.
1508 */
1509 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1510 xfs_filestream_deassociate(ip);
1511
fbf3ce8d
CH
1512 /*
1513 * If we previously truncated this file and removed old data
1514 * in the process, we want to initiate "early" writeout on
1515 * the last close. This is an attempt to combat the notorious
1516 * NULL files problem which is particularly noticable from a
1517 * truncate down, buffered (re-)write (delalloc), followed by
1518 * a crash. What we are effectively doing here is
1519 * significantly reducing the time window where we'd otherwise
1520 * be exposed to that problem.
1521 */
09262b43 1522 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
b3aea4ed 1523 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
739bfb2a 1524 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
fbf3ce8d
CH
1525 }
1526
1da177e4
LT
1527 if (ip->i_d.di_nlink != 0) {
1528 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
ba87ea69 1529 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
68bdb6ea 1530 ip->i_delayed_blks > 0)) &&
1da177e4 1531 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
dd9f438e
NS
1532 (!(ip->i_d.di_flags &
1533 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
92dfe8d2
DC
1534 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1535 if (error)
014c2544 1536 return error;
1da177e4
LT
1537 }
1538 }
1539
1540 return 0;
1541}
1542
1543/*
1544 * xfs_inactive
1545 *
1546 * This is called when the vnode reference count for the vnode
1547 * goes to zero. If the file has been unlinked, then it must
1548 * now be truncated. Also, we clear all of the read-ahead state
1549 * kept for the inode here since the file is now closed.
1550 */
993386c1 1551int
1da177e4 1552xfs_inactive(
993386c1 1553 xfs_inode_t *ip)
1da177e4 1554{
993386c1 1555 bhv_vnode_t *vp = XFS_ITOV(ip);
67fcaa73 1556 xfs_bmap_free_t free_list;
1da177e4
LT
1557 xfs_fsblock_t first_block;
1558 int committed;
1559 xfs_trans_t *tp;
1560 xfs_mount_t *mp;
1561 int error;
1562 int truncate;
1563
cf441eeb 1564 xfs_itrace_entry(ip);
1da177e4 1565
1da177e4
LT
1566 /*
1567 * If the inode is already free, then there can be nothing
1568 * to clean up here.
1569 */
1570 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1571 ASSERT(ip->i_df.if_real_bytes == 0);
1572 ASSERT(ip->i_df.if_broot_bytes == 0);
1573 return VN_INACTIVE_CACHE;
1574 }
1575
1576 /*
1577 * Only do a truncate if it's a regular file with
1578 * some actual space in it. It's OK to look at the
1579 * inode's fields without the lock because we're the
1580 * only one with a reference to the inode.
1581 */
1582 truncate = ((ip->i_d.di_nlink == 0) &&
ba87ea69
LM
1583 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1584 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1da177e4
LT
1585 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1586
1587 mp = ip->i_mount;
1588
eb9df39d 1589 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
1da177e4
LT
1590 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1591 }
1592
1593 error = 0;
1594
1595 /* If this is a read-only mount, don't do this (would generate I/O) */
bd186aa9 1596 if (mp->m_flags & XFS_MOUNT_RDONLY)
1da177e4
LT
1597 goto out;
1598
1599 if (ip->i_d.di_nlink != 0) {
1600 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
ba87ea69 1601 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
68bdb6ea 1602 ip->i_delayed_blks > 0)) &&
dd9f438e
NS
1603 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1604 (!(ip->i_d.di_flags &
1605 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1606 (ip->i_delayed_blks != 0)))) {
92dfe8d2
DC
1607 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1608 if (error)
014c2544 1609 return VN_INACTIVE_CACHE;
1da177e4
LT
1610 }
1611 goto out;
1612 }
1613
1614 ASSERT(ip->i_d.di_nlink == 0);
1615
1616 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
014c2544 1617 return VN_INACTIVE_CACHE;
1da177e4
LT
1618
1619 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1620 if (truncate) {
1621 /*
1622 * Do the xfs_itruncate_start() call before
1623 * reserving any log space because itruncate_start
1624 * will call into the buffer cache and we can't
1625 * do that within a transaction.
1626 */
1627 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1628
d3cf2094
LM
1629 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1630 if (error) {
87ae3c24 1631 xfs_trans_cancel(tp, 0);
d3cf2094
LM
1632 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1633 return VN_INACTIVE_CACHE;
1634 }
1da177e4
LT
1635
1636 error = xfs_trans_reserve(tp, 0,
1637 XFS_ITRUNCATE_LOG_RES(mp),
1638 0, XFS_TRANS_PERM_LOG_RES,
1639 XFS_ITRUNCATE_LOG_COUNT);
1640 if (error) {
1641 /* Don't call itruncate_cleanup */
1642 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1643 xfs_trans_cancel(tp, 0);
1644 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
014c2544 1645 return VN_INACTIVE_CACHE;
1da177e4
LT
1646 }
1647
1648 xfs_ilock(ip, XFS_ILOCK_EXCL);
1649 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1650 xfs_trans_ihold(tp, ip);
1651
1652 /*
1653 * normally, we have to run xfs_itruncate_finish sync.
1654 * But if filesystem is wsync and we're in the inactive
1655 * path, then we know that nlink == 0, and that the
1656 * xaction that made nlink == 0 is permanently committed
1657 * since xfs_remove runs as a synchronous transaction.
1658 */
1659 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1660 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1661
1662 if (error) {
1663 xfs_trans_cancel(tp,
1664 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1665 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
014c2544 1666 return VN_INACTIVE_CACHE;
1da177e4
LT
1667 }
1668 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1669
1670 /*
1671 * If we get an error while cleaning up a
1672 * symlink we bail out.
1673 */
1674 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1675 xfs_inactive_symlink_rmt(ip, &tp) :
1676 xfs_inactive_symlink_local(ip, &tp);
1677
1678 if (error) {
1679 ASSERT(tp == NULL);
014c2544 1680 return VN_INACTIVE_CACHE;
1da177e4
LT
1681 }
1682
1683 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1684 xfs_trans_ihold(tp, ip);
1685 } else {
1686 error = xfs_trans_reserve(tp, 0,
1687 XFS_IFREE_LOG_RES(mp),
1688 0, XFS_TRANS_PERM_LOG_RES,
1689 XFS_INACTIVE_LOG_COUNT);
1690 if (error) {
1691 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1692 xfs_trans_cancel(tp, 0);
014c2544 1693 return VN_INACTIVE_CACHE;
1da177e4
LT
1694 }
1695
1696 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1697 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1698 xfs_trans_ihold(tp, ip);
1699 }
1700
1701 /*
1702 * If there are attributes associated with the file
1703 * then blow them away now. The code calls a routine
1704 * that recursively deconstructs the attribute fork.
1705 * We need to just commit the current transaction
1706 * because we can't use it for xfs_attr_inactive().
1707 */
1708 if (ip->i_d.di_anextents > 0) {
1709 error = xfs_inactive_attrs(ip, &tp);
1710 /*
1711 * If we got an error, the transaction is already
1712 * cancelled, and the inode is unlocked. Just get out.
1713 */
1714 if (error)
014c2544 1715 return VN_INACTIVE_CACHE;
1da177e4
LT
1716 } else if (ip->i_afp) {
1717 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1718 }
1719
1720 /*
1721 * Free the inode.
1722 */
1723 XFS_BMAP_INIT(&free_list, &first_block);
1724 error = xfs_ifree(tp, ip, &free_list);
1725 if (error) {
1726 /*
1727 * If we fail to free the inode, shut down. The cancel
1728 * might do that, we need to make sure. Otherwise the
1729 * inode might be lost for a long time or forever.
1730 */
1731 if (!XFS_FORCED_SHUTDOWN(mp)) {
1732 cmn_err(CE_NOTE,
1733 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1734 error, mp->m_fsname);
7d04a335 1735 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1da177e4
LT
1736 }
1737 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1738 } else {
1739 /*
1740 * Credit the quota account(s). The inode is gone.
1741 */
1742 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1743
1744 /*
1745 * Just ignore errors at this point. There is
1746 * nothing we can do except to try to keep going.
1747 */
f7c99b6f 1748 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1c72bf90 1749 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1750 }
1751 /*
1752 * Release the dquots held by inode, if any.
1753 */
1754 XFS_QM_DQDETACH(mp, ip);
1755
1756 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1757
1758 out:
1759 return VN_INACTIVE_CACHE;
1760}
1761
1762
993386c1 1763int
1da177e4 1764xfs_lookup(
993386c1 1765 xfs_inode_t *dp,
8285fb58 1766 bhv_vname_t *dentry,
993386c1 1767 bhv_vnode_t **vpp)
1da177e4 1768{
993386c1 1769 xfs_inode_t *ip;
1da177e4
LT
1770 xfs_ino_t e_inum;
1771 int error;
1772 uint lock_mode;
1da177e4 1773
cf441eeb 1774 xfs_itrace_entry(dp);
1da177e4
LT
1775
1776 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1777 return XFS_ERROR(EIO);
1778
1779 lock_mode = xfs_ilock_map_shared(dp);
993386c1 1780 error = xfs_dir_lookup_int(dp, lock_mode, dentry, &e_inum, &ip);
1da177e4
LT
1781 if (!error) {
1782 *vpp = XFS_ITOV(ip);
cf441eeb 1783 xfs_itrace_ref(ip);
1da177e4
LT
1784 }
1785 xfs_iunlock_map_shared(dp, lock_mode);
1786 return error;
1787}
1788
993386c1 1789int
1da177e4 1790xfs_create(
993386c1 1791 xfs_inode_t *dp,
8285fb58 1792 bhv_vname_t *dentry,
3e5daf05
CH
1793 mode_t mode,
1794 xfs_dev_t rdev,
67fcaa73 1795 bhv_vnode_t **vpp,
1da177e4
LT
1796 cred_t *credp)
1797{
1798 char *name = VNAME(dentry);
993386c1
CH
1799 xfs_mount_t *mp = dp->i_mount;
1800 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1801 xfs_inode_t *ip;
67fcaa73 1802 bhv_vnode_t *vp = NULL;
1da177e4 1803 xfs_trans_t *tp;
1da177e4
LT
1804 int error;
1805 xfs_bmap_free_t free_list;
1806 xfs_fsblock_t first_block;
993386c1 1807 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
1808 int dm_event_sent = 0;
1809 uint cancel_flags;
1810 int committed;
1811 xfs_prid_t prid;
1812 struct xfs_dquot *udqp, *gdqp;
1813 uint resblks;
1da177e4
LT
1814 int namelen;
1815
1816 ASSERT(!*vpp);
cf441eeb 1817 xfs_itrace_entry(dp);
1da177e4 1818
1da177e4
LT
1819 namelen = VNAMELEN(dentry);
1820
eb9df39d 1821 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1da177e4
LT
1822 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1823 dir_vp, DM_RIGHT_NULL, NULL,
1824 DM_RIGHT_NULL, name, NULL,
3e5daf05 1825 mode, 0, 0);
1da177e4
LT
1826
1827 if (error)
1828 return error;
1829 dm_event_sent = 1;
1830 }
1831
1832 if (XFS_FORCED_SHUTDOWN(mp))
1833 return XFS_ERROR(EIO);
1834
1835 /* Return through std_return after this point. */
1836
1837 udqp = gdqp = NULL;
365ca83d
NS
1838 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1839 prid = dp->i_d.di_projid;
1da177e4
LT
1840 else
1841 prid = (xfs_prid_t)dfltprid;
1842
1843 /*
1844 * Make sure that we have allocated dquot(s) on disk.
1845 */
1846 error = XFS_QM_DQVOPALLOC(mp, dp,
c8ad20ff 1847 current_fsuid(credp), current_fsgid(credp), prid,
1da177e4
LT
1848 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1849 if (error)
1850 goto std_return;
1851
1852 ip = NULL;
1da177e4
LT
1853
1854 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1855 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1856 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1857 /*
1858 * Initially assume that the file does not exist and
1859 * reserve the resources for that case. If that is not
1860 * the case we'll drop the one we have and get a more
1861 * appropriate transaction later.
1862 */
1863 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1864 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1865 if (error == ENOSPC) {
1866 resblks = 0;
1867 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1868 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1869 }
1870 if (error) {
1871 cancel_flags = 0;
1da177e4
LT
1872 goto error_return;
1873 }
1874
f7c66ce3 1875 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 1876 unlock_dp_on_error = B_TRUE;
1da177e4
LT
1877
1878 XFS_BMAP_INIT(&free_list, &first_block);
1879
1880 ASSERT(ip == NULL);
1881
1882 /*
1883 * Reserve disk quota and the inode.
1884 */
1885 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1886 if (error)
1887 goto error_return;
1888
f6c2d1fa 1889 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1da177e4 1890 goto error_return;
3e5daf05 1891 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1da177e4
LT
1892 rdev, credp, prid, resblks > 0,
1893 &ip, &committed);
1894 if (error) {
1895 if (error == ENOSPC)
1896 goto error_return;
1897 goto abort_return;
1898 }
cf441eeb 1899 xfs_itrace_ref(ip);
1da177e4
LT
1900
1901 /*
1902 * At this point, we've gotten a newly allocated inode.
1903 * It is locked (and joined to the transaction).
1904 */
1905
1906 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1907
1908 /*
993386c1
CH
1909 * Now we join the directory inode to the transaction. We do not do it
1910 * earlier because xfs_dir_ialloc might commit the previous transaction
1911 * (and release all the locks). An error from here on will result in
1912 * the transaction cancel unlocking dp so don't do it explicitly in the
1913 * error path.
1da177e4 1914 */
1da177e4
LT
1915 VN_HOLD(dir_vp);
1916 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 1917 unlock_dp_on_error = B_FALSE;
1da177e4 1918
f6c2d1fa
NS
1919 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1920 &first_block, &free_list, resblks ?
1921 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1da177e4
LT
1922 if (error) {
1923 ASSERT(error != ENOSPC);
1924 goto abort_return;
1925 }
1926 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1927 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1928
1929 /*
1930 * If this is a synchronous mount, make sure that the
1931 * create transaction goes to disk before returning to
1932 * the user.
1933 */
1934 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1935 xfs_trans_set_sync(tp);
1936 }
1937
1938 dp->i_gen++;
1939
1940 /*
1941 * Attach the dquot(s) to the inodes and modify them incore.
1942 * These ids of the inode couldn't have changed since the new
1943 * inode has been locked ever since it was created.
1944 */
1945 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1946
1947 /*
1948 * xfs_trans_commit normally decrements the vnode ref count
1949 * when it unlocks the inode. Since we want to return the
1950 * vnode to the caller, we bump the vnode ref count now.
1951 */
1952 IHOLD(ip);
1953 vp = XFS_ITOV(ip);
1954
f7c99b6f 1955 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
1956 if (error) {
1957 xfs_bmap_cancel(&free_list);
1958 goto abort_rele;
1959 }
1960
1c72bf90 1961 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
1962 if (error) {
1963 IRELE(ip);
1964 tp = NULL;
1965 goto error_return;
1966 }
1967
1968 XFS_QM_DQRELE(mp, udqp);
1969 XFS_QM_DQRELE(mp, gdqp);
1970
1da177e4
LT
1971 *vpp = vp;
1972
1973 /* Fallthrough to std_return with error = 0 */
1974
1975std_return:
eb9df39d 1976 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
993386c1 1977 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1da177e4
LT
1978 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1979 dir_vp, DM_RIGHT_NULL,
1980 *vpp ? vp:NULL,
1981 DM_RIGHT_NULL, name, NULL,
3e5daf05 1982 mode, error, 0);
1da177e4
LT
1983 }
1984 return error;
1985
1986 abort_return:
1987 cancel_flags |= XFS_TRANS_ABORT;
1988 /* FALLTHROUGH */
1da177e4 1989
014c2544 1990 error_return:
1da177e4
LT
1991 if (tp != NULL)
1992 xfs_trans_cancel(tp, cancel_flags);
1993
1da177e4
LT
1994 XFS_QM_DQRELE(mp, udqp);
1995 XFS_QM_DQRELE(mp, gdqp);
1996
993386c1
CH
1997 if (unlock_dp_on_error)
1998 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1999
1da177e4
LT
2000 goto std_return;
2001
2002 abort_rele:
2003 /*
2004 * Wait until after the current transaction is aborted to
2005 * release the inode. This prevents recursive transactions
2006 * and deadlocks from xfs_inactive.
2007 */
2008 cancel_flags |= XFS_TRANS_ABORT;
2009 xfs_trans_cancel(tp, cancel_flags);
2010 IRELE(ip);
2011
2012 XFS_QM_DQRELE(mp, udqp);
2013 XFS_QM_DQRELE(mp, gdqp);
2014
2015 goto std_return;
2016}
2017
2018#ifdef DEBUG
2019/*
2020 * Some counters to see if (and how often) we are hitting some deadlock
2021 * prevention code paths.
2022 */
2023
2024int xfs_rm_locks;
2025int xfs_rm_lock_delays;
2026int xfs_rm_attempts;
2027#endif
2028
2029/*
2030 * The following routine will lock the inodes associated with the
2031 * directory and the named entry in the directory. The locks are
2032 * acquired in increasing inode number.
2033 *
2034 * If the entry is "..", then only the directory is locked. The
2035 * vnode ref count will still include that from the .. entry in
2036 * this case.
2037 *
2038 * There is a deadlock we need to worry about. If the locked directory is
2039 * in the AIL, it might be blocking up the log. The next inode we lock
2040 * could be already locked by another thread waiting for log space (e.g
2041 * a permanent log reservation with a long running transaction (see
2042 * xfs_itruncate_finish)). To solve this, we must check if the directory
2043 * is in the ail and use lock_nowait. If we can't lock, we need to
2044 * drop the inode lock on the directory and try again. xfs_iunlock will
2045 * potentially push the tail if we were holding up the log.
2046 */
2047STATIC int
2048xfs_lock_dir_and_entry(
2049 xfs_inode_t *dp,
1da177e4
LT
2050 xfs_inode_t *ip) /* inode of entry 'name' */
2051{
2052 int attempts;
2053 xfs_ino_t e_inum;
2054 xfs_inode_t *ips[2];
2055 xfs_log_item_t *lp;
2056
2057#ifdef DEBUG
2058 xfs_rm_locks++;
2059#endif
2060 attempts = 0;
2061
2062again:
f7c66ce3 2063 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1da177e4
LT
2064
2065 e_inum = ip->i_ino;
2066
cf441eeb 2067 xfs_itrace_ref(ip);
1da177e4
LT
2068
2069 /*
2070 * We want to lock in increasing inum. Since we've already
2071 * acquired the lock on the directory, we may need to release
2072 * if if the inum of the entry turns out to be less.
2073 */
2074 if (e_inum > dp->i_ino) {
2075 /*
2076 * We are already in the right order, so just
2077 * lock on the inode of the entry.
2078 * We need to use nowait if dp is in the AIL.
2079 */
2080
2081 lp = (xfs_log_item_t *)dp->i_itemp;
2082 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2083 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2084 attempts++;
2085#ifdef DEBUG
2086 xfs_rm_attempts++;
2087#endif
2088
2089 /*
2090 * Unlock dp and try again.
2091 * xfs_iunlock will try to push the tail
2092 * if the inode is in the AIL.
2093 */
2094
2095 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2096
2097 if ((attempts % 5) == 0) {
2098 delay(1); /* Don't just spin the CPU */
2099#ifdef DEBUG
2100 xfs_rm_lock_delays++;
2101#endif
2102 }
2103 goto again;
2104 }
2105 } else {
2106 xfs_ilock(ip, XFS_ILOCK_EXCL);
2107 }
2108 } else if (e_inum < dp->i_ino) {
2109 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2110
2111 ips[0] = ip;
2112 ips[1] = dp;
2113 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2114 }
2115 /* else e_inum == dp->i_ino */
2116 /* This can happen if we're asked to lock /x/..
2117 * the entry is "..", which is also the parent directory.
2118 */
2119
2120 return 0;
2121}
2122
2123#ifdef DEBUG
2124int xfs_locked_n;
2125int xfs_small_retries;
2126int xfs_middle_retries;
2127int xfs_lots_retries;
2128int xfs_lock_delays;
2129#endif
2130
f7c66ce3
LM
2131/*
2132 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2133 * a different value
2134 */
2135static inline int
2136xfs_lock_inumorder(int lock_mode, int subclass)
2137{
2138 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
0f1145cc 2139 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
f7c66ce3 2140 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
0f1145cc 2141 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
f7c66ce3
LM
2142
2143 return lock_mode;
2144}
2145
1da177e4
LT
2146/*
2147 * The following routine will lock n inodes in exclusive mode.
2148 * We assume the caller calls us with the inodes in i_ino order.
2149 *
2150 * We need to detect deadlock where an inode that we lock
2151 * is in the AIL and we start waiting for another inode that is locked
2152 * by a thread in a long running transaction (such as truncate). This can
2153 * result in deadlock since the long running trans might need to wait
2154 * for the inode we just locked in order to push the tail and free space
2155 * in the log.
2156 */
2157void
2158xfs_lock_inodes(
2159 xfs_inode_t **ips,
2160 int inodes,
2161 int first_locked,
2162 uint lock_mode)
2163{
2164 int attempts = 0, i, j, try_lock;
2165 xfs_log_item_t *lp;
2166
2167 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2168
2169 if (first_locked) {
2170 try_lock = 1;
2171 i = 1;
2172 } else {
2173 try_lock = 0;
2174 i = 0;
2175 }
2176
2177again:
2178 for (; i < inodes; i++) {
2179 ASSERT(ips[i]);
2180
2181 if (i && (ips[i] == ips[i-1])) /* Already locked */
2182 continue;
2183
2184 /*
2185 * If try_lock is not set yet, make sure all locked inodes
2186 * are not in the AIL.
2187 * If any are, set try_lock to be used later.
2188 */
2189
2190 if (!try_lock) {
2191 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2192 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2193 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2194 try_lock++;
2195 }
2196 }
2197 }
2198
2199 /*
2200 * If any of the previous locks we have locked is in the AIL,
2201 * we must TRY to get the second and subsequent locks. If
2202 * we can't get any, we must release all we have
2203 * and try again.
2204 */
2205
2206 if (try_lock) {
2207 /* try_lock must be 0 if i is 0. */
2208 /*
2209 * try_lock means we have an inode locked
2210 * that is in the AIL.
2211 */
2212 ASSERT(i != 0);
f7c66ce3 2213 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1da177e4
LT
2214 attempts++;
2215
2216 /*
2217 * Unlock all previous guys and try again.
2218 * xfs_iunlock will try to push the tail
2219 * if the inode is in the AIL.
2220 */
2221
2222 for(j = i - 1; j >= 0; j--) {
2223
2224 /*
2225 * Check to see if we've already
2226 * unlocked this one.
2227 * Not the first one going back,
2228 * and the inode ptr is the same.
2229 */
2230 if ((j != (i - 1)) && ips[j] ==
2231 ips[j+1])
2232 continue;
2233
2234 xfs_iunlock(ips[j], lock_mode);
2235 }
2236
2237 if ((attempts % 5) == 0) {
2238 delay(1); /* Don't just spin the CPU */
2239#ifdef DEBUG
2240 xfs_lock_delays++;
2241#endif
2242 }
2243 i = 0;
2244 try_lock = 0;
2245 goto again;
2246 }
2247 } else {
f7c66ce3 2248 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1da177e4
LT
2249 }
2250 }
2251
2252#ifdef DEBUG
2253 if (attempts) {
2254 if (attempts < 5) xfs_small_retries++;
2255 else if (attempts < 100) xfs_middle_retries++;
2256 else xfs_lots_retries++;
2257 } else {
2258 xfs_locked_n++;
2259 }
2260#endif
2261}
2262
2263#ifdef DEBUG
2264#define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2265int remove_which_error_return = 0;
2266#else /* ! DEBUG */
2267#define REMOVE_DEBUG_TRACE(x)
2268#endif /* ! DEBUG */
2269
993386c1 2270int
1da177e4 2271xfs_remove(
993386c1
CH
2272 xfs_inode_t *dp,
2273 bhv_vname_t *dentry)
1da177e4 2274{
993386c1 2275 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1da177e4 2276 char *name = VNAME(dentry);
993386c1 2277 xfs_mount_t *mp = dp->i_mount;
43973964
CH
2278 xfs_inode_t *ip = VNAME_TO_INODE(dentry);
2279 int namelen = VNAMELEN(dentry);
1da177e4 2280 xfs_trans_t *tp = NULL;
1da177e4
LT
2281 int error = 0;
2282 xfs_bmap_free_t free_list;
2283 xfs_fsblock_t first_block;
2284 int cancel_flags;
2285 int committed;
1da177e4
LT
2286 int link_zero;
2287 uint resblks;
1da177e4 2288
cf441eeb 2289 xfs_itrace_entry(dp);
1da177e4 2290
1da177e4
LT
2291 if (XFS_FORCED_SHUTDOWN(mp))
2292 return XFS_ERROR(EIO);
2293
eb9df39d 2294 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1da177e4
LT
2295 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2296 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
43973964 2297 name, NULL, ip->i_d.di_mode, 0, 0);
1da177e4
LT
2298 if (error)
2299 return error;
2300 }
2301
1da177e4
LT
2302 /*
2303 * We need to get a reference to ip before we get our log
2304 * reservation. The reason for this is that we cannot call
2305 * xfs_iget for an inode for which we do not have a reference
2306 * once we've acquired a log reservation. This is because the
2307 * inode we are trying to get might be in xfs_inactive going
2308 * for a log reservation. Since we'll have to wait for the
2309 * inactive code to complete before returning from xfs_iget,
2310 * we need to make sure that we don't have log space reserved
c41564b5 2311 * when we call xfs_iget. Instead we get an unlocked reference
1da177e4
LT
2312 * to the inode before getting our log reservation.
2313 */
43973964 2314 IHOLD(ip);
1da177e4 2315
cf441eeb
LM
2316 xfs_itrace_entry(ip);
2317 xfs_itrace_ref(ip);
1da177e4
LT
2318
2319 error = XFS_QM_DQATTACH(mp, dp, 0);
2320 if (!error && dp != ip)
2321 error = XFS_QM_DQATTACH(mp, ip, 0);
2322 if (error) {
2323 REMOVE_DEBUG_TRACE(__LINE__);
2324 IRELE(ip);
2325 goto std_return;
2326 }
2327
2328 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2329 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2330 /*
2331 * We try to get the real space reservation first,
2332 * allowing for directory btree deletion(s) implying
2333 * possible bmap insert(s). If we can't get the space
2334 * reservation then we use 0 instead, and avoid the bmap
2335 * btree insert(s) in the directory code by, if the bmap
2336 * insert tries to happen, instead trimming the LAST
2337 * block from the directory.
2338 */
2339 resblks = XFS_REMOVE_SPACE_RES(mp);
2340 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2341 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2342 if (error == ENOSPC) {
2343 resblks = 0;
2344 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2345 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2346 }
2347 if (error) {
2348 ASSERT(error != ENOSPC);
2349 REMOVE_DEBUG_TRACE(__LINE__);
2350 xfs_trans_cancel(tp, 0);
2351 IRELE(ip);
2352 return error;
2353 }
2354
e9ed9d22 2355 error = xfs_lock_dir_and_entry(dp, ip);
1da177e4
LT
2356 if (error) {
2357 REMOVE_DEBUG_TRACE(__LINE__);
2358 xfs_trans_cancel(tp, cancel_flags);
2359 IRELE(ip);
2360 goto std_return;
2361 }
2362
2363 /*
2364 * At this point, we've gotten both the directory and the entry
2365 * inodes locked.
2366 */
2367 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2368 if (dp != ip) {
2369 /*
2370 * Increment vnode ref count only in this case since
2371 * there's an extra vnode reference in the case where
2372 * dp == ip.
2373 */
2374 IHOLD(dp);
2375 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2376 }
2377
2378 /*
2379 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2380 */
2381 XFS_BMAP_INIT(&free_list, &first_block);
f6c2d1fa
NS
2382 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2383 &first_block, &free_list, 0);
1da177e4
LT
2384 if (error) {
2385 ASSERT(error != ENOENT);
2386 REMOVE_DEBUG_TRACE(__LINE__);
2387 goto error1;
2388 }
2389 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2390
2391 dp->i_gen++;
2392 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2393
2394 error = xfs_droplink(tp, ip);
2395 if (error) {
2396 REMOVE_DEBUG_TRACE(__LINE__);
2397 goto error1;
2398 }
2399
2400 /* Determine if this is the last link while
2401 * we are in the transaction.
2402 */
2403 link_zero = (ip)->i_d.di_nlink==0;
2404
2405 /*
2406 * Take an extra ref on the inode so that it doesn't
2407 * go to xfs_inactive() from within the commit.
2408 */
2409 IHOLD(ip);
2410
2411 /*
2412 * If this is a synchronous mount, make sure that the
2413 * remove transaction goes to disk before returning to
2414 * the user.
2415 */
2416 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2417 xfs_trans_set_sync(tp);
2418 }
2419
f7c99b6f 2420 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2421 if (error) {
2422 REMOVE_DEBUG_TRACE(__LINE__);
2423 goto error_rele;
2424 }
2425
1c72bf90 2426 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2427 if (error) {
2428 IRELE(ip);
2429 goto std_return;
2430 }
2431
2a82b8be
DC
2432 /*
2433 * If we are using filestreams, kill the stream association.
2434 * If the file is still open it may get a new one but that
2435 * will get killed on last close in xfs_close() so we don't
2436 * have to worry about that.
2437 */
2438 if (link_zero && xfs_inode_is_filestream(ip))
2439 xfs_filestream_deassociate(ip);
2440
cf441eeb 2441 xfs_itrace_exit(ip);
1da177e4
LT
2442 IRELE(ip);
2443
2444/* Fall through to std_return with error = 0 */
2445 std_return:
eb9df39d 2446 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
1da177e4
LT
2447 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2448 dir_vp, DM_RIGHT_NULL,
2449 NULL, DM_RIGHT_NULL,
43973964 2450 name, NULL, ip->i_d.di_mode, error, 0);
1da177e4
LT
2451 }
2452 return error;
2453
2454 error1:
2455 xfs_bmap_cancel(&free_list);
2456 cancel_flags |= XFS_TRANS_ABORT;
2457 xfs_trans_cancel(tp, cancel_flags);
2458 goto std_return;
2459
2460 error_rele:
2461 /*
2462 * In this case make sure to not release the inode until after
2463 * the current transaction is aborted. Releasing it beforehand
2464 * can cause us to go to xfs_inactive and start a recursive
2465 * transaction which can easily deadlock with the current one.
2466 */
2467 xfs_bmap_cancel(&free_list);
2468 cancel_flags |= XFS_TRANS_ABORT;
2469 xfs_trans_cancel(tp, cancel_flags);
2470
1da177e4
LT
2471 IRELE(ip);
2472
2473 goto std_return;
2474}
2475
993386c1 2476int
1da177e4 2477xfs_link(
993386c1 2478 xfs_inode_t *tdp,
67fcaa73 2479 bhv_vnode_t *src_vp,
993386c1 2480 bhv_vname_t *dentry)
1da177e4 2481{
993386c1
CH
2482 bhv_vnode_t *target_dir_vp = XFS_ITOV(tdp);
2483 xfs_mount_t *mp = tdp->i_mount;
2484 xfs_inode_t *sip = xfs_vtoi(src_vp);
1da177e4 2485 xfs_trans_t *tp;
1da177e4
LT
2486 xfs_inode_t *ips[2];
2487 int error;
2488 xfs_bmap_free_t free_list;
2489 xfs_fsblock_t first_block;
2490 int cancel_flags;
2491 int committed;
1da177e4
LT
2492 int resblks;
2493 char *target_name = VNAME(dentry);
2494 int target_namelen;
2495
cf441eeb
LM
2496 xfs_itrace_entry(tdp);
2497 xfs_itrace_entry(xfs_vtoi(src_vp));
1da177e4
LT
2498
2499 target_namelen = VNAMELEN(dentry);
b71d300c 2500 ASSERT(!VN_ISDIR(src_vp));
1da177e4 2501
1da177e4
LT
2502 if (XFS_FORCED_SHUTDOWN(mp))
2503 return XFS_ERROR(EIO);
2504
eb9df39d 2505 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
1da177e4
LT
2506 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2507 target_dir_vp, DM_RIGHT_NULL,
2508 src_vp, DM_RIGHT_NULL,
2509 target_name, NULL, 0, 0, 0);
2510 if (error)
2511 return error;
2512 }
2513
2514 /* Return through std_return after this point. */
2515
2516 error = XFS_QM_DQATTACH(mp, sip, 0);
2517 if (!error && sip != tdp)
2518 error = XFS_QM_DQATTACH(mp, tdp, 0);
2519 if (error)
2520 goto std_return;
2521
2522 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2523 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2524 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2525 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2526 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2527 if (error == ENOSPC) {
2528 resblks = 0;
2529 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2530 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2531 }
2532 if (error) {
2533 cancel_flags = 0;
2534 goto error_return;
2535 }
2536
2537 if (sip->i_ino < tdp->i_ino) {
2538 ips[0] = sip;
2539 ips[1] = tdp;
2540 } else {
2541 ips[0] = tdp;
2542 ips[1] = sip;
2543 }
2544
2545 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2546
2547 /*
2548 * Increment vnode ref counts since xfs_trans_commit &
2549 * xfs_trans_cancel will both unlock the inodes and
2550 * decrement the associated ref counts.
2551 */
2552 VN_HOLD(src_vp);
2553 VN_HOLD(target_dir_vp);
2554 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2555 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2556
2557 /*
2558 * If the source has too many links, we can't make any more to it.
2559 */
2560 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2561 error = XFS_ERROR(EMLINK);
2562 goto error_return;
2563 }
2564
365ca83d
NS
2565 /*
2566 * If we are using project inheritance, we only allow hard link
2567 * creation in our tree when the project IDs are the same; else
2568 * the tree quota mechanism could be circumvented.
2569 */
2570 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2571 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
b1ecdda9 2572 error = XFS_ERROR(EXDEV);
365ca83d
NS
2573 goto error_return;
2574 }
2575
1da177e4 2576 if (resblks == 0 &&
f6c2d1fa 2577 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
1da177e4
LT
2578 goto error_return;
2579
2580 XFS_BMAP_INIT(&free_list, &first_block);
2581
f6c2d1fa 2582 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
1da177e4
LT
2583 sip->i_ino, &first_block, &free_list,
2584 resblks);
2585 if (error)
2586 goto abort_return;
2587 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2588 tdp->i_gen++;
2589 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2590
2591 error = xfs_bumplink(tp, sip);
b71d300c 2592 if (error)
1da177e4 2593 goto abort_return;
1da177e4
LT
2594
2595 /*
2596 * If this is a synchronous mount, make sure that the
2597 * link transaction goes to disk before returning to
2598 * the user.
2599 */
2600 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2601 xfs_trans_set_sync(tp);
2602 }
2603
f7c99b6f 2604 error = xfs_bmap_finish (&tp, &free_list, &committed);
1da177e4
LT
2605 if (error) {
2606 xfs_bmap_cancel(&free_list);
2607 goto abort_return;
2608 }
2609
1c72bf90 2610 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
b71d300c 2611 if (error)
1da177e4 2612 goto std_return;
1da177e4
LT
2613
2614 /* Fall through to std_return with error = 0. */
2615std_return:
eb9df39d 2616 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
1da177e4
LT
2617 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2618 target_dir_vp, DM_RIGHT_NULL,
2619 src_vp, DM_RIGHT_NULL,
2620 target_name, NULL, 0, error, 0);
2621 }
2622 return error;
2623
2624 abort_return:
2625 cancel_flags |= XFS_TRANS_ABORT;
2626 /* FALLTHROUGH */
014c2544 2627
1da177e4
LT
2628 error_return:
2629 xfs_trans_cancel(tp, cancel_flags);
1da177e4
LT
2630 goto std_return;
2631}
b71d300c
AD
2632
2633
993386c1 2634int
1da177e4 2635xfs_mkdir(
993386c1 2636 xfs_inode_t *dp,
8285fb58 2637 bhv_vname_t *dentry,
3e5daf05 2638 mode_t mode,
67fcaa73 2639 bhv_vnode_t **vpp,
1da177e4
LT
2640 cred_t *credp)
2641{
993386c1 2642 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1da177e4 2643 char *dir_name = VNAME(dentry);
993386c1
CH
2644 int dir_namelen = VNAMELEN(dentry);
2645 xfs_mount_t *mp = dp->i_mount;
1da177e4 2646 xfs_inode_t *cdp; /* inode of created dir */
67fcaa73 2647 bhv_vnode_t *cvp; /* vnode of created dir */
1da177e4 2648 xfs_trans_t *tp;
1da177e4
LT
2649 int cancel_flags;
2650 int error;
2651 int committed;
2652 xfs_bmap_free_t free_list;
2653 xfs_fsblock_t first_block;
993386c1 2654 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
2655 boolean_t created = B_FALSE;
2656 int dm_event_sent = 0;
2657 xfs_prid_t prid;
2658 struct xfs_dquot *udqp, *gdqp;
2659 uint resblks;
1da177e4
LT
2660
2661 if (XFS_FORCED_SHUTDOWN(mp))
2662 return XFS_ERROR(EIO);
2663
1da177e4 2664 tp = NULL;
1da177e4 2665
eb9df39d 2666 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1da177e4
LT
2667 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2668 dir_vp, DM_RIGHT_NULL, NULL,
2669 DM_RIGHT_NULL, dir_name, NULL,
3e5daf05 2670 mode, 0, 0);
1da177e4
LT
2671 if (error)
2672 return error;
2673 dm_event_sent = 1;
2674 }
2675
2676 /* Return through std_return after this point. */
2677
cf441eeb 2678 xfs_itrace_entry(dp);
1da177e4
LT
2679
2680 mp = dp->i_mount;
2681 udqp = gdqp = NULL;
365ca83d
NS
2682 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2683 prid = dp->i_d.di_projid;
1da177e4
LT
2684 else
2685 prid = (xfs_prid_t)dfltprid;
2686
2687 /*
2688 * Make sure that we have allocated dquot(s) on disk.
2689 */
2690 error = XFS_QM_DQVOPALLOC(mp, dp,
c8ad20ff 2691 current_fsuid(credp), current_fsgid(credp), prid,
1da177e4
LT
2692 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2693 if (error)
2694 goto std_return;
2695
2696 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2697 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2698 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2699 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2700 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2701 if (error == ENOSPC) {
2702 resblks = 0;
2703 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2704 XFS_TRANS_PERM_LOG_RES,
2705 XFS_MKDIR_LOG_COUNT);
2706 }
2707 if (error) {
2708 cancel_flags = 0;
1da177e4
LT
2709 goto error_return;
2710 }
2711
f7c66ce3 2712 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 2713 unlock_dp_on_error = B_TRUE;
1da177e4
LT
2714
2715 /*
2716 * Check for directory link count overflow.
2717 */
2718 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2719 error = XFS_ERROR(EMLINK);
2720 goto error_return;
2721 }
2722
2723 /*
2724 * Reserve disk quota and the inode.
2725 */
2726 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2727 if (error)
2728 goto error_return;
2729
2730 if (resblks == 0 &&
f6c2d1fa 2731 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
1da177e4
LT
2732 goto error_return;
2733 /*
2734 * create the directory inode.
2735 */
3e5daf05 2736 error = xfs_dir_ialloc(&tp, dp, mode, 2,
1da177e4
LT
2737 0, credp, prid, resblks > 0,
2738 &cdp, NULL);
2739 if (error) {
2740 if (error == ENOSPC)
2741 goto error_return;
2742 goto abort_return;
2743 }
cf441eeb 2744 xfs_itrace_ref(cdp);
1da177e4
LT
2745
2746 /*
2747 * Now we add the directory inode to the transaction.
2748 * We waited until now since xfs_dir_ialloc might start
2749 * a new transaction. Had we joined the transaction
993386c1
CH
2750 * earlier, the locks might have gotten released. An error
2751 * from here on will result in the transaction cancel
2752 * unlocking dp so don't do it explicitly in the error path.
1da177e4
LT
2753 */
2754 VN_HOLD(dir_vp);
2755 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 2756 unlock_dp_on_error = B_FALSE;
1da177e4
LT
2757
2758 XFS_BMAP_INIT(&free_list, &first_block);
2759
f6c2d1fa
NS
2760 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2761 &first_block, &free_list, resblks ?
2762 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1da177e4
LT
2763 if (error) {
2764 ASSERT(error != ENOSPC);
2765 goto error1;
2766 }
2767 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2768
2769 /*
2770 * Bump the in memory version number of the parent directory
2771 * so that other processes accessing it will recognize that
2772 * the directory has changed.
2773 */
2774 dp->i_gen++;
2775
f6c2d1fa
NS
2776 error = xfs_dir_init(tp, cdp, dp);
2777 if (error)
1da177e4 2778 goto error2;
1da177e4
LT
2779
2780 cdp->i_gen = 1;
2781 error = xfs_bumplink(tp, dp);
f6c2d1fa 2782 if (error)
1da177e4 2783 goto error2;
1da177e4
LT
2784
2785 cvp = XFS_ITOV(cdp);
2786
2787 created = B_TRUE;
2788
2789 *vpp = cvp;
2790 IHOLD(cdp);
2791
2792 /*
2793 * Attach the dquots to the new inode and modify the icount incore.
2794 */
2795 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2796
2797 /*
2798 * If this is a synchronous mount, make sure that the
2799 * mkdir transaction goes to disk before returning to
2800 * the user.
2801 */
2802 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2803 xfs_trans_set_sync(tp);
2804 }
2805
f7c99b6f 2806 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
2807 if (error) {
2808 IRELE(cdp);
2809 goto error2;
2810 }
2811
1c72bf90 2812 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
2813 XFS_QM_DQRELE(mp, udqp);
2814 XFS_QM_DQRELE(mp, gdqp);
2815 if (error) {
2816 IRELE(cdp);
2817 }
2818
2819 /* Fall through to std_return with error = 0 or errno from
2820 * xfs_trans_commit. */
2821
2822std_return:
eb9df39d 2823 if ((created || (error != 0 && dm_event_sent != 0)) &&
993386c1 2824 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1da177e4
LT
2825 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2826 dir_vp, DM_RIGHT_NULL,
2827 created ? XFS_ITOV(cdp):NULL,
2828 DM_RIGHT_NULL,
2829 dir_name, NULL,
3e5daf05 2830 mode, error, 0);
1da177e4
LT
2831 }
2832 return error;
2833
2834 error2:
2835 error1:
2836 xfs_bmap_cancel(&free_list);
2837 abort_return:
2838 cancel_flags |= XFS_TRANS_ABORT;
2839 error_return:
2840 xfs_trans_cancel(tp, cancel_flags);
2841 XFS_QM_DQRELE(mp, udqp);
2842 XFS_QM_DQRELE(mp, gdqp);
2843
993386c1 2844 if (unlock_dp_on_error)
1da177e4 2845 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1da177e4
LT
2846
2847 goto std_return;
2848}
2849
993386c1 2850int
1da177e4 2851xfs_rmdir(
993386c1
CH
2852 xfs_inode_t *dp,
2853 bhv_vname_t *dentry)
1da177e4 2854{
993386c1 2855 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
1da177e4 2856 char *name = VNAME(dentry);
993386c1
CH
2857 int namelen = VNAMELEN(dentry);
2858 xfs_mount_t *mp = dp->i_mount;
43973964 2859 xfs_inode_t *cdp = VNAME_TO_INODE(dentry);
1da177e4 2860 xfs_trans_t *tp;
1da177e4
LT
2861 int error;
2862 xfs_bmap_free_t free_list;
2863 xfs_fsblock_t first_block;
2864 int cancel_flags;
2865 int committed;
1da177e4 2866 int last_cdp_link;
1da177e4
LT
2867 uint resblks;
2868
cf441eeb 2869 xfs_itrace_entry(dp);
1da177e4 2870
993386c1 2871 if (XFS_FORCED_SHUTDOWN(mp))
1da177e4 2872 return XFS_ERROR(EIO);
1da177e4 2873
eb9df39d 2874 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1da177e4
LT
2875 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2876 dir_vp, DM_RIGHT_NULL,
2877 NULL, DM_RIGHT_NULL,
43973964 2878 name, NULL, cdp->i_d.di_mode, 0, 0);
1da177e4
LT
2879 if (error)
2880 return XFS_ERROR(error);
2881 }
2882
1da177e4
LT
2883 /*
2884 * We need to get a reference to cdp before we get our log
2885 * reservation. The reason for this is that we cannot call
2886 * xfs_iget for an inode for which we do not have a reference
2887 * once we've acquired a log reservation. This is because the
2888 * inode we are trying to get might be in xfs_inactive going
2889 * for a log reservation. Since we'll have to wait for the
2890 * inactive code to complete before returning from xfs_iget,
2891 * we need to make sure that we don't have log space reserved
c41564b5 2892 * when we call xfs_iget. Instead we get an unlocked reference
1da177e4
LT
2893 * to the inode before getting our log reservation.
2894 */
43973964 2895 IHOLD(cdp);
1da177e4
LT
2896
2897 /*
2898 * Get the dquots for the inodes.
2899 */
2900 error = XFS_QM_DQATTACH(mp, dp, 0);
2901 if (!error && dp != cdp)
2902 error = XFS_QM_DQATTACH(mp, cdp, 0);
2903 if (error) {
2904 IRELE(cdp);
2905 REMOVE_DEBUG_TRACE(__LINE__);
2906 goto std_return;
2907 }
2908
2909 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2910 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2911 /*
2912 * We try to get the real space reservation first,
2913 * allowing for directory btree deletion(s) implying
2914 * possible bmap insert(s). If we can't get the space
2915 * reservation then we use 0 instead, and avoid the bmap
2916 * btree insert(s) in the directory code by, if the bmap
2917 * insert tries to happen, instead trimming the LAST
2918 * block from the directory.
2919 */
2920 resblks = XFS_REMOVE_SPACE_RES(mp);
2921 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2922 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2923 if (error == ENOSPC) {
2924 resblks = 0;
2925 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2926 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2927 }
2928 if (error) {
2929 ASSERT(error != ENOSPC);
2930 cancel_flags = 0;
2931 IRELE(cdp);
2932 goto error_return;
2933 }
2934 XFS_BMAP_INIT(&free_list, &first_block);
2935
2936 /*
2937 * Now lock the child directory inode and the parent directory
2938 * inode in the proper order. This will take care of validating
2939 * that the directory entry for the child directory inode has
2940 * not changed while we were obtaining a log reservation.
2941 */
e9ed9d22 2942 error = xfs_lock_dir_and_entry(dp, cdp);
1da177e4
LT
2943 if (error) {
2944 xfs_trans_cancel(tp, cancel_flags);
2945 IRELE(cdp);
2946 goto std_return;
2947 }
2948
2949 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2950 if (dp != cdp) {
2951 /*
2952 * Only increment the parent directory vnode count if
2953 * we didn't bump it in looking up cdp. The only time
2954 * we don't bump it is when we're looking up ".".
2955 */
2956 VN_HOLD(dir_vp);
2957 }
2958
cf441eeb 2959 xfs_itrace_ref(cdp);
1da177e4
LT
2960 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2961
2962 ASSERT(cdp->i_d.di_nlink >= 2);
2963 if (cdp->i_d.di_nlink != 2) {
2964 error = XFS_ERROR(ENOTEMPTY);
2965 goto error_return;
2966 }
f6c2d1fa 2967 if (!xfs_dir_isempty(cdp)) {
1da177e4
LT
2968 error = XFS_ERROR(ENOTEMPTY);
2969 goto error_return;
2970 }
2971
f6c2d1fa
NS
2972 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
2973 &first_block, &free_list, resblks);
2974 if (error)
1da177e4 2975 goto error1;
1da177e4
LT
2976
2977 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2978
2979 /*
2980 * Bump the in memory generation count on the parent
2981 * directory so that other can know that it has changed.
2982 */
2983 dp->i_gen++;
2984
2985 /*
2986 * Drop the link from cdp's "..".
2987 */
2988 error = xfs_droplink(tp, dp);
2989 if (error) {
2990 goto error1;
2991 }
2992
2993 /*
2994 * Drop the link from dp to cdp.
2995 */
2996 error = xfs_droplink(tp, cdp);
2997 if (error) {
2998 goto error1;
2999 }
3000
3001 /*
3002 * Drop the "." link from cdp to self.
3003 */
3004 error = xfs_droplink(tp, cdp);
3005 if (error) {
3006 goto error1;
3007 }
3008
3009 /* Determine these before committing transaction */
3010 last_cdp_link = (cdp)->i_d.di_nlink==0;
3011
3012 /*
3013 * Take an extra ref on the child vnode so that it
3014 * does not go to xfs_inactive() from within the commit.
3015 */
3016 IHOLD(cdp);
3017
3018 /*
3019 * If this is a synchronous mount, make sure that the
3020 * rmdir transaction goes to disk before returning to
3021 * the user.
3022 */
3023 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3024 xfs_trans_set_sync(tp);
3025 }
3026
f7c99b6f 3027 error = xfs_bmap_finish (&tp, &free_list, &committed);
1da177e4
LT
3028 if (error) {
3029 xfs_bmap_cancel(&free_list);
3030 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3031 XFS_TRANS_ABORT));
3032 IRELE(cdp);
3033 goto std_return;
3034 }
3035
1c72bf90 3036 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
3037 if (error) {
3038 IRELE(cdp);
3039 goto std_return;
3040 }
3041
3042
1da177e4
LT
3043 IRELE(cdp);
3044
3045 /* Fall through to std_return with error = 0 or the errno
3046 * from xfs_trans_commit. */
bb19fba1 3047 std_return:
eb9df39d 3048 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
1da177e4
LT
3049 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3050 dir_vp, DM_RIGHT_NULL,
3051 NULL, DM_RIGHT_NULL,
43973964 3052 name, NULL, cdp->i_d.di_mode,
1da177e4
LT
3053 error, 0);
3054 }
3055 return error;
3056
bb19fba1 3057 error1:
1da177e4
LT
3058 xfs_bmap_cancel(&free_list);
3059 cancel_flags |= XFS_TRANS_ABORT;
014c2544
JJ
3060 /* FALLTHROUGH */
3061
bb19fba1 3062 error_return:
1da177e4
LT
3063 xfs_trans_cancel(tp, cancel_flags);
3064 goto std_return;
3065}
3066
993386c1 3067int
1da177e4 3068xfs_symlink(
993386c1 3069 xfs_inode_t *dp,
8285fb58 3070 bhv_vname_t *dentry,
1da177e4 3071 char *target_path,
3e5daf05 3072 mode_t mode,
67fcaa73 3073 bhv_vnode_t **vpp,
1da177e4
LT
3074 cred_t *credp)
3075{
993386c1
CH
3076 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
3077 xfs_mount_t *mp = dp->i_mount;
1da177e4 3078 xfs_trans_t *tp;
1da177e4
LT
3079 xfs_inode_t *ip;
3080 int error;
3081 int pathlen;
3082 xfs_bmap_free_t free_list;
3083 xfs_fsblock_t first_block;
993386c1 3084 boolean_t unlock_dp_on_error = B_FALSE;
1da177e4
LT
3085 uint cancel_flags;
3086 int committed;
3087 xfs_fileoff_t first_fsb;
3088 xfs_filblks_t fs_blocks;
3089 int nmaps;
3090 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3091 xfs_daddr_t d;
3092 char *cur_chunk;
3093 int byte_cnt;
3094 int n;
3095 xfs_buf_t *bp;
3096 xfs_prid_t prid;
3097 struct xfs_dquot *udqp, *gdqp;
3098 uint resblks;
3099 char *link_name = VNAME(dentry);
3100 int link_namelen;
3101
3102 *vpp = NULL;
1da177e4
LT
3103 error = 0;
3104 ip = NULL;
3105 tp = NULL;
3106
cf441eeb 3107 xfs_itrace_entry(dp);
1da177e4
LT
3108
3109 if (XFS_FORCED_SHUTDOWN(mp))
3110 return XFS_ERROR(EIO);
3111
3112 link_namelen = VNAMELEN(dentry);
3113
3114 /*
3115 * Check component lengths of the target path name.
3116 */
3117 pathlen = strlen(target_path);
3118 if (pathlen >= MAXPATHLEN) /* total string too long */
3119 return XFS_ERROR(ENAMETOOLONG);
3120 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3121 int len, total;
3122 char *path;
3123
f6c2d1fa 3124 for (total = 0, path = target_path; total < pathlen;) {
1da177e4
LT
3125 /*
3126 * Skip any slashes.
3127 */
3128 while(*path == '/') {
3129 total++;
3130 path++;
3131 }
3132
3133 /*
3134 * Count up to the next slash or end of path.
3135 * Error out if the component is bigger than MAXNAMELEN.
3136 */
3137 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3138 if (++len >= MAXNAMELEN) {
3139 error = ENAMETOOLONG;
3140 return error;
3141 }
3142 }
3143 }
3144 }
3145
eb9df39d 3146 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
1da177e4
LT
3147 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3148 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3149 link_name, target_path, 0, 0, 0);
3150 if (error)
3151 return error;
3152 }
3153
3154 /* Return through std_return after this point. */
3155
3156 udqp = gdqp = NULL;
365ca83d
NS
3157 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3158 prid = dp->i_d.di_projid;
1da177e4
LT
3159 else
3160 prid = (xfs_prid_t)dfltprid;
3161
3162 /*
3163 * Make sure that we have allocated dquot(s) on disk.
3164 */
3165 error = XFS_QM_DQVOPALLOC(mp, dp,
c8ad20ff 3166 current_fsuid(credp), current_fsgid(credp), prid,
1da177e4
LT
3167 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3168 if (error)
3169 goto std_return;
3170
3171 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3172 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3173 /*
3174 * The symlink will fit into the inode data fork?
3175 * There can't be any attributes so we get the whole variable part.
3176 */
3177 if (pathlen <= XFS_LITINO(mp))
3178 fs_blocks = 0;
3179 else
3180 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3181 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3182 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3183 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3184 if (error == ENOSPC && fs_blocks == 0) {
3185 resblks = 0;
3186 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3187 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3188 }
3189 if (error) {
3190 cancel_flags = 0;
1da177e4
LT
3191 goto error_return;
3192 }
3193
f7c66ce3 3194 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
993386c1 3195 unlock_dp_on_error = B_TRUE;
1da177e4
LT
3196
3197 /*
3198 * Check whether the directory allows new symlinks or not.
3199 */
3200 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3201 error = XFS_ERROR(EPERM);
3202 goto error_return;
3203 }
3204
3205 /*
3206 * Reserve disk quota : blocks and inode.
3207 */
3208 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3209 if (error)
3210 goto error_return;
3211
3212 /*
3213 * Check for ability to enter directory entry, if no space reserved.
3214 */
3215 if (resblks == 0 &&
f6c2d1fa 3216 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
1da177e4
LT
3217 goto error_return;
3218 /*
3219 * Initialize the bmap freelist prior to calling either
3220 * bmapi or the directory create code.
3221 */
3222 XFS_BMAP_INIT(&free_list, &first_block);
3223
3224 /*
3225 * Allocate an inode for the symlink.
3226 */
3e5daf05 3227 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
1da177e4
LT
3228 1, 0, credp, prid, resblks > 0, &ip, NULL);
3229 if (error) {
3230 if (error == ENOSPC)
3231 goto error_return;
3232 goto error1;
3233 }
cf441eeb 3234 xfs_itrace_ref(ip);
1da177e4 3235
993386c1
CH
3236 /*
3237 * An error after we've joined dp to the transaction will result in the
3238 * transaction cancel unlocking dp so don't do it explicitly in the
3239 * error path.
3240 */
1da177e4
LT
3241 VN_HOLD(dir_vp);
3242 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
993386c1 3243 unlock_dp_on_error = B_FALSE;
1da177e4
LT
3244
3245 /*
3246 * Also attach the dquot(s) to it, if applicable.
3247 */
3248 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3249
3250 if (resblks)
3251 resblks -= XFS_IALLOC_SPACE_RES(mp);
3252 /*
3253 * If the symlink will fit into the inode, write it inline.
3254 */
3255 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3256 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3257 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3258 ip->i_d.di_size = pathlen;
3259
3260 /*
3261 * The inode was initially created in extent format.
3262 */
3263 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3264 ip->i_df.if_flags |= XFS_IFINLINE;
3265
3266 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3267 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3268
3269 } else {
3270 first_fsb = 0;
3271 nmaps = SYMLINK_MAPS;
3272
3273 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3274 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3275 &first_block, resblks, mval, &nmaps,
3e57ecf6 3276 &free_list, NULL);
1da177e4
LT
3277 if (error) {
3278 goto error1;
3279 }
3280
3281 if (resblks)
3282 resblks -= fs_blocks;
3283 ip->i_d.di_size = pathlen;
3284 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3285
3286 cur_chunk = target_path;
3287 for (n = 0; n < nmaps; n++) {
3288 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3289 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3290 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3291 BTOBB(byte_cnt), 0);
3292 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3293 if (pathlen < byte_cnt) {
3294 byte_cnt = pathlen;
3295 }
3296 pathlen -= byte_cnt;
3297
3298 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3299 cur_chunk += byte_cnt;
3300
3301 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3302 }
3303 }
3304
3305 /*
3306 * Create the directory entry for the symlink.
3307 */
f6c2d1fa
NS
3308 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3309 &first_block, &free_list, resblks);
3310 if (error)
1da177e4 3311 goto error1;
1da177e4
LT
3312 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3313 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3314
3315 /*
3316 * Bump the in memory version number of the parent directory
3317 * so that other processes accessing it will recognize that
3318 * the directory has changed.
3319 */
3320 dp->i_gen++;
3321
3322 /*
3323 * If this is a synchronous mount, make sure that the
3324 * symlink transaction goes to disk before returning to
3325 * the user.
3326 */
3327 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3328 xfs_trans_set_sync(tp);
3329 }
3330
3331 /*
3332 * xfs_trans_commit normally decrements the vnode ref count
3333 * when it unlocks the inode. Since we want to return the
3334 * vnode to the caller, we bump the vnode ref count now.
3335 */
3336 IHOLD(ip);
3337
f7c99b6f 3338 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
3339 if (error) {
3340 goto error2;
3341 }
1c72bf90 3342 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
3343 XFS_QM_DQRELE(mp, udqp);
3344 XFS_QM_DQRELE(mp, gdqp);
3345
3346 /* Fall through to std_return with error = 0 or errno from
3347 * xfs_trans_commit */
3348std_return:
993386c1 3349 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
1da177e4
LT
3350 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3351 dir_vp, DM_RIGHT_NULL,
3352 error ? NULL : XFS_ITOV(ip),
3353 DM_RIGHT_NULL, link_name, target_path,
3354 0, error, 0);
3355 }
3356
3357 if (!error) {
67fcaa73 3358 bhv_vnode_t *vp;
1da177e4
LT
3359
3360 ASSERT(ip);
3361 vp = XFS_ITOV(ip);
3362 *vpp = vp;
3363 }
3364 return error;
3365
3366 error2:
3367 IRELE(ip);
3368 error1:
3369 xfs_bmap_cancel(&free_list);
3370 cancel_flags |= XFS_TRANS_ABORT;
3371 error_return:
3372 xfs_trans_cancel(tp, cancel_flags);
3373 XFS_QM_DQRELE(mp, udqp);
3374 XFS_QM_DQRELE(mp, gdqp);
3375
993386c1 3376 if (unlock_dp_on_error)
1da177e4 3377 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1da177e4
LT
3378
3379 goto std_return;
3380}
3381
993386c1 3382int
1da177e4 3383xfs_inode_flush(
993386c1 3384 xfs_inode_t *ip,
1da177e4
LT
3385 int flags)
3386{
993386c1 3387 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
3388 int error = 0;
3389
1da177e4
LT
3390 if (XFS_FORCED_SHUTDOWN(mp))
3391 return XFS_ERROR(EIO);
3392
3393 /*
3394 * Bypass inodes which have already been cleaned by
3395 * the inode flush clustering code inside xfs_iflush
3396 */
33540408 3397 if (xfs_inode_clean(ip))
1da177e4
LT
3398 return 0;
3399
1da177e4
LT
3400 /*
3401 * We make this non-blocking if the inode is contended,
3402 * return EAGAIN to indicate to the caller that they
3403 * did not succeed. This prevents the flush path from
3404 * blocking on inodes inside another operation right
3405 * now, they get caught later by xfs_sync.
3406 */
a3f74ffb
DC
3407 if (flags & FLUSH_SYNC) {
3408 xfs_ilock(ip, XFS_ILOCK_SHARED);
3409 xfs_iflock(ip);
3410 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3411 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3412 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1da177e4
LT
3413 return EAGAIN;
3414 }
a3f74ffb
DC
3415 } else {
3416 return EAGAIN;
1da177e4
LT
3417 }
3418
a3f74ffb
DC
3419 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3420 : XFS_IFLUSH_ASYNC_NOBLOCK);
3421 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3422
1da177e4
LT
3423 return error;
3424}
3425
993386c1 3426
1da177e4 3427int
993386c1
CH
3428xfs_set_dmattrs(
3429 xfs_inode_t *ip,
1da177e4 3430 u_int evmask,
993386c1 3431 u_int16_t state)
1da177e4 3432{
993386c1 3433 xfs_mount_t *mp = ip->i_mount;
1da177e4 3434 xfs_trans_t *tp;
1da177e4
LT
3435 int error;
3436
3437 if (!capable(CAP_SYS_ADMIN))
3438 return XFS_ERROR(EPERM);
3439
1da177e4
LT
3440 if (XFS_FORCED_SHUTDOWN(mp))
3441 return XFS_ERROR(EIO);
3442
3443 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3444 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3445 if (error) {
3446 xfs_trans_cancel(tp, 0);
3447 return error;
3448 }
3449 xfs_ilock(ip, XFS_ILOCK_EXCL);
3450 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3451
613d7043
CH
3452 ip->i_d.di_dmevmask = evmask;
3453 ip->i_d.di_dmstate = state;
1da177e4
LT
3454
3455 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3456 IHOLD(ip);
1c72bf90 3457 error = xfs_trans_commit(tp, 0);
1da177e4
LT
3458
3459 return error;
3460}
3461
993386c1 3462int
1da177e4 3463xfs_reclaim(
993386c1 3464 xfs_inode_t *ip)
1da177e4 3465{
993386c1 3466 bhv_vnode_t *vp = XFS_ITOV(ip);
1da177e4 3467
cf441eeb 3468 xfs_itrace_entry(ip);
1da177e4
LT
3469
3470 ASSERT(!VN_MAPPED(vp));
3471
3472 /* bad inode, get out here ASAP */
3473 if (VN_BAD(vp)) {
3474 xfs_ireclaim(ip);
3475 return 0;
3476 }
3477
b677c210 3478 vn_iowait(ip);
1da177e4 3479
51c91ed5 3480 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
1da177e4 3481
42fe2b1f
CH
3482 /*
3483 * Make sure the atime in the XFS inode is correct before freeing the
3484 * Linux inode.
3485 */
3486 xfs_synchronize_atime(ip);
1da177e4 3487
4c60658e
DC
3488 /*
3489 * If we have nothing to flush with this inode then complete the
3490 * teardown now, otherwise break the link between the xfs inode and the
3491 * linux inode and clean up the xfs inode later. This avoids flushing
3492 * the inode to disk during the delete operation itself.
3493 *
3494 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3495 * first to ensure that xfs_iunpin() will never see an xfs inode
3496 * that has a linux inode being reclaimed. Synchronisation is provided
3497 * by the i_flags_lock.
1da177e4
LT
3498 */
3499 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3500 xfs_ilock(ip, XFS_ILOCK_EXCL);
3501 xfs_iflock(ip);
3502 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3503 } else {
3504 xfs_mount_t *mp = ip->i_mount;
3505
4c60658e 3506 /* Protect sync and unpin from us */
1da177e4 3507 XFS_MOUNT_ILOCK(mp);
4c60658e
DC
3508 spin_lock(&ip->i_flags_lock);
3509 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
739bfb2a
CH
3510 vn_to_inode(vp)->i_private = NULL;
3511 ip->i_vnode = NULL;
4c60658e 3512 spin_unlock(&ip->i_flags_lock);
1da177e4 3513 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
1da177e4
LT
3514 XFS_MOUNT_IUNLOCK(mp);
3515 }
3516 return 0;
3517}
3518
3519int
3520xfs_finish_reclaim(
3521 xfs_inode_t *ip,
3522 int locked,
3523 int sync_mode)
3524{
da353b0d 3525 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
67fcaa73 3526 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
1da177e4
LT
3527 int error;
3528
3529 if (vp && VN_BAD(vp))
3530 goto reclaim;
3531
3532 /* The hash lock here protects a thread in xfs_iget_core from
3533 * racing with us on linking the inode back with a vnode.
3534 * Once we have the XFS_IRECLAIM flag set it will not touch
3535 * us.
3536 */
da353b0d 3537 write_lock(&pag->pag_ici_lock);
f273ab84 3538 spin_lock(&ip->i_flags_lock);
7a18c386
DC
3539 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3540 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
f273ab84 3541 spin_unlock(&ip->i_flags_lock);
da353b0d 3542 write_unlock(&pag->pag_ici_lock);
1da177e4
LT
3543 if (locked) {
3544 xfs_ifunlock(ip);
3545 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3546 }
014c2544 3547 return 1;
1da177e4 3548 }
7a18c386 3549 __xfs_iflags_set(ip, XFS_IRECLAIM);
f273ab84 3550 spin_unlock(&ip->i_flags_lock);
da353b0d
DC
3551 write_unlock(&pag->pag_ici_lock);
3552 xfs_put_perag(ip->i_mount, pag);
1da177e4
LT
3553
3554 /*
3555 * If the inode is still dirty, then flush it out. If the inode
3556 * is not in the AIL, then it will be OK to flush it delwri as
3557 * long as xfs_iflush() does not keep any references to the inode.
3558 * We leave that decision up to xfs_iflush() since it has the
3559 * knowledge of whether it's OK to simply do a delwri flush of
3560 * the inode or whether we need to wait until the inode is
3561 * pulled from the AIL.
3562 * We get the flush lock regardless, though, just to make sure
3563 * we don't free it while it is being flushed.
3564 */
461aa8a2
LM
3565 if (!locked) {
3566 xfs_ilock(ip, XFS_ILOCK_EXCL);
3567 xfs_iflock(ip);
3568 }
1da177e4 3569
461aa8a2 3570 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1da177e4
LT
3571 if (ip->i_update_core ||
3572 ((ip->i_itemp != NULL) &&
3573 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3574 error = xfs_iflush(ip, sync_mode);
3575 /*
3576 * If we hit an error, typically because of filesystem
3577 * shutdown, we don't need to let vn_reclaim to know
3578 * because we're gonna reclaim the inode anyway.
3579 */
3580 if (error) {
3581 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3582 goto reclaim;
3583 }
3584 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3585 }
3586
3587 ASSERT(ip->i_update_core == 0);
3588 ASSERT(ip->i_itemp == NULL ||
3589 ip->i_itemp->ili_format.ilf_fields == 0);
1da177e4
LT
3590 }
3591
461aa8a2
LM
3592 xfs_ifunlock(ip);
3593 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3594
1da177e4
LT
3595 reclaim:
3596 xfs_ireclaim(ip);
3597 return 0;
3598}
3599
3600int
3601xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3602{
3603 int purged;
3604 xfs_inode_t *ip, *n;
3605 int done = 0;
3606
3607 while (!done) {
3608 purged = 0;
3609 XFS_MOUNT_ILOCK(mp);
3610 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3611 if (noblock) {
3612 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3613 continue;
3614 if (xfs_ipincount(ip) ||
3615 !xfs_iflock_nowait(ip)) {
3616 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3617 continue;
3618 }
3619 }
3620 XFS_MOUNT_IUNLOCK(mp);
6b2cf618
FB
3621 if (xfs_finish_reclaim(ip, noblock,
3622 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3623 delay(1);
1da177e4
LT
3624 purged = 1;
3625 break;
3626 }
3627
3628 done = !purged;
3629 }
3630
3631 XFS_MOUNT_IUNLOCK(mp);
3632 return 0;
3633}
3634
3635/*
3636 * xfs_alloc_file_space()
3637 * This routine allocates disk space for the given file.
3638 *
3639 * If alloc_type == 0, this request is for an ALLOCSP type
3640 * request which will change the file size. In this case, no
3641 * DMAPI event will be generated by the call. A TRUNCATE event
3642 * will be generated later by xfs_setattr.
3643 *
3644 * If alloc_type != 0, this request is for a RESVSP type
3645 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3646 * lower block boundary byte address is less than the file's
3647 * length.
3648 *
3649 * RETURNS:
3650 * 0 on success
3651 * errno on error
3652 *
3653 */
ba0f32d4 3654STATIC int
1da177e4
LT
3655xfs_alloc_file_space(
3656 xfs_inode_t *ip,
3657 xfs_off_t offset,
3658 xfs_off_t len,
3659 int alloc_type,
3660 int attr_flags)
3661{
dd9f438e
NS
3662 xfs_mount_t *mp = ip->i_mount;
3663 xfs_off_t count;
1da177e4
LT
3664 xfs_filblks_t allocated_fsb;
3665 xfs_filblks_t allocatesize_fsb;
dd9f438e
NS
3666 xfs_extlen_t extsz, temp;
3667 xfs_fileoff_t startoffset_fsb;
1da177e4 3668 xfs_fsblock_t firstfsb;
dd9f438e
NS
3669 int nimaps;
3670 int bmapi_flag;
3671 int quota_flag;
1da177e4 3672 int rt;
1da177e4 3673 xfs_trans_t *tp;
dd9f438e
NS
3674 xfs_bmbt_irec_t imaps[1], *imapp;
3675 xfs_bmap_free_t free_list;
3676 uint qblocks, resblks, resrtextents;
3677 int committed;
3678 int error;
1da177e4 3679
cf441eeb 3680 xfs_itrace_entry(ip);
1da177e4
LT
3681
3682 if (XFS_FORCED_SHUTDOWN(mp))
3683 return XFS_ERROR(EIO);
3684
1da177e4
LT
3685 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3686 return error;
3687
3688 if (len <= 0)
3689 return XFS_ERROR(EINVAL);
3690
957d0ebe
DC
3691 rt = XFS_IS_REALTIME_INODE(ip);
3692 extsz = xfs_get_extsz_hint(ip);
3693
1da177e4 3694 count = len;
1da177e4 3695 imapp = &imaps[0];
dd9f438e
NS
3696 nimaps = 1;
3697 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
1da177e4
LT
3698 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3699 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3700
3701 /* Generate a DMAPI event if needed. */
ba87ea69 3702 if (alloc_type != 0 && offset < ip->i_size &&
1da177e4 3703 (attr_flags&ATTR_DMI) == 0 &&
eb9df39d 3704 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
1da177e4
LT
3705 xfs_off_t end_dmi_offset;
3706
3707 end_dmi_offset = offset+len;
ba87ea69
LM
3708 if (end_dmi_offset > ip->i_size)
3709 end_dmi_offset = ip->i_size;
1da177e4
LT
3710 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
3711 offset, end_dmi_offset - offset,
3712 0, NULL);
3713 if (error)
014c2544 3714 return error;
1da177e4
LT
3715 }
3716
3717 /*
dd9f438e 3718 * Allocate file space until done or until there is an error
1da177e4
LT
3719 */
3720retry:
3721 while (allocatesize_fsb && !error) {
dd9f438e
NS
3722 xfs_fileoff_t s, e;
3723
1da177e4 3724 /*
3ddb8fa9 3725 * Determine space reservations for data/realtime.
1da177e4 3726 */
dd9f438e 3727 if (unlikely(extsz)) {
1da177e4 3728 s = startoffset_fsb;
dd9f438e
NS
3729 do_div(s, extsz);
3730 s *= extsz;
3731 e = startoffset_fsb + allocatesize_fsb;
3732 if ((temp = do_mod(startoffset_fsb, extsz)))
3733 e += temp;
3734 if ((temp = do_mod(e, extsz)))
3735 e += extsz - temp;
3736 } else {
3737 s = 0;
3738 e = allocatesize_fsb;
3739 }
3740
3741 if (unlikely(rt)) {
3742 resrtextents = qblocks = (uint)(e - s);
3743 resrtextents /= mp->m_sb.sb_rextsize;
3744 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3745 quota_flag = XFS_QMOPT_RES_RTBLKS;
1da177e4 3746 } else {
dd9f438e
NS
3747 resrtextents = 0;
3748 resblks = qblocks = \
3749 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3750 quota_flag = XFS_QMOPT_RES_REGBLKS;
1da177e4
LT
3751 }
3752
3753 /*
dd9f438e 3754 * Allocate and setup the transaction.
1da177e4
LT
3755 */
3756 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
dd9f438e
NS
3757 error = xfs_trans_reserve(tp, resblks,
3758 XFS_WRITE_LOG_RES(mp), resrtextents,
1da177e4
LT
3759 XFS_TRANS_PERM_LOG_RES,
3760 XFS_WRITE_LOG_COUNT);
1da177e4 3761 /*
dd9f438e 3762 * Check for running out of space
1da177e4
LT
3763 */
3764 if (error) {
3765 /*
3766 * Free the transaction structure.
3767 */
3768 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3769 xfs_trans_cancel(tp, 0);
3770 break;
3771 }
3772 xfs_ilock(ip, XFS_ILOCK_EXCL);
dd9f438e
NS
3773 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3774 qblocks, 0, quota_flag);
1da177e4
LT
3775 if (error)
3776 goto error1;
3777
3778 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3779 xfs_trans_ihold(tp, ip);
3780
3781 /*
dd9f438e 3782 * Issue the xfs_bmapi() call to allocate the blocks
1da177e4
LT
3783 */
3784 XFS_BMAP_INIT(&free_list, &firstfsb);
541d7d3c 3785 error = xfs_bmapi(tp, ip, startoffset_fsb,
dd9f438e
NS
3786 allocatesize_fsb, bmapi_flag,
3787 &firstfsb, 0, imapp, &nimaps,
3e57ecf6 3788 &free_list, NULL);
1da177e4
LT
3789 if (error) {
3790 goto error0;
3791 }
3792
3793 /*
dd9f438e 3794 * Complete the transaction
1da177e4 3795 */
f7c99b6f 3796 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
3797 if (error) {
3798 goto error0;
3799 }
3800
1c72bf90 3801 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
3802 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3803 if (error) {
3804 break;
3805 }
3806
3807 allocated_fsb = imapp->br_blockcount;
3808
dd9f438e 3809 if (nimaps == 0) {
1da177e4
LT
3810 error = XFS_ERROR(ENOSPC);
3811 break;
3812 }
3813
3814 startoffset_fsb += allocated_fsb;
3815 allocatesize_fsb -= allocated_fsb;
3816 }
3817dmapi_enospc_check:
eb9df39d
CH
3818 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3819 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
1da177e4
LT
3820 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3821 XFS_ITOV(ip), DM_RIGHT_NULL,
3822 XFS_ITOV(ip), DM_RIGHT_NULL,
3823 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3824 if (error == 0)
3825 goto retry; /* Maybe DMAPI app. has made space */
3826 /* else fall through with error from XFS_SEND_DATA */
3827 }
3828
3829 return error;
3830
dd9f438e 3831error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1da177e4 3832 xfs_bmap_cancel(&free_list);
dd9f438e
NS
3833 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3834
3835error1: /* Just cancel transaction */
1da177e4
LT
3836 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3837 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3838 goto dmapi_enospc_check;
3839}
3840
3841/*
3842 * Zero file bytes between startoff and endoff inclusive.
3843 * The iolock is held exclusive and no blocks are buffered.
3844 */
3845STATIC int
3846xfs_zero_remaining_bytes(
3847 xfs_inode_t *ip,
3848 xfs_off_t startoff,
3849 xfs_off_t endoff)
3850{
3851 xfs_bmbt_irec_t imap;
3852 xfs_fileoff_t offset_fsb;
3853 xfs_off_t lastoffset;
3854 xfs_off_t offset;
3855 xfs_buf_t *bp;
3856 xfs_mount_t *mp = ip->i_mount;
3857 int nimap;
3858 int error = 0;
3859
3860 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
71ddabb9 3861 XFS_IS_REALTIME_INODE(ip) ?
1da177e4
LT
3862 mp->m_rtdev_targp : mp->m_ddev_targp);
3863
3864 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3865 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3866 nimap = 1;
541d7d3c 3867 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3e57ecf6 3868 NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
3869 if (error || nimap < 1)
3870 break;
3871 ASSERT(imap.br_blockcount >= 1);
3872 ASSERT(imap.br_startoff == offset_fsb);
3873 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3874 if (lastoffset > endoff)
3875 lastoffset = endoff;
3876 if (imap.br_startblock == HOLESTARTBLOCK)
3877 continue;
3878 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3879 if (imap.br_state == XFS_EXT_UNWRITTEN)
3880 continue;
3881 XFS_BUF_UNDONE(bp);
3882 XFS_BUF_UNWRITE(bp);
3883 XFS_BUF_READ(bp);
3884 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3885 xfsbdstrat(mp, bp);
3886 if ((error = xfs_iowait(bp))) {
3887 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3888 mp, bp, XFS_BUF_ADDR(bp));
3889 break;
3890 }
3891 memset(XFS_BUF_PTR(bp) +
3892 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3893 0, lastoffset - offset + 1);
3894 XFS_BUF_UNDONE(bp);
3895 XFS_BUF_UNREAD(bp);
3896 XFS_BUF_WRITE(bp);
3897 xfsbdstrat(mp, bp);
3898 if ((error = xfs_iowait(bp))) {
3899 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3900 mp, bp, XFS_BUF_ADDR(bp));
3901 break;
3902 }
3903 }
3904 xfs_buf_free(bp);
3905 return error;
3906}
3907
3908/*
3909 * xfs_free_file_space()
3910 * This routine frees disk space for the given file.
3911 *
3912 * This routine is only called by xfs_change_file_space
3913 * for an UNRESVSP type call.
3914 *
3915 * RETURNS:
3916 * 0 on success
3917 * errno on error
3918 *
3919 */
3920STATIC int
3921xfs_free_file_space(
3922 xfs_inode_t *ip,
3923 xfs_off_t offset,
3924 xfs_off_t len,
3925 int attr_flags)
3926{
67fcaa73 3927 bhv_vnode_t *vp;
1da177e4
LT
3928 int committed;
3929 int done;
3930 xfs_off_t end_dmi_offset;
3931 xfs_fileoff_t endoffset_fsb;
3932 int error;
3933 xfs_fsblock_t firstfsb;
3934 xfs_bmap_free_t free_list;
1da177e4
LT
3935 xfs_bmbt_irec_t imap;
3936 xfs_off_t ioffset;
3937 xfs_extlen_t mod=0;
3938 xfs_mount_t *mp;
3939 int nimap;
3940 uint resblks;
673cdf5c 3941 uint rounding;
1da177e4
LT
3942 int rt;
3943 xfs_fileoff_t startoffset_fsb;
3944 xfs_trans_t *tp;
5fcbab35 3945 int need_iolock = 1;
1da177e4 3946
bd5a876a 3947 vp = XFS_ITOV(ip);
1da177e4
LT
3948 mp = ip->i_mount;
3949
cf441eeb 3950 xfs_itrace_entry(ip);
bd5a876a 3951
1da177e4
LT
3952 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3953 return error;
3954
3955 error = 0;
3956 if (len <= 0) /* if nothing being freed */
3957 return error;
71ddabb9 3958 rt = XFS_IS_REALTIME_INODE(ip);
1da177e4
LT
3959 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3960 end_dmi_offset = offset + len;
3961 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3962
eb9df39d
CH
3963 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3964 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
ba87ea69
LM
3965 if (end_dmi_offset > ip->i_size)
3966 end_dmi_offset = ip->i_size;
bd5a876a 3967 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
1da177e4
LT
3968 offset, end_dmi_offset - offset,
3969 AT_DELAY_FLAG(attr_flags), NULL);
3970 if (error)
014c2544 3971 return error;
1da177e4
LT
3972 }
3973
5fcbab35
DR
3974 if (attr_flags & ATTR_NOLOCK)
3975 need_iolock = 0;
9fa8046f 3976 if (need_iolock) {
1da177e4 3977 xfs_ilock(ip, XFS_IOLOCK_EXCL);
b677c210 3978 vn_iowait(ip); /* wait for the completion of any pending DIOs */
9fa8046f 3979 }
5fcbab35 3980
e6a4b37f 3981 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1da177e4 3982 ioffset = offset & ~(rounding - 1);
bd5a876a
CH
3983
3984 if (VN_CACHED(vp) != 0) {
e6a4b37f
TS
3985 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3986 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
d3cf2094
LM
3987 if (error)
3988 goto out_unlock_iolock;
bd5a876a
CH
3989 }
3990
1da177e4
LT
3991 /*
3992 * Need to zero the stuff we're not freeing, on disk.
3993 * If its a realtime file & can't use unwritten extents then we
3994 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3995 * will take care of it for us.
3996 */
62118709 3997 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1da177e4 3998 nimap = 1;
541d7d3c 3999 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3e57ecf6 4000 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
4001 if (error)
4002 goto out_unlock_iolock;
4003 ASSERT(nimap == 0 || nimap == 1);
4004 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4005 xfs_daddr_t block;
4006
4007 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4008 block = imap.br_startblock;
4009 mod = do_div(block, mp->m_sb.sb_rextsize);
4010 if (mod)
4011 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4012 }
4013 nimap = 1;
541d7d3c 4014 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3e57ecf6 4015 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
1da177e4
LT
4016 if (error)
4017 goto out_unlock_iolock;
4018 ASSERT(nimap == 0 || nimap == 1);
4019 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4020 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4021 mod++;
4022 if (mod && (mod != mp->m_sb.sb_rextsize))
4023 endoffset_fsb -= mod;
4024 }
4025 }
4026 if ((done = (endoffset_fsb <= startoffset_fsb)))
4027 /*
4028 * One contiguous piece to clear
4029 */
4030 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4031 else {
4032 /*
4033 * Some full blocks, possibly two pieces to clear
4034 */
4035 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4036 error = xfs_zero_remaining_bytes(ip, offset,
4037 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4038 if (!error &&
4039 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4040 error = xfs_zero_remaining_bytes(ip,
4041 XFS_FSB_TO_B(mp, endoffset_fsb),
4042 offset + len - 1);
4043 }
4044
4045 /*
4046 * free file space until done or until there is an error
4047 */
4048 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4049 while (!error && !done) {
4050
4051 /*
91ebecc7
DC
4052 * allocate and setup the transaction. Allow this
4053 * transaction to dip into the reserve blocks to ensure
4054 * the freeing of the space succeeds at ENOSPC.
1da177e4
LT
4055 */
4056 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
91ebecc7 4057 tp->t_flags |= XFS_TRANS_RESERVE;
1da177e4
LT
4058 error = xfs_trans_reserve(tp,
4059 resblks,
4060 XFS_WRITE_LOG_RES(mp),
4061 0,
4062 XFS_TRANS_PERM_LOG_RES,
4063 XFS_WRITE_LOG_COUNT);
4064
4065 /*
4066 * check for running out of space
4067 */
4068 if (error) {
4069 /*
4070 * Free the transaction structure.
4071 */
4072 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4073 xfs_trans_cancel(tp, 0);
4074 break;
4075 }
4076 xfs_ilock(ip, XFS_ILOCK_EXCL);
4077 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
dd9f438e
NS
4078 ip->i_udquot, ip->i_gdquot, resblks, 0,
4079 XFS_QMOPT_RES_REGBLKS);
1da177e4
LT
4080 if (error)
4081 goto error1;
4082
4083 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4084 xfs_trans_ihold(tp, ip);
4085
4086 /*
4087 * issue the bunmapi() call to free the blocks
4088 */
4089 XFS_BMAP_INIT(&free_list, &firstfsb);
541d7d3c 4090 error = xfs_bunmapi(tp, ip, startoffset_fsb,
1da177e4 4091 endoffset_fsb - startoffset_fsb,
3e57ecf6 4092 0, 2, &firstfsb, &free_list, NULL, &done);
1da177e4
LT
4093 if (error) {
4094 goto error0;
4095 }
4096
4097 /*
4098 * complete the transaction
4099 */
f7c99b6f 4100 error = xfs_bmap_finish(&tp, &free_list, &committed);
1da177e4
LT
4101 if (error) {
4102 goto error0;
4103 }
4104
1c72bf90 4105 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1da177e4
LT
4106 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4107 }
4108
4109 out_unlock_iolock:
4110 if (need_iolock)
4111 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4112 return error;
4113
4114 error0:
4115 xfs_bmap_cancel(&free_list);
4116 error1:
4117 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4118 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4119 XFS_ILOCK_EXCL);
4120 return error;
4121}
4122
4123/*
4124 * xfs_change_file_space()
4125 * This routine allocates or frees disk space for the given file.
4126 * The user specified parameters are checked for alignment and size
4127 * limitations.
4128 *
4129 * RETURNS:
4130 * 0 on success
4131 * errno on error
4132 *
4133 */
4134int
4135xfs_change_file_space(
993386c1 4136 xfs_inode_t *ip,
1da177e4
LT
4137 int cmd,
4138 xfs_flock64_t *bf,
4139 xfs_off_t offset,
4140 cred_t *credp,
4141 int attr_flags)
4142{
993386c1 4143 xfs_mount_t *mp = ip->i_mount;
1da177e4
LT
4144 int clrprealloc;
4145 int error;
4146 xfs_fsize_t fsize;
1da177e4
LT
4147 int setprealloc;
4148 xfs_off_t startoffset;
4149 xfs_off_t llen;
4150 xfs_trans_t *tp;
8285fb58 4151 bhv_vattr_t va;
1da177e4 4152
cf441eeb 4153 xfs_itrace_entry(ip);
1da177e4 4154
993386c1 4155 if (!S_ISREG(ip->i_d.di_mode))
1da177e4
LT
4156 return XFS_ERROR(EINVAL);
4157
1da177e4
LT
4158 switch (bf->l_whence) {
4159 case 0: /*SEEK_SET*/
4160 break;
4161 case 1: /*SEEK_CUR*/
4162 bf->l_start += offset;
4163 break;
4164 case 2: /*SEEK_END*/
ba87ea69 4165 bf->l_start += ip->i_size;
1da177e4
LT
4166 break;
4167 default:
4168 return XFS_ERROR(EINVAL);
4169 }
4170
4171 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4172
4173 if ( (bf->l_start < 0)
4174 || (bf->l_start > XFS_MAXIOFFSET(mp))
4175 || (bf->l_start + llen < 0)
4176 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4177 return XFS_ERROR(EINVAL);
4178
4179 bf->l_whence = 0;
4180
4181 startoffset = bf->l_start;
ba87ea69 4182 fsize = ip->i_size;
1da177e4
LT
4183
4184 /*
4185 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4186 * file space.
4187 * These calls do NOT zero the data space allocated to the file,
4188 * nor do they change the file size.
4189 *
4190 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4191 * space.
4192 * These calls cause the new file data to be zeroed and the file
4193 * size to be changed.
4194 */
4195 setprealloc = clrprealloc = 0;
4196
4197 switch (cmd) {
4198 case XFS_IOC_RESVSP:
4199 case XFS_IOC_RESVSP64:
4200 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4201 1, attr_flags);
4202 if (error)
4203 return error;
4204 setprealloc = 1;
4205 break;
4206
4207 case XFS_IOC_UNRESVSP:
4208 case XFS_IOC_UNRESVSP64:
4209 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4210 attr_flags)))
4211 return error;
4212 break;
4213
4214 case XFS_IOC_ALLOCSP:
4215 case XFS_IOC_ALLOCSP64:
4216 case XFS_IOC_FREESP:
4217 case XFS_IOC_FREESP64:
4218 if (startoffset > fsize) {
4219 error = xfs_alloc_file_space(ip, fsize,
4220 startoffset - fsize, 0, attr_flags);
4221 if (error)
4222 break;
4223 }
4224
4225 va.va_mask = XFS_AT_SIZE;
4226 va.va_size = startoffset;
4227
993386c1 4228 error = xfs_setattr(ip, &va, attr_flags, credp);
1da177e4
LT
4229
4230 if (error)
4231 return error;
4232
4233 clrprealloc = 1;
4234 break;
4235
4236 default:
4237 ASSERT(0);
4238 return XFS_ERROR(EINVAL);
4239 }
4240
4241 /*
4242 * update the inode timestamp, mode, and prealloc flag bits
4243 */
4244 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4245
4246 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4247 0, 0, 0))) {
4248 /* ASSERT(0); */
4249 xfs_trans_cancel(tp, 0);
4250 return error;
4251 }
4252
4253 xfs_ilock(ip, XFS_ILOCK_EXCL);
4254
4255 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4256 xfs_trans_ihold(tp, ip);
4257
4258 if ((attr_flags & ATTR_DMI) == 0) {
4259 ip->i_d.di_mode &= ~S_ISUID;
4260
4261 /*
4262 * Note that we don't have to worry about mandatory
4263 * file locking being disabled here because we only
4264 * clear the S_ISGID bit if the Group execute bit is
4265 * on, but if it was on then mandatory locking wouldn't
4266 * have been enabled.
4267 */
4268 if (ip->i_d.di_mode & S_IXGRP)
4269 ip->i_d.di_mode &= ~S_ISGID;
4270
4271 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4272 }
4273 if (setprealloc)
4274 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4275 else if (clrprealloc)
4276 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4277
4278 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4279 xfs_trans_set_sync(tp);
4280
1c72bf90 4281 error = xfs_trans_commit(tp, 0);
1da177e4
LT
4282
4283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4284
4285 return error;
4286}
This page took 0.562429 seconds and 5 git commands to generate.