1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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_dir2.h"
29#include "xfs_dmapi.h"
30#include "xfs_mount.h"
31#include "xfs_da_btree.h"
32#include "xfs_bmap_btree.h"
33#include "xfs_alloc_btree.h"
34#include "xfs_ialloc_btree.h"
35#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
39#include "xfs_inode_item.h"
40#include "xfs_itable.h"
41#include "xfs_btree.h"
42#include "xfs_ialloc.h"
43#include "xfs_alloc.h"
44#include "xfs_bmap.h"
45#include "xfs_acl.h"
46#include "xfs_attr.h"
47#include "xfs_rw.h"
48#include "xfs_error.h"
49#include "xfs_quota.h"
50#include "xfs_utils.h"
51#include "xfs_rtalloc.h"
52#include "xfs_trans_space.h"
53#include "xfs_log_priv.h"
54#include "xfs_filestream.h"
55#include "xfs_vnodeops.h"
56
57int
58xfs_setattr(
59 struct xfs_inode *ip,
60 struct iattr *iattr,
61 int flags)
62{
63 xfs_mount_t *mp = ip->i_mount;
64 struct inode *inode = VFS_I(ip);
65 int mask = iattr->ia_valid;
66 xfs_trans_t *tp;
67 int code;
68 uint lock_flags;
69 uint commit_flags=0;
70 uid_t uid=0, iuid=0;
71 gid_t gid=0, igid=0;
72 int timeflags = 0;
73 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
74 int need_iolock = 1;
75
76 xfs_itrace_entry(ip);
77
78 if (mp->m_flags & XFS_MOUNT_RDONLY)
79 return XFS_ERROR(EROFS);
80
81 if (XFS_FORCED_SHUTDOWN(mp))
82 return XFS_ERROR(EIO);
83
84 code = -inode_change_ok(inode, iattr);
85 if (code)
86 return code;
87
88 olddquot1 = olddquot2 = NULL;
89 udqp = gdqp = NULL;
90
91
92
93
94
95
96
97
98
99 if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
100 uint qflags = 0;
101
102 if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
103 uid = iattr->ia_uid;
104 qflags |= XFS_QMOPT_UQUOTA;
105 } else {
106 uid = ip->i_d.di_uid;
107 }
108 if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
109 gid = iattr->ia_gid;
110 qflags |= XFS_QMOPT_GQUOTA;
111 } else {
112 gid = ip->i_d.di_gid;
113 }
114
115
116
117
118
119
120 ASSERT(udqp == NULL);
121 ASSERT(gdqp == NULL);
122 code = xfs_qm_vop_dqalloc(ip, uid, gid, ip->i_d.di_projid,
123 qflags, &udqp, &gdqp);
124 if (code)
125 return code;
126 }
127
128
129
130
131
132 tp = NULL;
133 lock_flags = XFS_ILOCK_EXCL;
134 if (flags & XFS_ATTR_NOLOCK)
135 need_iolock = 0;
136 if (!(mask & ATTR_SIZE)) {
137 if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
138 (mp->m_flags & XFS_MOUNT_WSYNC)) {
139 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
140 commit_flags = 0;
141 if ((code = xfs_trans_reserve(tp, 0,
142 XFS_ICHANGE_LOG_RES(mp), 0,
143 0, 0))) {
144 lock_flags = 0;
145 goto error_return;
146 }
147 }
148 } else {
149 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
150 !(flags & XFS_ATTR_DMI)) {
151 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
152 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
153 iattr->ia_size, 0, dmflags, NULL);
154 if (code) {
155 lock_flags = 0;
156 goto error_return;
157 }
158 }
159 if (need_iolock)
160 lock_flags |= XFS_IOLOCK_EXCL;
161 }
162
163 xfs_ilock(ip, lock_flags);
164
165
166
167
168 if (mask & (ATTR_UID|ATTR_GID)) {
169
170
171
172
173
174
175 iuid = ip->i_d.di_uid;
176 igid = ip->i_d.di_gid;
177 gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
178 uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
179
180
181
182
183
184 if (XFS_IS_QUOTA_RUNNING(mp) &&
185 ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
186 (XFS_IS_GQUOTA_ON(mp) && igid != gid))) {
187 ASSERT(tp);
188 code = xfs_qm_vop_chown_reserve(tp, ip, udqp, gdqp,
189 capable(CAP_FOWNER) ?
190 XFS_QMOPT_FORCE_RES : 0);
191 if (code)
192 goto error_return;
193 }
194 }
195
196
197
198
199 if (mask & ATTR_SIZE) {
200
201 if (iattr->ia_size == 0 &&
202 ip->i_size == 0 && ip->i_d.di_nextents == 0) {
203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
204 lock_flags &= ~XFS_ILOCK_EXCL;
205 if (mask & ATTR_CTIME)
206 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
207 code = 0;
208 goto error_return;
209 }
210
211 if (S_ISDIR(ip->i_d.di_mode)) {
212 code = XFS_ERROR(EISDIR);
213 goto error_return;
214 } else if (!S_ISREG(ip->i_d.di_mode)) {
215 code = XFS_ERROR(EINVAL);
216 goto error_return;
217 }
218
219
220
221
222 code = xfs_qm_dqattach_locked(ip, 0);
223 if (code)
224 goto error_return;
225
226
227
228
229
230
231
232
233
234 if (iattr->ia_size > ip->i_size) {
235
236
237
238
239
240
241 code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
242 }
243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
244
245
246
247
248
249
250
251
252
253
254
255
256
257 if (!code &&
258 ip->i_size != ip->i_d.di_size &&
259 iattr->ia_size > ip->i_d.di_size) {
260 code = xfs_flush_pages(ip,
261 ip->i_d.di_size, iattr->ia_size,
262 XFS_B_ASYNC, FI_NONE);
263 }
264
265
266 xfs_ioend_wait(ip);
267
268 if (!code)
269 code = xfs_itruncate_data(ip, iattr->ia_size);
270 if (code) {
271 ASSERT(tp == NULL);
272 lock_flags &= ~XFS_ILOCK_EXCL;
273 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
274 goto error_return;
275 }
276 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
277 if ((code = xfs_trans_reserve(tp, 0,
278 XFS_ITRUNCATE_LOG_RES(mp), 0,
279 XFS_TRANS_PERM_LOG_RES,
280 XFS_ITRUNCATE_LOG_COUNT))) {
281 xfs_trans_cancel(tp, 0);
282 if (need_iolock)
283 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
284 return code;
285 }
286 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
287 xfs_ilock(ip, XFS_ILOCK_EXCL);
288
289 xfs_trans_ijoin(tp, ip, lock_flags);
290 xfs_trans_ihold(tp, ip);
291
292
293
294
295
296
297
298 if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
299 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
300
301 if (iattr->ia_size > ip->i_size) {
302 ip->i_d.di_size = iattr->ia_size;
303 ip->i_size = iattr->ia_size;
304 if (!(flags & XFS_ATTR_DMI))
305 xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
306 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
307 } else if (iattr->ia_size <= ip->i_size ||
308 (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
309
310
311
312
313
314 code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
315 XFS_DATA_FORK,
316 ((ip->i_d.di_nlink != 0 ||
317 !(mp->m_flags & XFS_MOUNT_WSYNC))
318 ? 1 : 0));
319 if (code)
320 goto abort_return;
321
322
323
324
325
326
327
328
329 xfs_iflags_set(ip, XFS_ITRUNCATED);
330 }
331 } else if (tp) {
332 xfs_trans_ijoin(tp, ip, lock_flags);
333 xfs_trans_ihold(tp, ip);
334 }
335
336
337
338
339 if (mask & (ATTR_UID|ATTR_GID)) {
340
341
342
343
344
345
346 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
347 !capable(CAP_FSETID)) {
348 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
349 }
350
351
352
353
354
355 if (iuid != uid) {
356 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_UQUOTA_ON(mp)) {
357 ASSERT(mask & ATTR_UID);
358 ASSERT(udqp);
359 olddquot1 = xfs_qm_vop_chown(tp, ip,
360 &ip->i_udquot, udqp);
361 }
362 ip->i_d.di_uid = uid;
363 inode->i_uid = uid;
364 }
365 if (igid != gid) {
366 if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_GQUOTA_ON(mp)) {
367 ASSERT(!XFS_IS_PQUOTA_ON(mp));
368 ASSERT(mask & ATTR_GID);
369 ASSERT(gdqp);
370 olddquot2 = xfs_qm_vop_chown(tp, ip,
371 &ip->i_gdquot, gdqp);
372 }
373 ip->i_d.di_gid = gid;
374 inode->i_gid = gid;
375 }
376
377 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
378 timeflags |= XFS_ICHGTIME_CHG;
379 }
380
381
382
383
384 if (mask & ATTR_MODE) {
385 umode_t mode = iattr->ia_mode;
386
387 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
388 mode &= ~S_ISGID;
389
390 ip->i_d.di_mode &= S_IFMT;
391 ip->i_d.di_mode |= mode & ~S_IFMT;
392
393 inode->i_mode &= S_IFMT;
394 inode->i_mode |= mode & ~S_IFMT;
395
396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
397 timeflags |= XFS_ICHGTIME_CHG;
398 }
399
400
401
402
403 if (mask & (ATTR_ATIME|ATTR_MTIME)) {
404 if (mask & ATTR_ATIME) {
405 inode->i_atime = iattr->ia_atime;
406 ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
407 ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
408 ip->i_update_core = 1;
409 }
410 if (mask & ATTR_MTIME) {
411 inode->i_mtime = iattr->ia_mtime;
412 ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
413 ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
414 timeflags &= ~XFS_ICHGTIME_MOD;
415 timeflags |= XFS_ICHGTIME_CHG;
416 }
417 if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
418 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
419 }
420
421
422
423
424
425
426 if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
427 inode->i_ctime = iattr->ia_ctime;
428 ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
429 ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
430 ip->i_update_core = 1;
431 timeflags &= ~XFS_ICHGTIME_CHG;
432 }
433
434
435
436
437
438 if (timeflags && !(flags & XFS_ATTR_DMI))
439 xfs_ichgtime(ip, timeflags);
440
441 XFS_STATS_INC(xs_ig_attrchg);
442
443
444
445
446
447
448
449
450
451
452
453 code = 0;
454 if (tp) {
455 if (mp->m_flags & XFS_MOUNT_WSYNC)
456 xfs_trans_set_sync(tp);
457
458 code = xfs_trans_commit(tp, commit_flags);
459 }
460
461 xfs_iunlock(ip, lock_flags);
462
463
464
465
466 xfs_qm_dqrele(olddquot1);
467 xfs_qm_dqrele(olddquot2);
468 xfs_qm_dqrele(udqp);
469 xfs_qm_dqrele(gdqp);
470
471 if (code)
472 return code;
473
474
475
476
477
478
479
480
481 if ((mask & ATTR_MODE) && !(flags & XFS_ATTR_NOACL)) {
482 code = -xfs_acl_chmod(inode);
483 if (code)
484 return XFS_ERROR(code);
485 }
486
487 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
488 !(flags & XFS_ATTR_DMI)) {
489 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
490 NULL, DM_RIGHT_NULL, NULL, NULL,
491 0, 0, AT_DELAY_FLAG(flags));
492 }
493 return 0;
494
495 abort_return:
496 commit_flags |= XFS_TRANS_ABORT;
497
498 error_return:
499 xfs_qm_dqrele(udqp);
500 xfs_qm_dqrele(gdqp);
501 if (tp) {
502 xfs_trans_cancel(tp, commit_flags);
503 }
504 if (lock_flags != 0) {
505 xfs_iunlock(ip, lock_flags);
506 }
507 return code;
508}
509
510
511
512
513
514
515#define SYMLINK_MAPS 2
516
517STATIC int
518xfs_readlink_bmap(
519 xfs_inode_t *ip,
520 char *link)
521{
522 xfs_mount_t *mp = ip->i_mount;
523 int pathlen = ip->i_d.di_size;
524 int nmaps = SYMLINK_MAPS;
525 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
526 xfs_daddr_t d;
527 int byte_cnt;
528 int n;
529 xfs_buf_t *bp;
530 int error = 0;
531
532 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
533 mval, &nmaps, NULL, NULL);
534 if (error)
535 goto out;
536
537 for (n = 0; n < nmaps; n++) {
538 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
539 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
540
541 bp = xfs_buf_read_flags(mp->m_ddev_targp, d, BTOBB(byte_cnt),
542 XBF_LOCK | XBF_MAPPED |
543 XBF_DONT_BLOCK);
544 error = XFS_BUF_GETERROR(bp);
545 if (error) {
546 xfs_ioerror_alert("xfs_readlink",
547 ip->i_mount, bp, XFS_BUF_ADDR(bp));
548 xfs_buf_relse(bp);
549 goto out;
550 }
551 if (pathlen < byte_cnt)
552 byte_cnt = pathlen;
553 pathlen -= byte_cnt;
554
555 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
556 xfs_buf_relse(bp);
557 }
558
559 link[ip->i_d.di_size] = '\0';
560 error = 0;
561
562 out:
563 return error;
564}
565
566int
567xfs_readlink(
568 xfs_inode_t *ip,
569 char *link)
570{
571 xfs_mount_t *mp = ip->i_mount;
572 int pathlen;
573 int error = 0;
574
575 xfs_itrace_entry(ip);
576
577 if (XFS_FORCED_SHUTDOWN(mp))
578 return XFS_ERROR(EIO);
579
580 xfs_ilock(ip, XFS_ILOCK_SHARED);
581
582 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
583 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
584
585 pathlen = ip->i_d.di_size;
586 if (!pathlen)
587 goto out;
588
589 if (ip->i_df.if_flags & XFS_IFINLINE) {
590 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
591 link[pathlen] = '\0';
592 } else {
593 error = xfs_readlink_bmap(ip, link);
594 }
595
596 out:
597 xfs_iunlock(ip, XFS_ILOCK_SHARED);
598 return error;
599}
600
601
602
603
604
605
606
607
608
609int
610xfs_fsync(
611 xfs_inode_t *ip)
612{
613 xfs_trans_t *tp;
614 int error = 0;
615 int log_flushed = 0, changed = 1;
616
617 xfs_itrace_entry(ip);
618
619 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
620 return XFS_ERROR(EIO);
621
622
623
624
625
626
627
628
629
630
631
632
633
634 xfs_ilock(ip, XFS_ILOCK_SHARED);
635
636 if (!ip->i_update_core) {
637
638
639
640
641
642
643
644
645
646 xfs_iunlock(ip, XFS_ILOCK_SHARED);
647
648 if (xfs_ipincount(ip)) {
649 error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
650 XFS_LOG_FORCE | XFS_LOG_SYNC,
651 &log_flushed);
652 } else {
653
654
655
656
657 changed = 0;
658 }
659 } else {
660
661
662
663
664 xfs_iunlock(ip, XFS_ILOCK_SHARED);
665 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
666 error = xfs_trans_reserve(tp, 0,
667 XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
668 if (error) {
669 xfs_trans_cancel(tp, 0);
670 return error;
671 }
672 xfs_ilock(ip, XFS_ILOCK_EXCL);
673
674
675
676
677
678
679
680
681
682
683 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
684 xfs_trans_ihold(tp, ip);
685 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
686 xfs_trans_set_sync(tp);
687 error = _xfs_trans_commit(tp, 0, &log_flushed);
688
689 xfs_iunlock(ip, XFS_ILOCK_EXCL);
690 }
691
692 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
693
694
695
696
697 if (!log_flushed)
698 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
699
700
701
702
703
704 if (XFS_IS_REALTIME_INODE(ip))
705 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
706 }
707
708 return error;
709}
710
711
712
713
714
715
716STATIC int
717xfs_free_eofblocks(
718 xfs_mount_t *mp,
719 xfs_inode_t *ip,
720 int flags)
721{
722 xfs_trans_t *tp;
723 int error;
724 xfs_fileoff_t end_fsb;
725 xfs_fileoff_t last_fsb;
726 xfs_filblks_t map_len;
727 int nimaps;
728 xfs_bmbt_irec_t imap;
729 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
730
731
732
733
734
735 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
736 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
737 map_len = last_fsb - end_fsb;
738 if (map_len <= 0)
739 return 0;
740
741 nimaps = 1;
742 xfs_ilock(ip, XFS_ILOCK_SHARED);
743 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
744 NULL, 0, &imap, &nimaps, NULL, NULL);
745 xfs_iunlock(ip, XFS_ILOCK_SHARED);
746
747 if (!error && (nimaps != 0) &&
748 (imap.br_startblock != HOLESTARTBLOCK ||
749 ip->i_delayed_blks)) {
750
751
752
753 error = xfs_qm_dqattach(ip, 0);
754 if (error)
755 return error;
756
757
758
759
760
761
762 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
763
764
765
766
767
768
769
770
771 if (use_iolock)
772 xfs_ilock(ip, XFS_IOLOCK_EXCL);
773 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
774 ip->i_size);
775 if (error) {
776 xfs_trans_cancel(tp, 0);
777 if (use_iolock)
778 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
779 return error;
780 }
781
782 error = xfs_trans_reserve(tp, 0,
783 XFS_ITRUNCATE_LOG_RES(mp),
784 0, XFS_TRANS_PERM_LOG_RES,
785 XFS_ITRUNCATE_LOG_COUNT);
786 if (error) {
787 ASSERT(XFS_FORCED_SHUTDOWN(mp));
788 xfs_trans_cancel(tp, 0);
789 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
790 return error;
791 }
792
793 xfs_ilock(ip, XFS_ILOCK_EXCL);
794 xfs_trans_ijoin(tp, ip,
795 XFS_IOLOCK_EXCL |
796 XFS_ILOCK_EXCL);
797 xfs_trans_ihold(tp, ip);
798
799 error = xfs_itruncate_finish(&tp, ip,
800 ip->i_size,
801 XFS_DATA_FORK,
802 0);
803
804
805
806
807 if (error) {
808 xfs_trans_cancel(tp,
809 (XFS_TRANS_RELEASE_LOG_RES |
810 XFS_TRANS_ABORT));
811 } else {
812 error = xfs_trans_commit(tp,
813 XFS_TRANS_RELEASE_LOG_RES);
814 }
815 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
816 : XFS_ILOCK_EXCL));
817 }
818 return error;
819}
820
821
822
823
824STATIC int
825xfs_inactive_symlink_rmt(
826 xfs_inode_t *ip,
827 xfs_trans_t **tpp)
828{
829 xfs_buf_t *bp;
830 int committed;
831 int done;
832 int error;
833 xfs_fsblock_t first_block;
834 xfs_bmap_free_t free_list;
835 int i;
836 xfs_mount_t *mp;
837 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
838 int nmaps;
839 xfs_trans_t *ntp;
840 int size;
841 xfs_trans_t *tp;
842
843 tp = *tpp;
844 mp = ip->i_mount;
845 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
846
847
848
849
850
851
852
853 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
854 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
855 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
856 ASSERT(XFS_FORCED_SHUTDOWN(mp));
857 xfs_trans_cancel(tp, 0);
858 *tpp = NULL;
859 return error;
860 }
861
862
863
864
865
866
867 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
868 size = (int)ip->i_d.di_size;
869 ip->i_d.di_size = 0;
870 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
871 xfs_trans_ihold(tp, ip);
872 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
873
874
875
876 done = 0;
877 xfs_bmap_init(&free_list, &first_block);
878 nmaps = ARRAY_SIZE(mval);
879 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
880 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
881 &free_list, NULL)))
882 goto error0;
883
884
885
886 for (i = 0; i < nmaps; i++) {
887 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
888 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
889 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
890 xfs_trans_binval(tp, bp);
891 }
892
893
894
895 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
896 &first_block, &free_list, NULL, &done)))
897 goto error1;
898 ASSERT(done);
899
900
901
902 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
903 goto error1;
904
905
906
907
908
909 ASSERT(committed);
910
911
912
913
914
915 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
916 xfs_trans_ihold(tp, ip);
917 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
918
919
920
921 ntp = xfs_trans_dup(tp);
922
923
924
925
926
927
928 error = xfs_trans_commit(tp, 0);
929 tp = ntp;
930 if (error) {
931 ASSERT(XFS_FORCED_SHUTDOWN(mp));
932 goto error0;
933 }
934
935
936
937
938 xfs_log_ticket_put(tp->t_ticket);
939
940
941
942
943 if (ip->i_df.if_bytes)
944 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
945 ASSERT(ip->i_df.if_bytes == 0);
946
947
948
949
950 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
951 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
952 ASSERT(XFS_FORCED_SHUTDOWN(mp));
953 goto error0;
954 }
955
956
957
958 *tpp = tp;
959 return 0;
960
961 error1:
962 xfs_bmap_cancel(&free_list);
963 error0:
964
965
966
967
968
969
970
971
972
973
974 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
975 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
976 *tpp = NULL;
977 return error;
978
979}
980
981STATIC int
982xfs_inactive_symlink_local(
983 xfs_inode_t *ip,
984 xfs_trans_t **tpp)
985{
986 int error;
987
988 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
989
990
991
992
993
994 error = xfs_trans_reserve(*tpp, 0,
995 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
996 0, XFS_TRANS_PERM_LOG_RES,
997 XFS_ITRUNCATE_LOG_COUNT);
998
999 if (error) {
1000 xfs_trans_cancel(*tpp, 0);
1001 *tpp = NULL;
1002 return error;
1003 }
1004 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1005
1006
1007
1008
1009 if (ip->i_df.if_bytes > 0) {
1010 xfs_idata_realloc(ip,
1011 -(ip->i_df.if_bytes),
1012 XFS_DATA_FORK);
1013 ASSERT(ip->i_df.if_bytes == 0);
1014 }
1015 return 0;
1016}
1017
1018STATIC int
1019xfs_inactive_attrs(
1020 xfs_inode_t *ip,
1021 xfs_trans_t **tpp)
1022{
1023 xfs_trans_t *tp;
1024 int error;
1025 xfs_mount_t *mp;
1026
1027 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1028 tp = *tpp;
1029 mp = ip->i_mount;
1030 ASSERT(ip->i_d.di_forkoff != 0);
1031 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1032 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1033 if (error)
1034 goto error_unlock;
1035
1036 error = xfs_attr_inactive(ip);
1037 if (error)
1038 goto error_unlock;
1039
1040 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1041 error = xfs_trans_reserve(tp, 0,
1042 XFS_IFREE_LOG_RES(mp),
1043 0, XFS_TRANS_PERM_LOG_RES,
1044 XFS_INACTIVE_LOG_COUNT);
1045 if (error)
1046 goto error_cancel;
1047
1048 xfs_ilock(ip, XFS_ILOCK_EXCL);
1049 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1050 xfs_trans_ihold(tp, ip);
1051 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1052
1053 ASSERT(ip->i_d.di_anextents == 0);
1054
1055 *tpp = tp;
1056 return 0;
1057
1058error_cancel:
1059 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1060 xfs_trans_cancel(tp, 0);
1061error_unlock:
1062 *tpp = NULL;
1063 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1064 return error;
1065}
1066
1067int
1068xfs_release(
1069 xfs_inode_t *ip)
1070{
1071 xfs_mount_t *mp = ip->i_mount;
1072 int error;
1073
1074 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1075 return 0;
1076
1077
1078 if (mp->m_flags & XFS_MOUNT_RDONLY)
1079 return 0;
1080
1081 if (!XFS_FORCED_SHUTDOWN(mp)) {
1082 int truncated;
1083
1084
1085
1086
1087
1088
1089
1090
1091 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1092 xfs_filestream_deassociate(ip);
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1105 if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
1106 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1107 }
1108
1109 if (ip->i_d.di_nlink != 0) {
1110 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1111 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1112 ip->i_delayed_blks > 0)) &&
1113 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1114 (!(ip->i_d.di_flags &
1115 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1116 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1117 if (error)
1118 return error;
1119 }
1120 }
1121
1122 return 0;
1123}
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133int
1134xfs_inactive(
1135 xfs_inode_t *ip)
1136{
1137 xfs_bmap_free_t free_list;
1138 xfs_fsblock_t first_block;
1139 int committed;
1140 xfs_trans_t *tp;
1141 xfs_mount_t *mp;
1142 int error;
1143 int truncate;
1144
1145 xfs_itrace_entry(ip);
1146
1147
1148
1149
1150
1151 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
1152 ASSERT(ip->i_df.if_real_bytes == 0);
1153 ASSERT(ip->i_df.if_broot_bytes == 0);
1154 return VN_INACTIVE_CACHE;
1155 }
1156
1157
1158
1159
1160
1161
1162
1163 truncate = ((ip->i_d.di_nlink == 0) &&
1164 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1165 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1166 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1167
1168 mp = ip->i_mount;
1169
1170 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1171 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1172
1173 error = 0;
1174
1175
1176 if (mp->m_flags & XFS_MOUNT_RDONLY)
1177 goto out;
1178
1179 if (ip->i_d.di_nlink != 0) {
1180 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1181 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
1182 ip->i_delayed_blks > 0)) &&
1183 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1184 (!(ip->i_d.di_flags &
1185 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1186 (ip->i_delayed_blks != 0)))) {
1187 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1188 if (error)
1189 return VN_INACTIVE_CACHE;
1190 }
1191 goto out;
1192 }
1193
1194 ASSERT(ip->i_d.di_nlink == 0);
1195
1196 error = xfs_qm_dqattach(ip, 0);
1197 if (error)
1198 return VN_INACTIVE_CACHE;
1199
1200 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1201 if (truncate) {
1202
1203
1204
1205
1206
1207
1208 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1209
1210 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1211 if (error) {
1212 xfs_trans_cancel(tp, 0);
1213 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1214 return VN_INACTIVE_CACHE;
1215 }
1216
1217 error = xfs_trans_reserve(tp, 0,
1218 XFS_ITRUNCATE_LOG_RES(mp),
1219 0, XFS_TRANS_PERM_LOG_RES,
1220 XFS_ITRUNCATE_LOG_COUNT);
1221 if (error) {
1222
1223 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1224 xfs_trans_cancel(tp, 0);
1225 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1226 return VN_INACTIVE_CACHE;
1227 }
1228
1229 xfs_ilock(ip, XFS_ILOCK_EXCL);
1230 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1231 xfs_trans_ihold(tp, ip);
1232
1233
1234
1235
1236
1237
1238
1239
1240 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1241 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1242
1243 if (error) {
1244 xfs_trans_cancel(tp,
1245 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1246 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1247 return VN_INACTIVE_CACHE;
1248 }
1249 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1250
1251
1252
1253
1254
1255 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1256 xfs_inactive_symlink_rmt(ip, &tp) :
1257 xfs_inactive_symlink_local(ip, &tp);
1258
1259 if (error) {
1260 ASSERT(tp == NULL);
1261 return VN_INACTIVE_CACHE;
1262 }
1263
1264 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1265 xfs_trans_ihold(tp, ip);
1266 } else {
1267 error = xfs_trans_reserve(tp, 0,
1268 XFS_IFREE_LOG_RES(mp),
1269 0, XFS_TRANS_PERM_LOG_RES,
1270 XFS_INACTIVE_LOG_COUNT);
1271 if (error) {
1272 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1273 xfs_trans_cancel(tp, 0);
1274 return VN_INACTIVE_CACHE;
1275 }
1276
1277 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1278 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1279 xfs_trans_ihold(tp, ip);
1280 }
1281
1282
1283
1284
1285
1286
1287
1288
1289 if (ip->i_d.di_anextents > 0) {
1290 error = xfs_inactive_attrs(ip, &tp);
1291
1292
1293
1294
1295 if (error)
1296 return VN_INACTIVE_CACHE;
1297 } else if (ip->i_afp) {
1298 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1299 }
1300
1301
1302
1303
1304 xfs_bmap_init(&free_list, &first_block);
1305 error = xfs_ifree(tp, ip, &free_list);
1306 if (error) {
1307
1308
1309
1310
1311
1312 if (!XFS_FORCED_SHUTDOWN(mp)) {
1313 cmn_err(CE_NOTE,
1314 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1315 error, mp->m_fsname);
1316 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1317 }
1318 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1319 } else {
1320
1321
1322
1323 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1324
1325
1326
1327
1328
1329
1330 error = xfs_bmap_finish(&tp, &free_list, &committed);
1331 if (error)
1332 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1333 "xfs_bmap_finish() returned error %d", error);
1334 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1335 if (error)
1336 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1337 "xfs_trans_commit() returned error %d", error);
1338 }
1339
1340
1341
1342
1343 xfs_qm_dqdetach(ip);
1344 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1345
1346 out:
1347 return VN_INACTIVE_CACHE;
1348}
1349
1350
1351
1352
1353
1354
1355
1356int
1357xfs_lookup(
1358 xfs_inode_t *dp,
1359 struct xfs_name *name,
1360 xfs_inode_t **ipp,
1361 struct xfs_name *ci_name)
1362{
1363 xfs_ino_t inum;
1364 int error;
1365 uint lock_mode;
1366
1367 xfs_itrace_entry(dp);
1368
1369 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1370 return XFS_ERROR(EIO);
1371
1372 lock_mode = xfs_ilock_map_shared(dp);
1373 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
1374 xfs_iunlock_map_shared(dp, lock_mode);
1375
1376 if (error)
1377 goto out;
1378
1379 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
1380 if (error)
1381 goto out_free_name;
1382
1383 xfs_itrace_ref(*ipp);
1384 return 0;
1385
1386out_free_name:
1387 if (ci_name)
1388 kmem_free(ci_name->name);
1389out:
1390 *ipp = NULL;
1391 return error;
1392}
1393
1394int
1395xfs_create(
1396 xfs_inode_t *dp,
1397 struct xfs_name *name,
1398 mode_t mode,
1399 xfs_dev_t rdev,
1400 xfs_inode_t **ipp,
1401 cred_t *credp)
1402{
1403 int is_dir = S_ISDIR(mode);
1404 struct xfs_mount *mp = dp->i_mount;
1405 struct xfs_inode *ip = NULL;
1406 struct xfs_trans *tp = NULL;
1407 int error;
1408 xfs_bmap_free_t free_list;
1409 xfs_fsblock_t first_block;
1410 boolean_t unlock_dp_on_error = B_FALSE;
1411 uint cancel_flags;
1412 int committed;
1413 xfs_prid_t prid;
1414 struct xfs_dquot *udqp = NULL;
1415 struct xfs_dquot *gdqp = NULL;
1416 uint resblks;
1417 uint log_res;
1418 uint log_count;
1419
1420 xfs_itrace_entry(dp);
1421
1422 if (XFS_FORCED_SHUTDOWN(mp))
1423 return XFS_ERROR(EIO);
1424
1425 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1426 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1427 dp, DM_RIGHT_NULL, NULL,
1428 DM_RIGHT_NULL, name->name, NULL,
1429 mode, 0, 0);
1430
1431 if (error)
1432 return error;
1433 }
1434
1435 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1436 prid = dp->i_d.di_projid;
1437 else
1438 prid = dfltprid;
1439
1440
1441
1442
1443 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1444 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1445 if (error)
1446 goto std_return;
1447
1448 if (is_dir) {
1449 rdev = 0;
1450 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1451 log_res = XFS_MKDIR_LOG_RES(mp);
1452 log_count = XFS_MKDIR_LOG_COUNT;
1453 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
1454 } else {
1455 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1456 log_res = XFS_CREATE_LOG_RES(mp);
1457 log_count = XFS_CREATE_LOG_COUNT;
1458 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1459 }
1460
1461 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1462
1463
1464
1465
1466
1467
1468
1469 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1470 XFS_TRANS_PERM_LOG_RES, log_count);
1471 if (error == ENOSPC) {
1472
1473 xfs_flush_inodes(dp);
1474 error = xfs_trans_reserve(tp, resblks, log_res, 0,
1475 XFS_TRANS_PERM_LOG_RES, log_count);
1476 }
1477 if (error == ENOSPC) {
1478
1479 resblks = 0;
1480 error = xfs_trans_reserve(tp, 0, log_res, 0,
1481 XFS_TRANS_PERM_LOG_RES, log_count);
1482 }
1483 if (error) {
1484 cancel_flags = 0;
1485 goto out_trans_cancel;
1486 }
1487
1488 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1489 unlock_dp_on_error = B_TRUE;
1490
1491
1492
1493
1494 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
1495 error = XFS_ERROR(EMLINK);
1496 goto out_trans_cancel;
1497 }
1498
1499 xfs_bmap_init(&free_list, &first_block);
1500
1501
1502
1503
1504 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1505 if (error)
1506 goto out_trans_cancel;
1507
1508 error = xfs_dir_canenter(tp, dp, name, resblks);
1509 if (error)
1510 goto out_trans_cancel;
1511
1512
1513
1514
1515
1516
1517 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, credp,
1518 prid, resblks > 0, &ip, &committed);
1519 if (error) {
1520 if (error == ENOSPC)
1521 goto out_trans_cancel;
1522 goto out_trans_abort;
1523 }
1524
1525
1526
1527
1528
1529 xfs_itrace_ref(ip);
1530 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1531
1532
1533
1534
1535
1536
1537
1538
1539 IHOLD(dp);
1540 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1541 unlock_dp_on_error = B_FALSE;
1542
1543 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1544 &first_block, &free_list, resblks ?
1545 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1546 if (error) {
1547 ASSERT(error != ENOSPC);
1548 goto out_trans_abort;
1549 }
1550 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1551 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1552
1553 if (is_dir) {
1554 error = xfs_dir_init(tp, ip, dp);
1555 if (error)
1556 goto out_bmap_cancel;
1557
1558 error = xfs_bumplink(tp, dp);
1559 if (error)
1560 goto out_bmap_cancel;
1561 }
1562
1563
1564
1565
1566
1567
1568 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1569 xfs_trans_set_sync(tp);
1570
1571
1572
1573
1574
1575
1576 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1577
1578
1579
1580
1581
1582
1583 IHOLD(ip);
1584
1585 error = xfs_bmap_finish(&tp, &free_list, &committed);
1586 if (error)
1587 goto out_abort_rele;
1588
1589 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1590 if (error) {
1591 IRELE(ip);
1592 goto out_dqrele;
1593 }
1594
1595 xfs_qm_dqrele(udqp);
1596 xfs_qm_dqrele(gdqp);
1597
1598 *ipp = ip;
1599
1600
1601 std_return:
1602 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1603 XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE, dp, DM_RIGHT_NULL,
1604 ip, DM_RIGHT_NULL, name->name, NULL, mode,
1605 error, 0);
1606 }
1607
1608 return error;
1609
1610 out_bmap_cancel:
1611 xfs_bmap_cancel(&free_list);
1612 out_trans_abort:
1613 cancel_flags |= XFS_TRANS_ABORT;
1614 out_trans_cancel:
1615 xfs_trans_cancel(tp, cancel_flags);
1616 out_dqrele:
1617 xfs_qm_dqrele(udqp);
1618 xfs_qm_dqrele(gdqp);
1619
1620 if (unlock_dp_on_error)
1621 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1622
1623 goto std_return;
1624
1625 out_abort_rele:
1626
1627
1628
1629
1630
1631 xfs_bmap_cancel(&free_list);
1632 cancel_flags |= XFS_TRANS_ABORT;
1633 xfs_trans_cancel(tp, cancel_flags);
1634 IRELE(ip);
1635 unlock_dp_on_error = B_FALSE;
1636 goto out_dqrele;
1637}
1638
1639#ifdef DEBUG
1640int xfs_locked_n;
1641int xfs_small_retries;
1642int xfs_middle_retries;
1643int xfs_lots_retries;
1644int xfs_lock_delays;
1645#endif
1646
1647
1648
1649
1650
1651static inline int
1652xfs_lock_inumorder(int lock_mode, int subclass)
1653{
1654 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1655 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1656 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1657 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1658
1659 return lock_mode;
1660}
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673void
1674xfs_lock_inodes(
1675 xfs_inode_t **ips,
1676 int inodes,
1677 uint lock_mode)
1678{
1679 int attempts = 0, i, j, try_lock;
1680 xfs_log_item_t *lp;
1681
1682 ASSERT(ips && (inodes >= 2));
1683
1684 try_lock = 0;
1685 i = 0;
1686
1687again:
1688 for (; i < inodes; i++) {
1689 ASSERT(ips[i]);
1690
1691 if (i && (ips[i] == ips[i-1]))
1692 continue;
1693
1694
1695
1696
1697
1698
1699
1700 if (!try_lock) {
1701 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1702 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1703 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1704 try_lock++;
1705 }
1706 }
1707 }
1708
1709
1710
1711
1712
1713
1714
1715
1716 if (try_lock) {
1717
1718
1719
1720
1721
1722 ASSERT(i != 0);
1723 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1724 attempts++;
1725
1726
1727
1728
1729
1730
1731
1732 for(j = i - 1; j >= 0; j--) {
1733
1734
1735
1736
1737
1738
1739
1740 if ((j != (i - 1)) && ips[j] ==
1741 ips[j+1])
1742 continue;
1743
1744 xfs_iunlock(ips[j], lock_mode);
1745 }
1746
1747 if ((attempts % 5) == 0) {
1748 delay(1);
1749#ifdef DEBUG
1750 xfs_lock_delays++;
1751#endif
1752 }
1753 i = 0;
1754 try_lock = 0;
1755 goto again;
1756 }
1757 } else {
1758 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1759 }
1760 }
1761
1762#ifdef DEBUG
1763 if (attempts) {
1764 if (attempts < 5) xfs_small_retries++;
1765 else if (attempts < 100) xfs_middle_retries++;
1766 else xfs_lots_retries++;
1767 } else {
1768 xfs_locked_n++;
1769 }
1770#endif
1771}
1772
1773
1774
1775
1776
1777
1778
1779void
1780xfs_lock_two_inodes(
1781 xfs_inode_t *ip0,
1782 xfs_inode_t *ip1,
1783 uint lock_mode)
1784{
1785 xfs_inode_t *temp;
1786 int attempts = 0;
1787 xfs_log_item_t *lp;
1788
1789 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1790 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1791 ASSERT(ip0->i_ino != ip1->i_ino);
1792
1793 if (ip0->i_ino > ip1->i_ino) {
1794 temp = ip0;
1795 ip0 = ip1;
1796 ip1 = temp;
1797 }
1798
1799 again:
1800 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1801
1802
1803
1804
1805
1806
1807 lp = (xfs_log_item_t *)ip0->i_itemp;
1808 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1809 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1810 xfs_iunlock(ip0, lock_mode);
1811 if ((++attempts % 5) == 0)
1812 delay(1);
1813 goto again;
1814 }
1815 } else {
1816 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1817 }
1818}
1819
1820int
1821xfs_remove(
1822 xfs_inode_t *dp,
1823 struct xfs_name *name,
1824 xfs_inode_t *ip)
1825{
1826 xfs_mount_t *mp = dp->i_mount;
1827 xfs_trans_t *tp = NULL;
1828 int is_dir = S_ISDIR(ip->i_d.di_mode);
1829 int error = 0;
1830 xfs_bmap_free_t free_list;
1831 xfs_fsblock_t first_block;
1832 int cancel_flags;
1833 int committed;
1834 int link_zero;
1835 uint resblks;
1836 uint log_count;
1837
1838 xfs_itrace_entry(dp);
1839 xfs_itrace_entry(ip);
1840
1841 if (XFS_FORCED_SHUTDOWN(mp))
1842 return XFS_ERROR(EIO);
1843
1844 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
1845 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
1846 NULL, DM_RIGHT_NULL, name->name, NULL,
1847 ip->i_d.di_mode, 0, 0);
1848 if (error)
1849 return error;
1850 }
1851
1852 error = xfs_qm_dqattach(dp, 0);
1853 if (error)
1854 goto std_return;
1855
1856 error = xfs_qm_dqattach(ip, 0);
1857 if (error)
1858 goto std_return;
1859
1860 if (is_dir) {
1861 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1862 log_count = XFS_DEFAULT_LOG_COUNT;
1863 } else {
1864 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1865 log_count = XFS_REMOVE_LOG_COUNT;
1866 }
1867 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878 resblks = XFS_REMOVE_SPACE_RES(mp);
1879 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1880 XFS_TRANS_PERM_LOG_RES, log_count);
1881 if (error == ENOSPC) {
1882 resblks = 0;
1883 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1884 XFS_TRANS_PERM_LOG_RES, log_count);
1885 }
1886 if (error) {
1887 ASSERT(error != ENOSPC);
1888 cancel_flags = 0;
1889 goto out_trans_cancel;
1890 }
1891
1892 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1893
1894
1895
1896
1897
1898 IHOLD(ip);
1899 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1900
1901 IHOLD(dp);
1902 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1903
1904
1905
1906
1907 if (is_dir) {
1908 ASSERT(ip->i_d.di_nlink >= 2);
1909 if (ip->i_d.di_nlink != 2) {
1910 error = XFS_ERROR(ENOTEMPTY);
1911 goto out_trans_cancel;
1912 }
1913 if (!xfs_dir_isempty(ip)) {
1914 error = XFS_ERROR(ENOTEMPTY);
1915 goto out_trans_cancel;
1916 }
1917 }
1918
1919 xfs_bmap_init(&free_list, &first_block);
1920 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1921 &first_block, &free_list, resblks);
1922 if (error) {
1923 ASSERT(error != ENOENT);
1924 goto out_bmap_cancel;
1925 }
1926 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1927
1928 if (is_dir) {
1929
1930
1931
1932 error = xfs_droplink(tp, dp);
1933 if (error)
1934 goto out_bmap_cancel;
1935
1936
1937
1938
1939 error = xfs_droplink(tp, ip);
1940 if (error)
1941 goto out_bmap_cancel;
1942 } else {
1943
1944
1945
1946
1947
1948 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1949 }
1950
1951
1952
1953
1954 error = xfs_droplink(tp, ip);
1955 if (error)
1956 goto out_bmap_cancel;
1957
1958
1959
1960
1961
1962 link_zero = (ip->i_d.di_nlink == 0);
1963
1964
1965
1966
1967
1968
1969 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1970 xfs_trans_set_sync(tp);
1971
1972 error = xfs_bmap_finish(&tp, &free_list, &committed);
1973 if (error)
1974 goto out_bmap_cancel;
1975
1976 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1977 if (error)
1978 goto std_return;
1979
1980
1981
1982
1983
1984
1985
1986 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1987 xfs_filestream_deassociate(ip);
1988
1989 xfs_itrace_exit(ip);
1990 xfs_itrace_exit(dp);
1991
1992 std_return:
1993 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
1994 XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
1995 NULL, DM_RIGHT_NULL, name->name, NULL,
1996 ip->i_d.di_mode, error, 0);
1997 }
1998
1999 return error;
2000
2001 out_bmap_cancel:
2002 xfs_bmap_cancel(&free_list);
2003 cancel_flags |= XFS_TRANS_ABORT;
2004 out_trans_cancel:
2005 xfs_trans_cancel(tp, cancel_flags);
2006 goto std_return;
2007}
2008
2009int
2010xfs_link(
2011 xfs_inode_t *tdp,
2012 xfs_inode_t *sip,
2013 struct xfs_name *target_name)
2014{
2015 xfs_mount_t *mp = tdp->i_mount;
2016 xfs_trans_t *tp;
2017 int error;
2018 xfs_bmap_free_t free_list;
2019 xfs_fsblock_t first_block;
2020 int cancel_flags;
2021 int committed;
2022 int resblks;
2023
2024 xfs_itrace_entry(tdp);
2025 xfs_itrace_entry(sip);
2026
2027 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2028
2029 if (XFS_FORCED_SHUTDOWN(mp))
2030 return XFS_ERROR(EIO);
2031
2032 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2033 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2034 tdp, DM_RIGHT_NULL,
2035 sip, DM_RIGHT_NULL,
2036 target_name->name, NULL, 0, 0, 0);
2037 if (error)
2038 return error;
2039 }
2040
2041
2042
2043 error = xfs_qm_dqattach(sip, 0);
2044 if (error)
2045 goto std_return;
2046
2047 error = xfs_qm_dqattach(tdp, 0);
2048 if (error)
2049 goto std_return;
2050
2051 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2052 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2053 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2054 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2055 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2056 if (error == ENOSPC) {
2057 resblks = 0;
2058 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2059 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2060 }
2061 if (error) {
2062 cancel_flags = 0;
2063 goto error_return;
2064 }
2065
2066 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
2067
2068
2069
2070
2071
2072
2073 IHOLD(sip);
2074 IHOLD(tdp);
2075 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2076 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2077
2078
2079
2080
2081 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2082 error = XFS_ERROR(EMLINK);
2083 goto error_return;
2084 }
2085
2086
2087
2088
2089
2090
2091 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2092 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2093 error = XFS_ERROR(EXDEV);
2094 goto error_return;
2095 }
2096
2097 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2098 if (error)
2099 goto error_return;
2100
2101 xfs_bmap_init(&free_list, &first_block);
2102
2103 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2104 &first_block, &free_list, resblks);
2105 if (error)
2106 goto abort_return;
2107 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2108 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2109
2110 error = xfs_bumplink(tp, sip);
2111 if (error)
2112 goto abort_return;
2113
2114
2115
2116
2117
2118
2119 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2120 xfs_trans_set_sync(tp);
2121 }
2122
2123 error = xfs_bmap_finish (&tp, &free_list, &committed);
2124 if (error) {
2125 xfs_bmap_cancel(&free_list);
2126 goto abort_return;
2127 }
2128
2129 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2130 if (error)
2131 goto std_return;
2132
2133
2134std_return:
2135 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2136 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2137 tdp, DM_RIGHT_NULL,
2138 sip, DM_RIGHT_NULL,
2139 target_name->name, NULL, 0, error, 0);
2140 }
2141 return error;
2142
2143 abort_return:
2144 cancel_flags |= XFS_TRANS_ABORT;
2145
2146
2147 error_return:
2148 xfs_trans_cancel(tp, cancel_flags);
2149 goto std_return;
2150}
2151
2152int
2153xfs_symlink(
2154 xfs_inode_t *dp,
2155 struct xfs_name *link_name,
2156 const char *target_path,
2157 mode_t mode,
2158 xfs_inode_t **ipp,
2159 cred_t *credp)
2160{
2161 xfs_mount_t *mp = dp->i_mount;
2162 xfs_trans_t *tp;
2163 xfs_inode_t *ip;
2164 int error;
2165 int pathlen;
2166 xfs_bmap_free_t free_list;
2167 xfs_fsblock_t first_block;
2168 boolean_t unlock_dp_on_error = B_FALSE;
2169 uint cancel_flags;
2170 int committed;
2171 xfs_fileoff_t first_fsb;
2172 xfs_filblks_t fs_blocks;
2173 int nmaps;
2174 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
2175 xfs_daddr_t d;
2176 const char *cur_chunk;
2177 int byte_cnt;
2178 int n;
2179 xfs_buf_t *bp;
2180 xfs_prid_t prid;
2181 struct xfs_dquot *udqp, *gdqp;
2182 uint resblks;
2183
2184 *ipp = NULL;
2185 error = 0;
2186 ip = NULL;
2187 tp = NULL;
2188
2189 xfs_itrace_entry(dp);
2190
2191 if (XFS_FORCED_SHUTDOWN(mp))
2192 return XFS_ERROR(EIO);
2193
2194
2195
2196
2197 pathlen = strlen(target_path);
2198 if (pathlen >= MAXPATHLEN)
2199 return XFS_ERROR(ENAMETOOLONG);
2200
2201 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
2202 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
2203 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2204 link_name->name, target_path, 0, 0, 0);
2205 if (error)
2206 return error;
2207 }
2208
2209
2210
2211 udqp = gdqp = NULL;
2212 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2213 prid = dp->i_d.di_projid;
2214 else
2215 prid = (xfs_prid_t)dfltprid;
2216
2217
2218
2219
2220 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
2221 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2222 if (error)
2223 goto std_return;
2224
2225 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
2226 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2227
2228
2229
2230
2231 if (pathlen <= XFS_LITINO(mp))
2232 fs_blocks = 0;
2233 else
2234 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
2235 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
2236 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
2237 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2238 if (error == ENOSPC && fs_blocks == 0) {
2239 resblks = 0;
2240 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
2241 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
2242 }
2243 if (error) {
2244 cancel_flags = 0;
2245 goto error_return;
2246 }
2247
2248 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2249 unlock_dp_on_error = B_TRUE;
2250
2251
2252
2253
2254 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
2255 error = XFS_ERROR(EPERM);
2256 goto error_return;
2257 }
2258
2259
2260
2261
2262 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
2263 if (error)
2264 goto error_return;
2265
2266
2267
2268
2269 error = xfs_dir_canenter(tp, dp, link_name, resblks);
2270 if (error)
2271 goto error_return;
2272
2273
2274
2275
2276 xfs_bmap_init(&free_list, &first_block);
2277
2278
2279
2280
2281 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
2282 1, 0, credp, prid, resblks > 0, &ip, NULL);
2283 if (error) {
2284 if (error == ENOSPC)
2285 goto error_return;
2286 goto error1;
2287 }
2288 xfs_itrace_ref(ip);
2289
2290
2291
2292
2293
2294
2295 IHOLD(dp);
2296 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2297 unlock_dp_on_error = B_FALSE;
2298
2299
2300
2301
2302 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
2303
2304 if (resblks)
2305 resblks -= XFS_IALLOC_SPACE_RES(mp);
2306
2307
2308
2309 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
2310 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
2311 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
2312 ip->i_d.di_size = pathlen;
2313
2314
2315
2316
2317 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
2318 ip->i_df.if_flags |= XFS_IFINLINE;
2319
2320 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
2321 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
2322
2323 } else {
2324 first_fsb = 0;
2325 nmaps = SYMLINK_MAPS;
2326
2327 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
2328 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
2329 &first_block, resblks, mval, &nmaps,
2330 &free_list, NULL);
2331 if (error) {
2332 goto error1;
2333 }
2334
2335 if (resblks)
2336 resblks -= fs_blocks;
2337 ip->i_d.di_size = pathlen;
2338 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2339
2340 cur_chunk = target_path;
2341 for (n = 0; n < nmaps; n++) {
2342 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
2343 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
2344 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
2345 BTOBB(byte_cnt), 0);
2346 ASSERT(bp && !XFS_BUF_GETERROR(bp));
2347 if (pathlen < byte_cnt) {
2348 byte_cnt = pathlen;
2349 }
2350 pathlen -= byte_cnt;
2351
2352 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
2353 cur_chunk += byte_cnt;
2354
2355 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
2356 }
2357 }
2358
2359
2360
2361
2362 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
2363 &first_block, &free_list, resblks);
2364 if (error)
2365 goto error1;
2366 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2367 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2368
2369
2370
2371
2372
2373
2374 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2375 xfs_trans_set_sync(tp);
2376 }
2377
2378
2379
2380
2381
2382
2383 IHOLD(ip);
2384
2385 error = xfs_bmap_finish(&tp, &free_list, &committed);
2386 if (error) {
2387 goto error2;
2388 }
2389 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2390 xfs_qm_dqrele(udqp);
2391 xfs_qm_dqrele(gdqp);
2392
2393
2394
2395std_return:
2396 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
2397 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
2398 dp, DM_RIGHT_NULL,
2399 error ? NULL : ip,
2400 DM_RIGHT_NULL, link_name->name,
2401 target_path, 0, error, 0);
2402 }
2403
2404 if (!error)
2405 *ipp = ip;
2406 return error;
2407
2408 error2:
2409 IRELE(ip);
2410 error1:
2411 xfs_bmap_cancel(&free_list);
2412 cancel_flags |= XFS_TRANS_ABORT;
2413 error_return:
2414 xfs_trans_cancel(tp, cancel_flags);
2415 xfs_qm_dqrele(udqp);
2416 xfs_qm_dqrele(gdqp);
2417
2418 if (unlock_dp_on_error)
2419 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2420
2421 goto std_return;
2422}
2423
2424int
2425xfs_set_dmattrs(
2426 xfs_inode_t *ip,
2427 u_int evmask,
2428 u_int16_t state)
2429{
2430 xfs_mount_t *mp = ip->i_mount;
2431 xfs_trans_t *tp;
2432 int error;
2433
2434 if (!capable(CAP_SYS_ADMIN))
2435 return XFS_ERROR(EPERM);
2436
2437 if (XFS_FORCED_SHUTDOWN(mp))
2438 return XFS_ERROR(EIO);
2439
2440 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
2441 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
2442 if (error) {
2443 xfs_trans_cancel(tp, 0);
2444 return error;
2445 }
2446 xfs_ilock(ip, XFS_ILOCK_EXCL);
2447 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2448
2449 ip->i_d.di_dmevmask = evmask;
2450 ip->i_d.di_dmstate = state;
2451
2452 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2453 IHOLD(ip);
2454 error = xfs_trans_commit(tp, 0);
2455
2456 return error;
2457}
2458
2459int
2460xfs_reclaim(
2461 xfs_inode_t *ip)
2462{
2463
2464 xfs_itrace_entry(ip);
2465
2466 ASSERT(!VN_MAPPED(VFS_I(ip)));
2467
2468
2469 if (is_bad_inode(VFS_I(ip))) {
2470 xfs_ireclaim(ip);
2471 return 0;
2472 }
2473
2474 xfs_ioend_wait(ip);
2475
2476 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
2490 xfs_ilock(ip, XFS_ILOCK_EXCL);
2491 xfs_iflock(ip);
2492 xfs_iflags_set(ip, XFS_IRECLAIMABLE);
2493 return xfs_reclaim_inode(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
2494 }
2495 xfs_inode_set_reclaim_tag(ip);
2496 return 0;
2497}
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518STATIC int
2519xfs_alloc_file_space(
2520 xfs_inode_t *ip,
2521 xfs_off_t offset,
2522 xfs_off_t len,
2523 int alloc_type,
2524 int attr_flags)
2525{
2526 xfs_mount_t *mp = ip->i_mount;
2527 xfs_off_t count;
2528 xfs_filblks_t allocated_fsb;
2529 xfs_filblks_t allocatesize_fsb;
2530 xfs_extlen_t extsz, temp;
2531 xfs_fileoff_t startoffset_fsb;
2532 xfs_fsblock_t firstfsb;
2533 int nimaps;
2534 int bmapi_flag;
2535 int quota_flag;
2536 int rt;
2537 xfs_trans_t *tp;
2538 xfs_bmbt_irec_t imaps[1], *imapp;
2539 xfs_bmap_free_t free_list;
2540 uint qblocks, resblks, resrtextents;
2541 int committed;
2542 int error;
2543
2544 xfs_itrace_entry(ip);
2545
2546 if (XFS_FORCED_SHUTDOWN(mp))
2547 return XFS_ERROR(EIO);
2548
2549 error = xfs_qm_dqattach(ip, 0);
2550 if (error)
2551 return error;
2552
2553 if (len <= 0)
2554 return XFS_ERROR(EINVAL);
2555
2556 rt = XFS_IS_REALTIME_INODE(ip);
2557 extsz = xfs_get_extsz_hint(ip);
2558
2559 count = len;
2560 imapp = &imaps[0];
2561 nimaps = 1;
2562 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
2563 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
2564 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
2565
2566
2567 if (alloc_type != 0 && offset < ip->i_size &&
2568 (attr_flags & XFS_ATTR_DMI) == 0 &&
2569 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2570 xfs_off_t end_dmi_offset;
2571
2572 end_dmi_offset = offset+len;
2573 if (end_dmi_offset > ip->i_size)
2574 end_dmi_offset = ip->i_size;
2575 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
2576 end_dmi_offset - offset, 0, NULL);
2577 if (error)
2578 return error;
2579 }
2580
2581
2582
2583
2584retry:
2585 while (allocatesize_fsb && !error) {
2586 xfs_fileoff_t s, e;
2587
2588
2589
2590
2591 if (unlikely(extsz)) {
2592 s = startoffset_fsb;
2593 do_div(s, extsz);
2594 s *= extsz;
2595 e = startoffset_fsb + allocatesize_fsb;
2596 if ((temp = do_mod(startoffset_fsb, extsz)))
2597 e += temp;
2598 if ((temp = do_mod(e, extsz)))
2599 e += extsz - temp;
2600 } else {
2601 s = 0;
2602 e = allocatesize_fsb;
2603 }
2604
2605 if (unlikely(rt)) {
2606 resrtextents = qblocks = (uint)(e - s);
2607 resrtextents /= mp->m_sb.sb_rextsize;
2608 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2609 quota_flag = XFS_QMOPT_RES_RTBLKS;
2610 } else {
2611 resrtextents = 0;
2612 resblks = qblocks = \
2613 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
2614 quota_flag = XFS_QMOPT_RES_REGBLKS;
2615 }
2616
2617
2618
2619
2620 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2621 error = xfs_trans_reserve(tp, resblks,
2622 XFS_WRITE_LOG_RES(mp), resrtextents,
2623 XFS_TRANS_PERM_LOG_RES,
2624 XFS_WRITE_LOG_COUNT);
2625
2626
2627
2628 if (error) {
2629
2630
2631
2632 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2633 xfs_trans_cancel(tp, 0);
2634 break;
2635 }
2636 xfs_ilock(ip, XFS_ILOCK_EXCL);
2637 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
2638 0, quota_flag);
2639 if (error)
2640 goto error1;
2641
2642 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2643 xfs_trans_ihold(tp, ip);
2644
2645
2646
2647
2648 xfs_bmap_init(&free_list, &firstfsb);
2649 error = xfs_bmapi(tp, ip, startoffset_fsb,
2650 allocatesize_fsb, bmapi_flag,
2651 &firstfsb, 0, imapp, &nimaps,
2652 &free_list, NULL);
2653 if (error) {
2654 goto error0;
2655 }
2656
2657
2658
2659
2660 error = xfs_bmap_finish(&tp, &free_list, &committed);
2661 if (error) {
2662 goto error0;
2663 }
2664
2665 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2666 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2667 if (error) {
2668 break;
2669 }
2670
2671 allocated_fsb = imapp->br_blockcount;
2672
2673 if (nimaps == 0) {
2674 error = XFS_ERROR(ENOSPC);
2675 break;
2676 }
2677
2678 startoffset_fsb += allocated_fsb;
2679 allocatesize_fsb -= allocated_fsb;
2680 }
2681dmapi_enospc_check:
2682 if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
2683 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
2684 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
2685 ip, DM_RIGHT_NULL,
2686 ip, DM_RIGHT_NULL,
2687 NULL, NULL, 0, 0, 0);
2688 if (error == 0)
2689 goto retry;
2690
2691 }
2692
2693 return error;
2694
2695error0:
2696 xfs_bmap_cancel(&free_list);
2697 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
2698
2699error1:
2700 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2701 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2702 goto dmapi_enospc_check;
2703}
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716STATIC int
2717xfs_zero_remaining_bytes(
2718 xfs_inode_t *ip,
2719 xfs_off_t startoff,
2720 xfs_off_t endoff)
2721{
2722 xfs_bmbt_irec_t imap;
2723 xfs_fileoff_t offset_fsb;
2724 xfs_off_t lastoffset;
2725 xfs_off_t offset;
2726 xfs_buf_t *bp;
2727 xfs_mount_t *mp = ip->i_mount;
2728 int nimap;
2729 int error = 0;
2730
2731
2732
2733
2734
2735
2736 if (startoff >= ip->i_size)
2737 return 0;
2738
2739 if (endoff > ip->i_size)
2740 endoff = ip->i_size;
2741
2742 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
2743 XFS_IS_REALTIME_INODE(ip) ?
2744 mp->m_rtdev_targp : mp->m_ddev_targp);
2745 if (!bp)
2746 return XFS_ERROR(ENOMEM);
2747
2748 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
2749 offset_fsb = XFS_B_TO_FSBT(mp, offset);
2750 nimap = 1;
2751 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
2752 NULL, 0, &imap, &nimap, NULL, NULL);
2753 if (error || nimap < 1)
2754 break;
2755 ASSERT(imap.br_blockcount >= 1);
2756 ASSERT(imap.br_startoff == offset_fsb);
2757 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
2758 if (lastoffset > endoff)
2759 lastoffset = endoff;
2760 if (imap.br_startblock == HOLESTARTBLOCK)
2761 continue;
2762 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2763 if (imap.br_state == XFS_EXT_UNWRITTEN)
2764 continue;
2765 XFS_BUF_UNDONE(bp);
2766 XFS_BUF_UNWRITE(bp);
2767 XFS_BUF_READ(bp);
2768 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2769 xfsbdstrat(mp, bp);
2770 error = xfs_iowait(bp);
2771 if (error) {
2772 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2773 mp, bp, XFS_BUF_ADDR(bp));
2774 break;
2775 }
2776 memset(XFS_BUF_PTR(bp) +
2777 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2778 0, lastoffset - offset + 1);
2779 XFS_BUF_UNDONE(bp);
2780 XFS_BUF_UNREAD(bp);
2781 XFS_BUF_WRITE(bp);
2782 xfsbdstrat(mp, bp);
2783 error = xfs_iowait(bp);
2784 if (error) {
2785 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2786 mp, bp, XFS_BUF_ADDR(bp));
2787 break;
2788 }
2789 }
2790 xfs_buf_free(bp);
2791 return error;
2792}
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806STATIC int
2807xfs_free_file_space(
2808 xfs_inode_t *ip,
2809 xfs_off_t offset,
2810 xfs_off_t len,
2811 int attr_flags)
2812{
2813 int committed;
2814 int done;
2815 xfs_off_t end_dmi_offset;
2816 xfs_fileoff_t endoffset_fsb;
2817 int error;
2818 xfs_fsblock_t firstfsb;
2819 xfs_bmap_free_t free_list;
2820 xfs_bmbt_irec_t imap;
2821 xfs_off_t ioffset;
2822 xfs_extlen_t mod=0;
2823 xfs_mount_t *mp;
2824 int nimap;
2825 uint resblks;
2826 uint rounding;
2827 int rt;
2828 xfs_fileoff_t startoffset_fsb;
2829 xfs_trans_t *tp;
2830 int need_iolock = 1;
2831
2832 mp = ip->i_mount;
2833
2834 xfs_itrace_entry(ip);
2835
2836 error = xfs_qm_dqattach(ip, 0);
2837 if (error)
2838 return error;
2839
2840 error = 0;
2841 if (len <= 0)
2842 return error;
2843 rt = XFS_IS_REALTIME_INODE(ip);
2844 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2845 end_dmi_offset = offset + len;
2846 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
2847
2848 if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
2849 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
2850 if (end_dmi_offset > ip->i_size)
2851 end_dmi_offset = ip->i_size;
2852 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
2853 offset, end_dmi_offset - offset,
2854 AT_DELAY_FLAG(attr_flags), NULL);
2855 if (error)
2856 return error;
2857 }
2858
2859 if (attr_flags & XFS_ATTR_NOLOCK)
2860 need_iolock = 0;
2861 if (need_iolock) {
2862 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2863
2864 xfs_ioend_wait(ip);
2865 }
2866
2867 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2868 ioffset = offset & ~(rounding - 1);
2869
2870 if (VN_CACHED(VFS_I(ip)) != 0) {
2871 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
2872 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2873 if (error)
2874 goto out_unlock_iolock;
2875 }
2876
2877
2878
2879
2880
2881
2882
2883 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2884 nimap = 1;
2885 error = xfs_bmapi(NULL, ip, startoffset_fsb,
2886 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2887 if (error)
2888 goto out_unlock_iolock;
2889 ASSERT(nimap == 0 || nimap == 1);
2890 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2891 xfs_daddr_t block;
2892
2893 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2894 block = imap.br_startblock;
2895 mod = do_div(block, mp->m_sb.sb_rextsize);
2896 if (mod)
2897 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2898 }
2899 nimap = 1;
2900 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
2901 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
2902 if (error)
2903 goto out_unlock_iolock;
2904 ASSERT(nimap == 0 || nimap == 1);
2905 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2906 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2907 mod++;
2908 if (mod && (mod != mp->m_sb.sb_rextsize))
2909 endoffset_fsb -= mod;
2910 }
2911 }
2912 if ((done = (endoffset_fsb <= startoffset_fsb)))
2913
2914
2915
2916 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2917 else {
2918
2919
2920
2921 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2922 error = xfs_zero_remaining_bytes(ip, offset,
2923 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2924 if (!error &&
2925 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2926 error = xfs_zero_remaining_bytes(ip,
2927 XFS_FSB_TO_B(mp, endoffset_fsb),
2928 offset + len - 1);
2929 }
2930
2931
2932
2933
2934 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2935 while (!error && !done) {
2936
2937
2938
2939
2940
2941
2942 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2943 tp->t_flags |= XFS_TRANS_RESERVE;
2944 error = xfs_trans_reserve(tp,
2945 resblks,
2946 XFS_WRITE_LOG_RES(mp),
2947 0,
2948 XFS_TRANS_PERM_LOG_RES,
2949 XFS_WRITE_LOG_COUNT);
2950
2951
2952
2953
2954 if (error) {
2955
2956
2957
2958 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2959 xfs_trans_cancel(tp, 0);
2960 break;
2961 }
2962 xfs_ilock(ip, XFS_ILOCK_EXCL);
2963 error = xfs_trans_reserve_quota(tp, mp,
2964 ip->i_udquot, ip->i_gdquot,
2965 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2966 if (error)
2967 goto error1;
2968
2969 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2970 xfs_trans_ihold(tp, ip);
2971
2972
2973
2974
2975 xfs_bmap_init(&free_list, &firstfsb);
2976 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2977 endoffset_fsb - startoffset_fsb,
2978 0, 2, &firstfsb, &free_list, NULL, &done);
2979 if (error) {
2980 goto error0;
2981 }
2982
2983
2984
2985
2986 error = xfs_bmap_finish(&tp, &free_list, &committed);
2987 if (error) {
2988 goto error0;
2989 }
2990
2991 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2992 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2993 }
2994
2995 out_unlock_iolock:
2996 if (need_iolock)
2997 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2998 return error;
2999
3000 error0:
3001 xfs_bmap_cancel(&free_list);
3002 error1:
3003 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3004 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
3005 XFS_ILOCK_EXCL);
3006 return error;
3007}
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020int
3021xfs_change_file_space(
3022 xfs_inode_t *ip,
3023 int cmd,
3024 xfs_flock64_t *bf,
3025 xfs_off_t offset,
3026 int attr_flags)
3027{
3028 xfs_mount_t *mp = ip->i_mount;
3029 int clrprealloc;
3030 int error;
3031 xfs_fsize_t fsize;
3032 int setprealloc;
3033 xfs_off_t startoffset;
3034 xfs_off_t llen;
3035 xfs_trans_t *tp;
3036 struct iattr iattr;
3037
3038 xfs_itrace_entry(ip);
3039
3040 if (!S_ISREG(ip->i_d.di_mode))
3041 return XFS_ERROR(EINVAL);
3042
3043 switch (bf->l_whence) {
3044 case 0:
3045 break;
3046 case 1:
3047 bf->l_start += offset;
3048 break;
3049 case 2:
3050 bf->l_start += ip->i_size;
3051 break;
3052 default:
3053 return XFS_ERROR(EINVAL);
3054 }
3055
3056 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
3057
3058 if ( (bf->l_start < 0)
3059 || (bf->l_start > XFS_MAXIOFFSET(mp))
3060 || (bf->l_start + llen < 0)
3061 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
3062 return XFS_ERROR(EINVAL);
3063
3064 bf->l_whence = 0;
3065
3066 startoffset = bf->l_start;
3067 fsize = ip->i_size;
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080 setprealloc = clrprealloc = 0;
3081
3082 switch (cmd) {
3083 case XFS_IOC_RESVSP:
3084 case XFS_IOC_RESVSP64:
3085 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
3086 1, attr_flags);
3087 if (error)
3088 return error;
3089 setprealloc = 1;
3090 break;
3091
3092 case XFS_IOC_UNRESVSP:
3093 case XFS_IOC_UNRESVSP64:
3094 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
3095 attr_flags)))
3096 return error;
3097 break;
3098
3099 case XFS_IOC_ALLOCSP:
3100 case XFS_IOC_ALLOCSP64:
3101 case XFS_IOC_FREESP:
3102 case XFS_IOC_FREESP64:
3103 if (startoffset > fsize) {
3104 error = xfs_alloc_file_space(ip, fsize,
3105 startoffset - fsize, 0, attr_flags);
3106 if (error)
3107 break;
3108 }
3109
3110 iattr.ia_valid = ATTR_SIZE;
3111 iattr.ia_size = startoffset;
3112
3113 error = xfs_setattr(ip, &iattr, attr_flags);
3114
3115 if (error)
3116 return error;
3117
3118 clrprealloc = 1;
3119 break;
3120
3121 default:
3122 ASSERT(0);
3123 return XFS_ERROR(EINVAL);
3124 }
3125
3126
3127
3128
3129 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
3130
3131 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
3132 0, 0, 0))) {
3133
3134 xfs_trans_cancel(tp, 0);
3135 return error;
3136 }
3137
3138 xfs_ilock(ip, XFS_ILOCK_EXCL);
3139
3140 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3141 xfs_trans_ihold(tp, ip);
3142
3143 if ((attr_flags & XFS_ATTR_DMI) == 0) {
3144 ip->i_d.di_mode &= ~S_ISUID;
3145
3146
3147
3148
3149
3150
3151
3152
3153 if (ip->i_d.di_mode & S_IXGRP)
3154 ip->i_d.di_mode &= ~S_ISGID;
3155
3156 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3157 }
3158 if (setprealloc)
3159 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
3160 else if (clrprealloc)
3161 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
3162
3163 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3164 xfs_trans_set_sync(tp);
3165
3166 error = xfs_trans_commit(tp, 0);
3167
3168 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3169
3170 return error;
3171}
3172