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