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