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