1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/log2.h>
19
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_sb.h"
27#include "xfs_mount.h"
28#include "xfs_defer.h"
29#include "xfs_inode.h"
30#include "xfs_da_format.h"
31#include "xfs_da_btree.h"
32#include "xfs_dir2.h"
33#include "xfs_attr_sf.h"
34#include "xfs_attr.h"
35#include "xfs_trans_space.h"
36#include "xfs_trans.h"
37#include "xfs_buf_item.h"
38#include "xfs_inode_item.h"
39#include "xfs_ialloc.h"
40#include "xfs_bmap.h"
41#include "xfs_bmap_util.h"
42#include "xfs_errortag.h"
43#include "xfs_error.h"
44#include "xfs_quota.h"
45#include "xfs_filestream.h"
46#include "xfs_cksum.h"
47#include "xfs_trace.h"
48#include "xfs_icache.h"
49#include "xfs_symlink.h"
50#include "xfs_trans_priv.h"
51#include "xfs_log.h"
52#include "xfs_bmap_btree.h"
53#include "xfs_dir2_priv.h"
54
55kmem_zone_t *xfs_inode_zone;
56
57
58
59
60
61#define XFS_ITRUNC_MAX_EXTENTS 2
62
63STATIC int xfs_iflush_int(struct xfs_inode *, struct xfs_buf *);
64STATIC int xfs_iunlink(struct xfs_trans *, struct xfs_inode *);
65STATIC int xfs_iunlink_remove(struct xfs_trans *, struct xfs_inode *);
66
67
68
69
70xfs_extlen_t
71xfs_get_extsz_hint(
72 struct xfs_inode *ip)
73{
74 if ((ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE) && ip->i_d.di_extsize)
75 return ip->i_d.di_extsize;
76 if (XFS_IS_REALTIME_INODE(ip))
77 return ip->i_mount->m_sb.sb_rextsize;
78 return 0;
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96uint
97xfs_ilock_data_map_shared(
98 struct xfs_inode *ip)
99{
100 uint lock_mode = XFS_ILOCK_SHARED;
101
102 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE &&
103 (ip->i_df.if_flags & XFS_IFEXTENTS) == 0)
104 lock_mode = XFS_ILOCK_EXCL;
105 xfs_ilock(ip, lock_mode);
106 return lock_mode;
107}
108
109uint
110xfs_ilock_attr_map_shared(
111 struct xfs_inode *ip)
112{
113 uint lock_mode = XFS_ILOCK_SHARED;
114
115 if (ip->i_d.di_aformat == XFS_DINODE_FMT_BTREE &&
116 (ip->i_afp->if_flags & XFS_IFEXTENTS) == 0)
117 lock_mode = XFS_ILOCK_EXCL;
118 xfs_ilock(ip, lock_mode);
119 return lock_mode;
120}
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152void
153xfs_ilock(
154 xfs_inode_t *ip,
155 uint lock_flags)
156{
157 trace_xfs_ilock(ip, lock_flags, _RET_IP_);
158
159
160
161
162
163
164 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
165 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
166 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
167 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
168 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
169 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
170 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
171
172 if (lock_flags & XFS_IOLOCK_EXCL)
173 mrupdate_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
174 else if (lock_flags & XFS_IOLOCK_SHARED)
175 mraccess_nested(&ip->i_iolock, XFS_IOLOCK_DEP(lock_flags));
176
177 if (lock_flags & XFS_MMAPLOCK_EXCL)
178 mrupdate_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
179 else if (lock_flags & XFS_MMAPLOCK_SHARED)
180 mraccess_nested(&ip->i_mmaplock, XFS_MMAPLOCK_DEP(lock_flags));
181
182 if (lock_flags & XFS_ILOCK_EXCL)
183 mrupdate_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
184 else if (lock_flags & XFS_ILOCK_SHARED)
185 mraccess_nested(&ip->i_lock, XFS_ILOCK_DEP(lock_flags));
186}
187
188
189
190
191
192
193
194
195
196
197
198
199
200int
201xfs_ilock_nowait(
202 xfs_inode_t *ip,
203 uint lock_flags)
204{
205 trace_xfs_ilock_nowait(ip, lock_flags, _RET_IP_);
206
207
208
209
210
211
212 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
213 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
214 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
215 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
216 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
217 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
218 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
219
220 if (lock_flags & XFS_IOLOCK_EXCL) {
221 if (!mrtryupdate(&ip->i_iolock))
222 goto out;
223 } else if (lock_flags & XFS_IOLOCK_SHARED) {
224 if (!mrtryaccess(&ip->i_iolock))
225 goto out;
226 }
227
228 if (lock_flags & XFS_MMAPLOCK_EXCL) {
229 if (!mrtryupdate(&ip->i_mmaplock))
230 goto out_undo_iolock;
231 } else if (lock_flags & XFS_MMAPLOCK_SHARED) {
232 if (!mrtryaccess(&ip->i_mmaplock))
233 goto out_undo_iolock;
234 }
235
236 if (lock_flags & XFS_ILOCK_EXCL) {
237 if (!mrtryupdate(&ip->i_lock))
238 goto out_undo_mmaplock;
239 } else if (lock_flags & XFS_ILOCK_SHARED) {
240 if (!mrtryaccess(&ip->i_lock))
241 goto out_undo_mmaplock;
242 }
243 return 1;
244
245out_undo_mmaplock:
246 if (lock_flags & XFS_MMAPLOCK_EXCL)
247 mrunlock_excl(&ip->i_mmaplock);
248 else if (lock_flags & XFS_MMAPLOCK_SHARED)
249 mrunlock_shared(&ip->i_mmaplock);
250out_undo_iolock:
251 if (lock_flags & XFS_IOLOCK_EXCL)
252 mrunlock_excl(&ip->i_iolock);
253 else if (lock_flags & XFS_IOLOCK_SHARED)
254 mrunlock_shared(&ip->i_iolock);
255out:
256 return 0;
257}
258
259
260
261
262
263
264
265
266
267
268
269
270
271void
272xfs_iunlock(
273 xfs_inode_t *ip,
274 uint lock_flags)
275{
276
277
278
279
280
281 ASSERT((lock_flags & (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL)) !=
282 (XFS_IOLOCK_SHARED | XFS_IOLOCK_EXCL));
283 ASSERT((lock_flags & (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL)) !=
284 (XFS_MMAPLOCK_SHARED | XFS_MMAPLOCK_EXCL));
285 ASSERT((lock_flags & (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL)) !=
286 (XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
287 ASSERT((lock_flags & ~(XFS_LOCK_MASK | XFS_LOCK_SUBCLASS_MASK)) == 0);
288 ASSERT(lock_flags != 0);
289
290 if (lock_flags & XFS_IOLOCK_EXCL)
291 mrunlock_excl(&ip->i_iolock);
292 else if (lock_flags & XFS_IOLOCK_SHARED)
293 mrunlock_shared(&ip->i_iolock);
294
295 if (lock_flags & XFS_MMAPLOCK_EXCL)
296 mrunlock_excl(&ip->i_mmaplock);
297 else if (lock_flags & XFS_MMAPLOCK_SHARED)
298 mrunlock_shared(&ip->i_mmaplock);
299
300 if (lock_flags & XFS_ILOCK_EXCL)
301 mrunlock_excl(&ip->i_lock);
302 else if (lock_flags & XFS_ILOCK_SHARED)
303 mrunlock_shared(&ip->i_lock);
304
305 trace_xfs_iunlock(ip, lock_flags, _RET_IP_);
306}
307
308
309
310
311
312void
313xfs_ilock_demote(
314 xfs_inode_t *ip,
315 uint lock_flags)
316{
317 ASSERT(lock_flags & (XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL));
318 ASSERT((lock_flags &
319 ~(XFS_IOLOCK_EXCL|XFS_MMAPLOCK_EXCL|XFS_ILOCK_EXCL)) == 0);
320
321 if (lock_flags & XFS_ILOCK_EXCL)
322 mrdemote(&ip->i_lock);
323 if (lock_flags & XFS_MMAPLOCK_EXCL)
324 mrdemote(&ip->i_mmaplock);
325 if (lock_flags & XFS_IOLOCK_EXCL)
326 mrdemote(&ip->i_iolock);
327
328 trace_xfs_ilock_demote(ip, lock_flags, _RET_IP_);
329}
330
331#if defined(DEBUG) || defined(XFS_WARN)
332int
333xfs_isilocked(
334 xfs_inode_t *ip,
335 uint lock_flags)
336{
337 if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
338 if (!(lock_flags & XFS_ILOCK_SHARED))
339 return !!ip->i_lock.mr_writer;
340 return rwsem_is_locked(&ip->i_lock.mr_lock);
341 }
342
343 if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
344 if (!(lock_flags & XFS_MMAPLOCK_SHARED))
345 return !!ip->i_mmaplock.mr_writer;
346 return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
347 }
348
349 if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED)) {
350 if (!(lock_flags & XFS_IOLOCK_SHARED))
351 return !!ip->i_iolock.mr_writer;
352 return rwsem_is_locked(&ip->i_iolock.mr_lock);
353 }
354
355 ASSERT(0);
356 return 0;
357}
358#endif
359
360
361
362
363
364
365
366#if (defined(DEBUG) || defined(XFS_WARN)) && defined(CONFIG_LOCKDEP)
367static bool
368xfs_lockdep_subclass_ok(
369 int subclass)
370{
371 return subclass < MAX_LOCKDEP_SUBCLASSES;
372}
373#else
374#define xfs_lockdep_subclass_ok(subclass) (true)
375#endif
376
377
378
379
380
381
382
383static inline int
384xfs_lock_inumorder(int lock_mode, int subclass)
385{
386 int class = 0;
387
388 ASSERT(!(lock_mode & (XFS_ILOCK_PARENT | XFS_ILOCK_RTBITMAP |
389 XFS_ILOCK_RTSUM)));
390 ASSERT(xfs_lockdep_subclass_ok(subclass));
391
392 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
393 ASSERT(subclass <= XFS_IOLOCK_MAX_SUBCLASS);
394 ASSERT(xfs_lockdep_subclass_ok(subclass +
395 XFS_IOLOCK_PARENT_VAL));
396 class += subclass << XFS_IOLOCK_SHIFT;
397 if (lock_mode & XFS_IOLOCK_PARENT)
398 class += XFS_IOLOCK_PARENT_VAL << XFS_IOLOCK_SHIFT;
399 }
400
401 if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)) {
402 ASSERT(subclass <= XFS_MMAPLOCK_MAX_SUBCLASS);
403 class += subclass << XFS_MMAPLOCK_SHIFT;
404 }
405
406 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) {
407 ASSERT(subclass <= XFS_ILOCK_MAX_SUBCLASS);
408 class += subclass << XFS_ILOCK_SHIFT;
409 }
410
411 return (lock_mode & ~XFS_LOCK_SUBCLASS_MASK) | class;
412}
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429static void
430xfs_lock_inodes(
431 xfs_inode_t **ips,
432 int inodes,
433 uint lock_mode)
434{
435 int attempts = 0, i, j, try_lock;
436 xfs_log_item_t *lp;
437
438
439
440
441
442
443
444
445 ASSERT(ips && inodes >= 2 && inodes <= 5);
446 ASSERT(lock_mode & (XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL |
447 XFS_ILOCK_EXCL));
448 ASSERT(!(lock_mode & (XFS_IOLOCK_SHARED | XFS_MMAPLOCK_SHARED |
449 XFS_ILOCK_SHARED)));
450 ASSERT(!(lock_mode & XFS_IOLOCK_EXCL) ||
451 inodes <= XFS_IOLOCK_MAX_SUBCLASS + 1);
452 ASSERT(!(lock_mode & XFS_MMAPLOCK_EXCL) ||
453 inodes <= XFS_MMAPLOCK_MAX_SUBCLASS + 1);
454 ASSERT(!(lock_mode & XFS_ILOCK_EXCL) ||
455 inodes <= XFS_ILOCK_MAX_SUBCLASS + 1);
456
457 if (lock_mode & XFS_IOLOCK_EXCL) {
458 ASSERT(!(lock_mode & (XFS_MMAPLOCK_EXCL | XFS_ILOCK_EXCL)));
459 } else if (lock_mode & XFS_MMAPLOCK_EXCL)
460 ASSERT(!(lock_mode & XFS_ILOCK_EXCL));
461
462 try_lock = 0;
463 i = 0;
464again:
465 for (; i < inodes; i++) {
466 ASSERT(ips[i]);
467
468 if (i && (ips[i] == ips[i - 1]))
469 continue;
470
471
472
473
474
475 if (!try_lock) {
476 for (j = (i - 1); j >= 0 && !try_lock; j--) {
477 lp = (xfs_log_item_t *)ips[j]->i_itemp;
478 if (lp && (lp->li_flags & XFS_LI_IN_AIL))
479 try_lock++;
480 }
481 }
482
483
484
485
486
487
488
489 if (!try_lock) {
490 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
491 continue;
492 }
493
494
495 ASSERT(i != 0);
496 if (xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i)))
497 continue;
498
499
500
501
502
503 attempts++;
504 for (j = i - 1; j >= 0; j--) {
505
506
507
508
509
510 if (j != (i - 1) && ips[j] == ips[j + 1])
511 continue;
512
513 xfs_iunlock(ips[j], lock_mode);
514 }
515
516 if ((attempts % 5) == 0) {
517 delay(1);
518 }
519 i = 0;
520 try_lock = 0;
521 goto again;
522 }
523}
524
525
526
527
528
529
530
531void
532xfs_lock_two_inodes(
533 xfs_inode_t *ip0,
534 xfs_inode_t *ip1,
535 uint lock_mode)
536{
537 xfs_inode_t *temp;
538 int attempts = 0;
539 xfs_log_item_t *lp;
540
541 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL)) {
542 ASSERT(!(lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL)));
543 ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
544 } else if (lock_mode & (XFS_MMAPLOCK_SHARED|XFS_MMAPLOCK_EXCL))
545 ASSERT(!(lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)));
546
547 ASSERT(ip0->i_ino != ip1->i_ino);
548
549 if (ip0->i_ino > ip1->i_ino) {
550 temp = ip0;
551 ip0 = ip1;
552 ip1 = temp;
553 }
554
555 again:
556 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
557
558
559
560
561
562
563 lp = (xfs_log_item_t *)ip0->i_itemp;
564 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
565 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
566 xfs_iunlock(ip0, lock_mode);
567 if ((++attempts % 5) == 0)
568 delay(1);
569 goto again;
570 }
571 } else {
572 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
573 }
574}
575
576
577void
578__xfs_iflock(
579 struct xfs_inode *ip)
580{
581 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IFLOCK_BIT);
582 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IFLOCK_BIT);
583
584 do {
585 prepare_to_wait_exclusive(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
586 if (xfs_isiflocked(ip))
587 io_schedule();
588 } while (!xfs_iflock_nowait(ip));
589
590 finish_wait(wq, &wait.wait);
591}
592
593STATIC uint
594_xfs_dic2xflags(
595 uint16_t di_flags,
596 uint64_t di_flags2,
597 bool has_attr)
598{
599 uint flags = 0;
600
601 if (di_flags & XFS_DIFLAG_ANY) {
602 if (di_flags & XFS_DIFLAG_REALTIME)
603 flags |= XFS_XFLAG_REALTIME;
604 if (di_flags & XFS_DIFLAG_PREALLOC)
605 flags |= XFS_XFLAG_PREALLOC;
606 if (di_flags & XFS_DIFLAG_IMMUTABLE)
607 flags |= XFS_XFLAG_IMMUTABLE;
608 if (di_flags & XFS_DIFLAG_APPEND)
609 flags |= XFS_XFLAG_APPEND;
610 if (di_flags & XFS_DIFLAG_SYNC)
611 flags |= XFS_XFLAG_SYNC;
612 if (di_flags & XFS_DIFLAG_NOATIME)
613 flags |= XFS_XFLAG_NOATIME;
614 if (di_flags & XFS_DIFLAG_NODUMP)
615 flags |= XFS_XFLAG_NODUMP;
616 if (di_flags & XFS_DIFLAG_RTINHERIT)
617 flags |= XFS_XFLAG_RTINHERIT;
618 if (di_flags & XFS_DIFLAG_PROJINHERIT)
619 flags |= XFS_XFLAG_PROJINHERIT;
620 if (di_flags & XFS_DIFLAG_NOSYMLINKS)
621 flags |= XFS_XFLAG_NOSYMLINKS;
622 if (di_flags & XFS_DIFLAG_EXTSIZE)
623 flags |= XFS_XFLAG_EXTSIZE;
624 if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
625 flags |= XFS_XFLAG_EXTSZINHERIT;
626 if (di_flags & XFS_DIFLAG_NODEFRAG)
627 flags |= XFS_XFLAG_NODEFRAG;
628 if (di_flags & XFS_DIFLAG_FILESTREAM)
629 flags |= XFS_XFLAG_FILESTREAM;
630 }
631
632 if (has_attr)
633 flags |= XFS_XFLAG_HASATTR;
634
635 return flags;
636}
637
638uint
639xfs_ip2xflags(
640 struct xfs_inode *ip)
641{
642 struct xfs_icdinode *dic = &ip->i_d;
643
644 return _xfs_dic2xflags(dic->di_flags, dic->di_flags2, XFS_IFORK_Q(ip));
645}
646
647
648
649
650
651
652
653int
654xfs_lookup(
655 xfs_inode_t *dp,
656 struct xfs_name *name,
657 xfs_inode_t **ipp,
658 struct xfs_name *ci_name)
659{
660 xfs_ino_t inum;
661 int error;
662
663 trace_xfs_lookup(dp, name);
664
665 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
666 return -EIO;
667
668 xfs_ilock(dp, XFS_IOLOCK_SHARED);
669 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
670 if (error)
671 goto out_unlock;
672
673 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
674 if (error)
675 goto out_free_name;
676
677 xfs_iunlock(dp, XFS_IOLOCK_SHARED);
678 return 0;
679
680out_free_name:
681 if (ci_name)
682 kmem_free(ci_name->name);
683out_unlock:
684 xfs_iunlock(dp, XFS_IOLOCK_SHARED);
685 *ipp = NULL;
686 return error;
687}
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720static int
721xfs_ialloc(
722 xfs_trans_t *tp,
723 xfs_inode_t *pip,
724 umode_t mode,
725 xfs_nlink_t nlink,
726 dev_t rdev,
727 prid_t prid,
728 xfs_buf_t **ialloc_context,
729 xfs_inode_t **ipp)
730{
731 struct xfs_mount *mp = tp->t_mountp;
732 xfs_ino_t ino;
733 xfs_inode_t *ip;
734 uint flags;
735 int error;
736 struct timespec tv;
737 struct inode *inode;
738
739
740
741
742
743 error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode,
744 ialloc_context, &ino);
745 if (error)
746 return error;
747 if (*ialloc_context || ino == NULLFSINO) {
748 *ipp = NULL;
749 return 0;
750 }
751 ASSERT(*ialloc_context == NULL);
752
753
754
755
756
757
758 error = xfs_iget(mp, tp, ino, XFS_IGET_CREATE,
759 XFS_ILOCK_EXCL, &ip);
760 if (error)
761 return error;
762 ASSERT(ip != NULL);
763 inode = VFS_I(ip);
764
765
766
767
768
769
770 if (ip->i_d.di_version == 1)
771 ip->i_d.di_version = 2;
772
773 inode->i_mode = mode;
774 set_nlink(inode, nlink);
775 ip->i_d.di_uid = xfs_kuid_to_uid(current_fsuid());
776 ip->i_d.di_gid = xfs_kgid_to_gid(current_fsgid());
777 inode->i_rdev = rdev;
778 xfs_set_projid(ip, prid);
779
780 if (pip && XFS_INHERIT_GID(pip)) {
781 ip->i_d.di_gid = pip->i_d.di_gid;
782 if ((VFS_I(pip)->i_mode & S_ISGID) && S_ISDIR(mode))
783 inode->i_mode |= S_ISGID;
784 }
785
786
787
788
789
790
791 if ((irix_sgid_inherit) &&
792 (inode->i_mode & S_ISGID) &&
793 (!in_group_p(xfs_gid_to_kgid(ip->i_d.di_gid))))
794 inode->i_mode &= ~S_ISGID;
795
796 ip->i_d.di_size = 0;
797 ip->i_d.di_nextents = 0;
798 ASSERT(ip->i_d.di_nblocks == 0);
799
800 tv = current_fs_time(mp->m_super);
801 inode->i_mtime = tv;
802 inode->i_atime = tv;
803 inode->i_ctime = tv;
804
805 ip->i_d.di_extsize = 0;
806 ip->i_d.di_dmevmask = 0;
807 ip->i_d.di_dmstate = 0;
808 ip->i_d.di_flags = 0;
809
810 if (ip->i_d.di_version == 3) {
811 inode->i_version = 1;
812 ip->i_d.di_flags2 = 0;
813 ip->i_d.di_crtime.t_sec = (int32_t)tv.tv_sec;
814 ip->i_d.di_crtime.t_nsec = (int32_t)tv.tv_nsec;
815 }
816
817
818 flags = XFS_ILOG_CORE;
819 switch (mode & S_IFMT) {
820 case S_IFIFO:
821 case S_IFCHR:
822 case S_IFBLK:
823 case S_IFSOCK:
824 ip->i_d.di_format = XFS_DINODE_FMT_DEV;
825 ip->i_df.if_flags = 0;
826 flags |= XFS_ILOG_DEV;
827 break;
828 case S_IFREG:
829 case S_IFDIR:
830 if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
831 uint di_flags = 0;
832
833 if (S_ISDIR(mode)) {
834 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
835 di_flags |= XFS_DIFLAG_RTINHERIT;
836 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
837 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
838 ip->i_d.di_extsize = pip->i_d.di_extsize;
839 }
840 if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
841 di_flags |= XFS_DIFLAG_PROJINHERIT;
842 } else if (S_ISREG(mode)) {
843 if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
844 di_flags |= XFS_DIFLAG_REALTIME;
845 if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
846 di_flags |= XFS_DIFLAG_EXTSIZE;
847 ip->i_d.di_extsize = pip->i_d.di_extsize;
848 }
849 }
850 if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
851 xfs_inherit_noatime)
852 di_flags |= XFS_DIFLAG_NOATIME;
853 if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
854 xfs_inherit_nodump)
855 di_flags |= XFS_DIFLAG_NODUMP;
856 if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
857 xfs_inherit_sync)
858 di_flags |= XFS_DIFLAG_SYNC;
859 if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
860 xfs_inherit_nosymlinks)
861 di_flags |= XFS_DIFLAG_NOSYMLINKS;
862 if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
863 xfs_inherit_nodefrag)
864 di_flags |= XFS_DIFLAG_NODEFRAG;
865 if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
866 di_flags |= XFS_DIFLAG_FILESTREAM;
867 ip->i_d.di_flags |= di_flags;
868 }
869
870 case S_IFLNK:
871 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
872 ip->i_df.if_flags = XFS_IFEXTENTS;
873 ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
874 ip->i_df.if_u1.if_root = NULL;
875 break;
876 default:
877 ASSERT(0);
878 }
879
880
881
882 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
883 ip->i_d.di_anextents = 0;
884
885
886
887
888 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
889 xfs_trans_log_inode(tp, ip, flags);
890
891
892 xfs_setup_inode(ip);
893
894 *ipp = ip;
895 return 0;
896}
897
898
899
900
901
902
903
904
905
906
907
908int
909xfs_dir_ialloc(
910 xfs_trans_t **tpp,
911
912 xfs_inode_t *dp,
913
914 umode_t mode,
915 xfs_nlink_t nlink,
916 dev_t rdev,
917 prid_t prid,
918 xfs_inode_t **ipp,
919
920 int *committed)
921
922{
923 xfs_trans_t *tp;
924 xfs_inode_t *ip;
925 xfs_buf_t *ialloc_context = NULL;
926 int code;
927 void *dqinfo;
928 uint tflags;
929
930 tp = *tpp;
931 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid, &ialloc_context,
949 &ip);
950
951
952
953
954
955
956 if (code) {
957 *ipp = NULL;
958 return code;
959 }
960 if (!ialloc_context && !ip) {
961 *ipp = NULL;
962 return -ENOSPC;
963 }
964
965
966
967
968
969
970
971 if (ialloc_context) {
972
973
974
975
976
977
978
979 xfs_trans_bhold(tp, ialloc_context);
980
981
982
983
984
985
986 dqinfo = NULL;
987 tflags = 0;
988 if (tp->t_dqinfo) {
989 dqinfo = (void *)tp->t_dqinfo;
990 tp->t_dqinfo = NULL;
991 tflags = tp->t_flags & XFS_TRANS_DQ_DIRTY;
992 tp->t_flags &= ~(XFS_TRANS_DQ_DIRTY);
993 }
994
995 code = xfs_trans_roll(&tp);
996 if (committed != NULL)
997 *committed = 1;
998
999
1000
1001
1002 if (dqinfo) {
1003 tp->t_dqinfo = dqinfo;
1004 tp->t_flags |= tflags;
1005 }
1006
1007 if (code) {
1008 xfs_buf_relse(ialloc_context);
1009 *tpp = tp;
1010 *ipp = NULL;
1011 return code;
1012 }
1013 xfs_trans_bjoin(tp, ialloc_context);
1014
1015
1016
1017
1018
1019
1020 code = xfs_ialloc(tp, dp, mode, nlink, rdev, prid,
1021 &ialloc_context, &ip);
1022
1023
1024
1025
1026
1027 if (code) {
1028 *tpp = tp;
1029 *ipp = NULL;
1030 return code;
1031 }
1032 ASSERT(!ialloc_context && ip);
1033
1034 } else {
1035 if (committed != NULL)
1036 *committed = 0;
1037 }
1038
1039 *ipp = ip;
1040 *tpp = tp;
1041
1042 return 0;
1043}
1044
1045
1046
1047
1048
1049
1050static int
1051xfs_droplink(
1052 xfs_trans_t *tp,
1053 xfs_inode_t *ip)
1054{
1055 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1056
1057 drop_nlink(VFS_I(ip));
1058 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1059
1060 if (VFS_I(ip)->i_nlink)
1061 return 0;
1062
1063 return xfs_iunlink(tp, ip);
1064}
1065
1066
1067
1068
1069static int
1070xfs_bumplink(
1071 xfs_trans_t *tp,
1072 xfs_inode_t *ip)
1073{
1074 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
1075
1076 ASSERT(ip->i_d.di_version > 1);
1077 inc_nlink(VFS_I(ip));
1078 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1079 return 0;
1080}
1081
1082int
1083xfs_create(
1084 xfs_inode_t *dp,
1085 struct xfs_name *name,
1086 umode_t mode,
1087 dev_t rdev,
1088 xfs_inode_t **ipp)
1089{
1090 int is_dir = S_ISDIR(mode);
1091 struct xfs_mount *mp = dp->i_mount;
1092 struct xfs_inode *ip = NULL;
1093 struct xfs_trans *tp = NULL;
1094 int error;
1095 struct xfs_defer_ops dfops;
1096 xfs_fsblock_t first_block;
1097 bool unlock_dp_on_error = false;
1098 prid_t prid;
1099 struct xfs_dquot *udqp = NULL;
1100 struct xfs_dquot *gdqp = NULL;
1101 struct xfs_dquot *pdqp = NULL;
1102 struct xfs_trans_res *tres;
1103 uint resblks;
1104
1105 trace_xfs_create(dp, name);
1106
1107 if (XFS_FORCED_SHUTDOWN(mp))
1108 return -EIO;
1109
1110 prid = xfs_get_initial_prid(dp);
1111
1112
1113
1114
1115 error = xfs_qm_vop_dqalloc(dp,
1116 xfs_kuid_to_uid(current_fsuid()),
1117 xfs_kgid_to_gid(current_fsgid()), prid,
1118 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1119 &udqp, &gdqp, &pdqp);
1120 if (error)
1121 return error;
1122
1123 if (is_dir) {
1124 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
1125 tres = &M_RES(mp)->tr_mkdir;
1126 } else {
1127 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1128 tres = &M_RES(mp)->tr_create;
1129 }
1130
1131
1132
1133
1134
1135
1136
1137 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1138 if (error == -ENOSPC) {
1139
1140 xfs_flush_inodes(mp);
1141 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1142 }
1143 if (error)
1144 goto out_release_inode;
1145
1146 xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL |
1147 XFS_IOLOCK_PARENT | XFS_ILOCK_PARENT);
1148 unlock_dp_on_error = true;
1149
1150 xfs_defer_init(&dfops, &first_block);
1151
1152
1153
1154
1155 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1156 pdqp, resblks, 1, 0);
1157 if (error)
1158 goto out_trans_cancel;
1159
1160
1161
1162
1163
1164
1165 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev, prid, &ip,
1166 NULL);
1167 if (error)
1168 goto out_trans_cancel;
1169
1170
1171
1172
1173
1174
1175
1176
1177 xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1178 unlock_dp_on_error = false;
1179
1180 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1181 &first_block, &dfops, resblks ?
1182 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1183 if (error) {
1184 ASSERT(error != -ENOSPC);
1185 goto out_trans_cancel;
1186 }
1187 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1188 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1189
1190 if (is_dir) {
1191 error = xfs_dir_init(tp, ip, dp);
1192 if (error)
1193 goto out_bmap_cancel;
1194
1195 error = xfs_bumplink(tp, dp);
1196 if (error)
1197 goto out_bmap_cancel;
1198 }
1199
1200
1201
1202
1203
1204
1205 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1206 xfs_trans_set_sync(tp);
1207
1208
1209
1210
1211
1212
1213 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1214
1215 error = xfs_defer_finish(&tp, &dfops);
1216 if (error)
1217 goto out_bmap_cancel;
1218
1219 error = xfs_trans_commit(tp);
1220 if (error)
1221 goto out_release_inode;
1222
1223 xfs_qm_dqrele(udqp);
1224 xfs_qm_dqrele(gdqp);
1225 xfs_qm_dqrele(pdqp);
1226
1227 *ipp = ip;
1228 return 0;
1229
1230 out_bmap_cancel:
1231 xfs_defer_cancel(&dfops);
1232 out_trans_cancel:
1233 xfs_trans_cancel(tp);
1234 out_release_inode:
1235
1236
1237
1238
1239
1240 if (ip) {
1241 xfs_finish_inode_setup(ip);
1242 IRELE(ip);
1243 }
1244
1245 xfs_qm_dqrele(udqp);
1246 xfs_qm_dqrele(gdqp);
1247 xfs_qm_dqrele(pdqp);
1248
1249 if (unlock_dp_on_error)
1250 xfs_iunlock(dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1251 return error;
1252}
1253
1254int
1255xfs_create_tmpfile(
1256 struct xfs_inode *dp,
1257 struct dentry *dentry,
1258 umode_t mode,
1259 struct xfs_inode **ipp)
1260{
1261 struct xfs_mount *mp = dp->i_mount;
1262 struct xfs_inode *ip = NULL;
1263 struct xfs_trans *tp = NULL;
1264 int error;
1265 prid_t prid;
1266 struct xfs_dquot *udqp = NULL;
1267 struct xfs_dquot *gdqp = NULL;
1268 struct xfs_dquot *pdqp = NULL;
1269 struct xfs_trans_res *tres;
1270 uint resblks;
1271
1272 if (XFS_FORCED_SHUTDOWN(mp))
1273 return -EIO;
1274
1275 prid = xfs_get_initial_prid(dp);
1276
1277
1278
1279
1280 error = xfs_qm_vop_dqalloc(dp, xfs_kuid_to_uid(current_fsuid()),
1281 xfs_kgid_to_gid(current_fsgid()), prid,
1282 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
1283 &udqp, &gdqp, &pdqp);
1284 if (error)
1285 return error;
1286
1287 resblks = XFS_IALLOC_SPACE_RES(mp);
1288 tres = &M_RES(mp)->tr_create_tmpfile;
1289
1290 error = xfs_trans_alloc(mp, tres, resblks, 0, 0, &tp);
1291 if (error)
1292 goto out_release_inode;
1293
1294 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
1295 pdqp, resblks, 1, 0);
1296 if (error)
1297 goto out_trans_cancel;
1298
1299 error = xfs_dir_ialloc(&tp, dp, mode, 1, 0, prid, &ip, NULL);
1300 if (error)
1301 goto out_trans_cancel;
1302
1303 if (mp->m_flags & XFS_MOUNT_WSYNC)
1304 xfs_trans_set_sync(tp);
1305
1306
1307
1308
1309
1310
1311 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
1312
1313 error = xfs_iunlink(tp, ip);
1314 if (error)
1315 goto out_trans_cancel;
1316
1317 error = xfs_trans_commit(tp);
1318 if (error)
1319 goto out_release_inode;
1320
1321 xfs_qm_dqrele(udqp);
1322 xfs_qm_dqrele(gdqp);
1323 xfs_qm_dqrele(pdqp);
1324
1325 *ipp = ip;
1326 return 0;
1327
1328 out_trans_cancel:
1329 xfs_trans_cancel(tp);
1330 out_release_inode:
1331
1332
1333
1334
1335
1336 if (ip) {
1337 xfs_finish_inode_setup(ip);
1338 IRELE(ip);
1339 }
1340
1341 xfs_qm_dqrele(udqp);
1342 xfs_qm_dqrele(gdqp);
1343 xfs_qm_dqrele(pdqp);
1344
1345 return error;
1346}
1347
1348int
1349xfs_link(
1350 xfs_inode_t *tdp,
1351 xfs_inode_t *sip,
1352 struct xfs_name *target_name)
1353{
1354 xfs_mount_t *mp = tdp->i_mount;
1355 xfs_trans_t *tp;
1356 int error;
1357 struct xfs_defer_ops dfops;
1358 xfs_fsblock_t first_block;
1359 int resblks;
1360
1361 trace_xfs_link(tdp, target_name);
1362
1363 ASSERT(!S_ISDIR(VFS_I(sip)->i_mode));
1364
1365 if (XFS_FORCED_SHUTDOWN(mp))
1366 return -EIO;
1367
1368 error = xfs_qm_dqattach(sip, 0);
1369 if (error)
1370 goto std_return;
1371
1372 error = xfs_qm_dqattach(tdp, 0);
1373 if (error)
1374 goto std_return;
1375
1376 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1377 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, resblks, 0, 0, &tp);
1378 if (error == -ENOSPC) {
1379 resblks = 0;
1380 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_link, 0, 0, 0, &tp);
1381 }
1382 if (error)
1383 goto std_return;
1384
1385 xfs_ilock(tdp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
1386 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1387
1388 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1389 xfs_trans_ijoin(tp, tdp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1390
1391
1392
1393
1394
1395
1396 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1397 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1398 error = -EXDEV;
1399 goto error_return;
1400 }
1401
1402 if (!resblks) {
1403 error = xfs_dir_canenter(tp, tdp, target_name);
1404 if (error)
1405 goto error_return;
1406 }
1407
1408 xfs_defer_init(&dfops, &first_block);
1409
1410
1411
1412
1413 if (VFS_I(sip)->i_nlink == 0) {
1414 error = xfs_iunlink_remove(tp, sip);
1415 if (error)
1416 goto error_return;
1417 }
1418
1419 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1420 &first_block, &dfops, resblks);
1421 if (error)
1422 goto error_return;
1423 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1424 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1425
1426 error = xfs_bumplink(tp, sip);
1427 if (error)
1428 goto error_return;
1429
1430
1431
1432
1433
1434
1435 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1436 xfs_trans_set_sync(tp);
1437
1438 error = xfs_defer_finish(&tp, &dfops);
1439 if (error) {
1440 xfs_defer_cancel(&dfops);
1441 goto error_return;
1442 }
1443
1444 return xfs_trans_commit(tp);
1445
1446 error_return:
1447 xfs_trans_cancel(tp);
1448 std_return:
1449 return error;
1450}
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473int
1474xfs_itruncate_extents(
1475 struct xfs_trans **tpp,
1476 struct xfs_inode *ip,
1477 int whichfork,
1478 xfs_fsize_t new_size)
1479{
1480 struct xfs_mount *mp = ip->i_mount;
1481 struct xfs_trans *tp = *tpp;
1482 struct xfs_defer_ops dfops;
1483 xfs_fsblock_t first_block;
1484 xfs_fileoff_t first_unmap_block;
1485 xfs_fileoff_t last_block;
1486 xfs_filblks_t unmap_len;
1487 int error = 0;
1488 int done = 0;
1489
1490 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1491 ASSERT(!atomic_read(&VFS_I(ip)->i_count) ||
1492 xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1493 ASSERT(new_size <= XFS_ISIZE(ip));
1494 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1495 ASSERT(ip->i_itemp != NULL);
1496 ASSERT(ip->i_itemp->ili_lock_flags == 0);
1497 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1498
1499 trace_xfs_itruncate_extents_start(ip, new_size);
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510 first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
1511 last_block = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
1512 if (first_unmap_block == last_block)
1513 return 0;
1514
1515 ASSERT(first_unmap_block < last_block);
1516 unmap_len = last_block - first_unmap_block + 1;
1517 while (!done) {
1518 xfs_defer_init(&dfops, &first_block);
1519 error = xfs_bunmapi(tp, ip,
1520 first_unmap_block, unmap_len,
1521 xfs_bmapi_aflag(whichfork),
1522 XFS_ITRUNC_MAX_EXTENTS,
1523 &first_block, &dfops,
1524 &done);
1525 if (error)
1526 goto out_bmap_cancel;
1527
1528
1529
1530
1531
1532 error = xfs_defer_finish(&tp, &dfops);
1533 if (error)
1534 goto out_bmap_cancel;
1535
1536 error = xfs_trans_roll_inode(&tp, ip);
1537 if (error)
1538 goto out;
1539 }
1540
1541
1542
1543
1544
1545 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1546
1547 trace_xfs_itruncate_extents_end(ip, new_size);
1548
1549out:
1550 *tpp = tp;
1551 return error;
1552out_bmap_cancel:
1553
1554
1555
1556
1557
1558 xfs_defer_cancel(&dfops);
1559 goto out;
1560}
1561
1562int
1563xfs_release(
1564 xfs_inode_t *ip)
1565{
1566 xfs_mount_t *mp = ip->i_mount;
1567 int error;
1568
1569 if (!S_ISREG(VFS_I(ip)->i_mode) || (VFS_I(ip)->i_mode == 0))
1570 return 0;
1571
1572
1573 if (mp->m_flags & XFS_MOUNT_RDONLY)
1574 return 0;
1575
1576 if (!XFS_FORCED_SHUTDOWN(mp)) {
1577 int truncated;
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1590 if (truncated) {
1591 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
1592 if (ip->i_delayed_blks > 0) {
1593 error = filemap_flush(VFS_I(ip)->i_mapping);
1594 if (error)
1595 return error;
1596 }
1597 }
1598 }
1599
1600 if (VFS_I(ip)->i_nlink == 0)
1601 return 0;
1602
1603 if (xfs_can_free_eofblocks(ip, false)) {
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
1620 return 0;
1621
1622
1623
1624
1625
1626
1627
1628 if (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
1629 error = xfs_free_eofblocks(ip);
1630 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1631 if (error)
1632 return error;
1633 }
1634
1635
1636 if (ip->i_delayed_blks)
1637 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
1638 }
1639 return 0;
1640}
1641
1642
1643
1644
1645
1646
1647STATIC int
1648xfs_inactive_truncate(
1649 struct xfs_inode *ip)
1650{
1651 struct xfs_mount *mp = ip->i_mount;
1652 struct xfs_trans *tp;
1653 int error;
1654
1655 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
1656 if (error) {
1657 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1658 return error;
1659 }
1660
1661 xfs_ilock(ip, XFS_ILOCK_EXCL);
1662 xfs_trans_ijoin(tp, ip, 0);
1663
1664
1665
1666
1667
1668
1669 ip->i_d.di_size = 0;
1670 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1671
1672 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
1673 if (error)
1674 goto error_trans_cancel;
1675
1676 ASSERT(ip->i_d.di_nextents == 0);
1677
1678 error = xfs_trans_commit(tp);
1679 if (error)
1680 goto error_unlock;
1681
1682 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1683 return 0;
1684
1685error_trans_cancel:
1686 xfs_trans_cancel(tp);
1687error_unlock:
1688 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1689 return error;
1690}
1691
1692
1693
1694
1695
1696
1697STATIC int
1698xfs_inactive_ifree(
1699 struct xfs_inode *ip)
1700{
1701 struct xfs_defer_ops dfops;
1702 xfs_fsblock_t first_block;
1703 struct xfs_mount *mp = ip->i_mount;
1704 struct xfs_trans *tp;
1705 int error;
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ifree,
1723 XFS_IFREE_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp);
1724 if (error) {
1725 if (error == -ENOSPC) {
1726 xfs_warn_ratelimited(mp,
1727 "Failed to remove inode(s) from unlinked list. "
1728 "Please free space, unmount and run xfs_repair.");
1729 } else {
1730 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1731 }
1732 return error;
1733 }
1734
1735 xfs_ilock(ip, XFS_ILOCK_EXCL);
1736 xfs_trans_ijoin(tp, ip, 0);
1737
1738 xfs_defer_init(&dfops, &first_block);
1739 error = xfs_ifree(tp, ip, &dfops);
1740 if (error) {
1741
1742
1743
1744
1745
1746 if (!XFS_FORCED_SHUTDOWN(mp)) {
1747 xfs_notice(mp, "%s: xfs_ifree returned error %d",
1748 __func__, error);
1749 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1750 }
1751 xfs_trans_cancel(tp);
1752 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1753 return error;
1754 }
1755
1756
1757
1758
1759 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1760
1761
1762
1763
1764
1765 error = xfs_defer_finish(&tp, &dfops);
1766 if (error) {
1767 xfs_notice(mp, "%s: xfs_defer_finish returned error %d",
1768 __func__, error);
1769 xfs_defer_cancel(&dfops);
1770 }
1771 error = xfs_trans_commit(tp);
1772 if (error)
1773 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
1774 __func__, error);
1775
1776 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1777 return 0;
1778}
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788void
1789xfs_inactive(
1790 xfs_inode_t *ip)
1791{
1792 struct xfs_mount *mp;
1793 int error;
1794 int truncate = 0;
1795
1796
1797
1798
1799
1800 if (VFS_I(ip)->i_mode == 0) {
1801 ASSERT(ip->i_df.if_real_bytes == 0);
1802 ASSERT(ip->i_df.if_broot_bytes == 0);
1803 return;
1804 }
1805
1806 mp = ip->i_mount;
1807
1808
1809 if (mp->m_flags & XFS_MOUNT_RDONLY)
1810 return;
1811
1812 if (VFS_I(ip)->i_nlink != 0) {
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822 if (xfs_can_free_eofblocks(ip, true))
1823 xfs_free_eofblocks(ip);
1824
1825 return;
1826 }
1827
1828 if (S_ISREG(VFS_I(ip)->i_mode) &&
1829 (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
1830 ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
1831 truncate = 1;
1832
1833 error = xfs_qm_dqattach(ip, 0);
1834 if (error)
1835 return;
1836
1837 if (S_ISLNK(VFS_I(ip)->i_mode))
1838 error = xfs_inactive_symlink(ip);
1839 else if (truncate)
1840 error = xfs_inactive_truncate(ip);
1841 if (error)
1842 return;
1843
1844
1845
1846
1847
1848
1849 if (XFS_IFORK_Q(ip)) {
1850 error = xfs_attr_inactive(ip);
1851 if (error)
1852 return;
1853 }
1854
1855 ASSERT(!ip->i_afp);
1856 ASSERT(ip->i_d.di_anextents == 0);
1857 ASSERT(ip->i_d.di_forkoff == 0);
1858
1859
1860
1861
1862 error = xfs_inactive_ifree(ip);
1863 if (error)
1864 return;
1865
1866
1867
1868
1869 xfs_qm_dqdetach(ip);
1870}
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882STATIC int
1883xfs_iunlink(
1884 struct xfs_trans *tp,
1885 struct xfs_inode *ip)
1886{
1887 xfs_mount_t *mp = tp->t_mountp;
1888 xfs_agi_t *agi;
1889 xfs_dinode_t *dip;
1890 xfs_buf_t *agibp;
1891 xfs_buf_t *ibp;
1892 xfs_agino_t agino;
1893 short bucket_index;
1894 int offset;
1895 int error;
1896
1897 ASSERT(VFS_I(ip)->i_mode != 0);
1898
1899
1900
1901
1902
1903 error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
1904 if (error)
1905 return error;
1906 agi = XFS_BUF_TO_AGI(agibp);
1907
1908
1909
1910
1911
1912 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1913 ASSERT(agino != 0);
1914 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1915 ASSERT(agi->agi_unlinked[bucket_index]);
1916 ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
1917
1918 if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
1919
1920
1921
1922
1923
1924
1925 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
1926 0, 0);
1927 if (error)
1928 return error;
1929
1930 ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
1931 dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
1932 offset = ip->i_imap.im_boffset +
1933 offsetof(xfs_dinode_t, di_next_unlinked);
1934
1935
1936 xfs_dinode_calc_crc(mp, dip);
1937
1938 xfs_trans_inode_buf(tp, ibp);
1939 xfs_trans_log_buf(tp, ibp, offset,
1940 (offset + sizeof(xfs_agino_t) - 1));
1941 xfs_inobp_check(mp, ibp);
1942 }
1943
1944
1945
1946
1947 ASSERT(agino != 0);
1948 agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
1949 offset = offsetof(xfs_agi_t, agi_unlinked) +
1950 (sizeof(xfs_agino_t) * bucket_index);
1951 xfs_trans_log_buf(tp, agibp, offset,
1952 (offset + sizeof(xfs_agino_t) - 1));
1953 return 0;
1954}
1955
1956
1957
1958
1959STATIC int
1960xfs_iunlink_remove(
1961 xfs_trans_t *tp,
1962 xfs_inode_t *ip)
1963{
1964 xfs_ino_t next_ino;
1965 xfs_mount_t *mp;
1966 xfs_agi_t *agi;
1967 xfs_dinode_t *dip;
1968 xfs_buf_t *agibp;
1969 xfs_buf_t *ibp;
1970 xfs_agnumber_t agno;
1971 xfs_agino_t agino;
1972 xfs_agino_t next_agino;
1973 xfs_buf_t *last_ibp;
1974 xfs_dinode_t *last_dip = NULL;
1975 short bucket_index;
1976 int offset, last_offset = 0;
1977 int error;
1978
1979 mp = tp->t_mountp;
1980 agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
1981
1982
1983
1984
1985
1986 error = xfs_read_agi(mp, tp, agno, &agibp);
1987 if (error)
1988 return error;
1989
1990 agi = XFS_BUF_TO_AGI(agibp);
1991
1992
1993
1994
1995
1996 agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
1997 ASSERT(agino != 0);
1998 bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
1999 ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
2000 ASSERT(agi->agi_unlinked[bucket_index]);
2001
2002 if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
2003
2004
2005
2006
2007
2008
2009
2010 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2011 0, 0);
2012 if (error) {
2013 xfs_warn(mp, "%s: xfs_imap_to_bp returned error %d.",
2014 __func__, error);
2015 return error;
2016 }
2017 next_agino = be32_to_cpu(dip->di_next_unlinked);
2018 ASSERT(next_agino != 0);
2019 if (next_agino != NULLAGINO) {
2020 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
2021 offset = ip->i_imap.im_boffset +
2022 offsetof(xfs_dinode_t, di_next_unlinked);
2023
2024
2025 xfs_dinode_calc_crc(mp, dip);
2026
2027 xfs_trans_inode_buf(tp, ibp);
2028 xfs_trans_log_buf(tp, ibp, offset,
2029 (offset + sizeof(xfs_agino_t) - 1));
2030 xfs_inobp_check(mp, ibp);
2031 } else {
2032 xfs_trans_brelse(tp, ibp);
2033 }
2034
2035
2036
2037 ASSERT(next_agino != 0);
2038 ASSERT(next_agino != agino);
2039 agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
2040 offset = offsetof(xfs_agi_t, agi_unlinked) +
2041 (sizeof(xfs_agino_t) * bucket_index);
2042 xfs_trans_log_buf(tp, agibp, offset,
2043 (offset + sizeof(xfs_agino_t) - 1));
2044 } else {
2045
2046
2047
2048 next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
2049 last_ibp = NULL;
2050 while (next_agino != agino) {
2051 struct xfs_imap imap;
2052
2053 if (last_ibp)
2054 xfs_trans_brelse(tp, last_ibp);
2055
2056 imap.im_blkno = 0;
2057 next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
2058
2059 error = xfs_imap(mp, tp, next_ino, &imap, 0);
2060 if (error) {
2061 xfs_warn(mp,
2062 "%s: xfs_imap returned error %d.",
2063 __func__, error);
2064 return error;
2065 }
2066
2067 error = xfs_imap_to_bp(mp, tp, &imap, &last_dip,
2068 &last_ibp, 0, 0);
2069 if (error) {
2070 xfs_warn(mp,
2071 "%s: xfs_imap_to_bp returned error %d.",
2072 __func__, error);
2073 return error;
2074 }
2075
2076 last_offset = imap.im_boffset;
2077 next_agino = be32_to_cpu(last_dip->di_next_unlinked);
2078 ASSERT(next_agino != NULLAGINO);
2079 ASSERT(next_agino != 0);
2080 }
2081
2082
2083
2084
2085
2086 error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &dip, &ibp,
2087 0, 0);
2088 if (error) {
2089 xfs_warn(mp, "%s: xfs_imap_to_bp(2) returned error %d.",
2090 __func__, error);
2091 return error;
2092 }
2093 next_agino = be32_to_cpu(dip->di_next_unlinked);
2094 ASSERT(next_agino != 0);
2095 ASSERT(next_agino != agino);
2096 if (next_agino != NULLAGINO) {
2097 dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
2098 offset = ip->i_imap.im_boffset +
2099 offsetof(xfs_dinode_t, di_next_unlinked);
2100
2101
2102 xfs_dinode_calc_crc(mp, dip);
2103
2104 xfs_trans_inode_buf(tp, ibp);
2105 xfs_trans_log_buf(tp, ibp, offset,
2106 (offset + sizeof(xfs_agino_t) - 1));
2107 xfs_inobp_check(mp, ibp);
2108 } else {
2109 xfs_trans_brelse(tp, ibp);
2110 }
2111
2112
2113
2114 last_dip->di_next_unlinked = cpu_to_be32(next_agino);
2115 ASSERT(next_agino != 0);
2116 offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
2117
2118
2119 xfs_dinode_calc_crc(mp, last_dip);
2120
2121 xfs_trans_inode_buf(tp, last_ibp);
2122 xfs_trans_log_buf(tp, last_ibp, offset,
2123 (offset + sizeof(xfs_agino_t) - 1));
2124 xfs_inobp_check(mp, last_ibp);
2125 }
2126 return 0;
2127}
2128
2129
2130
2131
2132
2133
2134STATIC int
2135xfs_ifree_cluster(
2136 xfs_inode_t *free_ip,
2137 xfs_trans_t *tp,
2138 struct xfs_icluster *xic)
2139{
2140 xfs_mount_t *mp = free_ip->i_mount;
2141 int blks_per_cluster;
2142 int inodes_per_cluster;
2143 int nbufs;
2144 int i, j;
2145 int ioffset;
2146 xfs_daddr_t blkno;
2147 xfs_buf_t *bp;
2148 xfs_inode_t *ip;
2149 xfs_inode_log_item_t *iip;
2150 xfs_log_item_t *lip;
2151 struct xfs_perag *pag;
2152 xfs_ino_t inum;
2153
2154 inum = xic->first_ino;
2155 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
2156 blks_per_cluster = xfs_icluster_size_fsb(mp);
2157 inodes_per_cluster = blks_per_cluster << mp->m_sb.sb_inopblog;
2158 nbufs = mp->m_ialloc_blks / blks_per_cluster;
2159
2160 for (j = 0; j < nbufs; j++, inum += inodes_per_cluster) {
2161
2162
2163
2164
2165
2166 ioffset = inum - xic->first_ino;
2167 if ((xic->alloc & XFS_INOBT_MASK(ioffset)) == 0) {
2168 ASSERT(do_mod(ioffset, inodes_per_cluster) == 0);
2169 continue;
2170 }
2171
2172 blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
2173 XFS_INO_TO_AGBNO(mp, inum));
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
2184 mp->m_bsize * blks_per_cluster,
2185 XBF_UNMAPPED);
2186
2187 if (!bp)
2188 return -ENOMEM;
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199 bp->b_ops = &xfs_inode_buf_ops;
2200
2201
2202
2203
2204
2205
2206
2207
2208 lip = bp->b_fspriv;
2209 while (lip) {
2210 if (lip->li_type == XFS_LI_INODE) {
2211 iip = (xfs_inode_log_item_t *)lip;
2212 ASSERT(iip->ili_logged == 1);
2213 lip->li_cb = xfs_istale_done;
2214 xfs_trans_ail_copy_lsn(mp->m_ail,
2215 &iip->ili_flush_lsn,
2216 &iip->ili_item.li_lsn);
2217 xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
2218 }
2219 lip = lip->li_bio_list;
2220 }
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233 for (i = 0; i < inodes_per_cluster; i++) {
2234retry:
2235 rcu_read_lock();
2236 ip = radix_tree_lookup(&pag->pag_ici_root,
2237 XFS_INO_TO_AGINO(mp, (inum + i)));
2238
2239
2240 if (!ip) {
2241 rcu_read_unlock();
2242 continue;
2243 }
2244
2245
2246
2247
2248
2249
2250
2251
2252 spin_lock(&ip->i_flags_lock);
2253 if (ip->i_ino != inum + i ||
2254 __xfs_iflags_test(ip, XFS_ISTALE)) {
2255 spin_unlock(&ip->i_flags_lock);
2256 rcu_read_unlock();
2257 continue;
2258 }
2259 spin_unlock(&ip->i_flags_lock);
2260
2261
2262
2263
2264
2265
2266
2267
2268 if (ip != free_ip) {
2269 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2270 rcu_read_unlock();
2271 delay(1);
2272 goto retry;
2273 }
2274
2275
2276
2277
2278
2279
2280
2281
2282 if (ip->i_ino != inum + i) {
2283 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2284 rcu_read_unlock();
2285 continue;
2286 }
2287 }
2288 rcu_read_unlock();
2289
2290 xfs_iflock(ip);
2291 xfs_iflags_set(ip, XFS_ISTALE);
2292
2293
2294
2295
2296
2297 iip = ip->i_itemp;
2298 if (!iip || xfs_inode_clean(ip)) {
2299 ASSERT(ip != free_ip);
2300 xfs_ifunlock(ip);
2301 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2302 continue;
2303 }
2304
2305 iip->ili_last_fields = iip->ili_fields;
2306 iip->ili_fields = 0;
2307 iip->ili_fsync_fields = 0;
2308 iip->ili_logged = 1;
2309 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
2310 &iip->ili_item.li_lsn);
2311
2312 xfs_buf_attach_iodone(bp, xfs_istale_done,
2313 &iip->ili_item);
2314
2315 if (ip != free_ip)
2316 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2317 }
2318
2319 xfs_trans_stale_inode_buf(tp, bp);
2320 xfs_trans_binval(tp, bp);
2321 }
2322
2323 xfs_perag_put(pag);
2324 return 0;
2325}
2326
2327
2328
2329
2330
2331static inline void
2332xfs_ifree_local_data(
2333 struct xfs_inode *ip,
2334 int whichfork)
2335{
2336 struct xfs_ifork *ifp;
2337
2338 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
2339 return;
2340
2341 ifp = XFS_IFORK_PTR(ip, whichfork);
2342 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
2343}
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355int
2356xfs_ifree(
2357 xfs_trans_t *tp,
2358 xfs_inode_t *ip,
2359 struct xfs_defer_ops *dfops)
2360{
2361 int error;
2362 struct xfs_icluster xic = { 0 };
2363
2364 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
2365 ASSERT(VFS_I(ip)->i_nlink == 0);
2366 ASSERT(ip->i_d.di_nextents == 0);
2367 ASSERT(ip->i_d.di_anextents == 0);
2368 ASSERT(ip->i_d.di_size == 0 || !S_ISREG(VFS_I(ip)->i_mode));
2369 ASSERT(ip->i_d.di_nblocks == 0);
2370
2371
2372
2373
2374 error = xfs_iunlink_remove(tp, ip);
2375 if (error)
2376 return error;
2377
2378 error = xfs_difree(tp, ip->i_ino, dfops, &xic);
2379 if (error)
2380 return error;
2381
2382 xfs_ifree_local_data(ip, XFS_DATA_FORK);
2383 xfs_ifree_local_data(ip, XFS_ATTR_FORK);
2384
2385 VFS_I(ip)->i_mode = 0;
2386 ip->i_d.di_flags = 0;
2387 ip->i_d.di_dmevmask = 0;
2388 ip->i_d.di_forkoff = 0;
2389 ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
2390 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
2391
2392
2393
2394
2395 VFS_I(ip)->i_generation++;
2396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2397
2398 if (xic.deleted)
2399 error = xfs_ifree_cluster(ip, tp, &xic);
2400
2401 return error;
2402}
2403
2404
2405
2406
2407
2408
2409static void
2410xfs_iunpin(
2411 struct xfs_inode *ip)
2412{
2413 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
2414
2415 trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
2416
2417
2418 xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
2419
2420}
2421
2422static void
2423__xfs_iunpin_wait(
2424 struct xfs_inode *ip)
2425{
2426 wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
2427 DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
2428
2429 xfs_iunpin(ip);
2430
2431 do {
2432 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2433 if (xfs_ipincount(ip))
2434 io_schedule();
2435 } while (xfs_ipincount(ip));
2436 finish_wait(wq, &wait.wait);
2437}
2438
2439void
2440xfs_iunpin_wait(
2441 struct xfs_inode *ip)
2442{
2443 if (xfs_ipincount(ip))
2444 __xfs_iunpin_wait(ip);
2445}
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474int
2475xfs_remove(
2476 xfs_inode_t *dp,
2477 struct xfs_name *name,
2478 xfs_inode_t *ip)
2479{
2480 xfs_mount_t *mp = dp->i_mount;
2481 xfs_trans_t *tp = NULL;
2482 int is_dir = S_ISDIR(VFS_I(ip)->i_mode);
2483 int error = 0;
2484 struct xfs_defer_ops dfops;
2485 xfs_fsblock_t first_block;
2486 uint resblks;
2487
2488 trace_xfs_remove(dp, name);
2489
2490 if (XFS_FORCED_SHUTDOWN(mp))
2491 return -EIO;
2492
2493 error = xfs_qm_dqattach(dp, 0);
2494 if (error)
2495 goto std_return;
2496
2497 error = xfs_qm_dqattach(ip, 0);
2498 if (error)
2499 goto std_return;
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510 resblks = XFS_REMOVE_SPACE_RES(mp);
2511 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, resblks, 0, 0, &tp);
2512 if (error == -ENOSPC) {
2513 resblks = 0;
2514 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_remove, 0, 0, 0,
2515 &tp);
2516 }
2517 if (error) {
2518 ASSERT(error != -ENOSPC);
2519 goto std_return;
2520 }
2521
2522 xfs_ilock(dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2523 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
2524
2525 xfs_trans_ijoin(tp, dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2526 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2527
2528
2529
2530
2531 if (is_dir) {
2532 ASSERT(VFS_I(ip)->i_nlink >= 2);
2533 if (VFS_I(ip)->i_nlink != 2) {
2534 error = -ENOTEMPTY;
2535 goto out_trans_cancel;
2536 }
2537 if (!xfs_dir_isempty(ip)) {
2538 error = -ENOTEMPTY;
2539 goto out_trans_cancel;
2540 }
2541
2542
2543 error = xfs_droplink(tp, dp);
2544 if (error)
2545 goto out_trans_cancel;
2546
2547
2548 error = xfs_droplink(tp, ip);
2549 if (error)
2550 goto out_trans_cancel;
2551 } else {
2552
2553
2554
2555
2556
2557 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2558 }
2559 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2560
2561
2562 error = xfs_droplink(tp, ip);
2563 if (error)
2564 goto out_trans_cancel;
2565
2566 xfs_defer_init(&dfops, &first_block);
2567 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2568 &first_block, &dfops, resblks);
2569 if (error) {
2570 ASSERT(error != -ENOENT);
2571 goto out_bmap_cancel;
2572 }
2573
2574
2575
2576
2577
2578
2579 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2580 xfs_trans_set_sync(tp);
2581
2582 error = xfs_defer_finish(&tp, &dfops);
2583 if (error)
2584 goto out_bmap_cancel;
2585
2586 error = xfs_trans_commit(tp);
2587 if (error)
2588 goto std_return;
2589
2590 if (is_dir && xfs_inode_is_filestream(ip))
2591 xfs_filestream_deassociate(ip);
2592
2593 return 0;
2594
2595 out_bmap_cancel:
2596 xfs_defer_cancel(&dfops);
2597 out_trans_cancel:
2598 xfs_trans_cancel(tp);
2599 std_return:
2600 return error;
2601}
2602
2603
2604
2605
2606#define __XFS_SORT_INODES 5
2607STATIC void
2608xfs_sort_for_rename(
2609 struct xfs_inode *dp1,
2610 struct xfs_inode *dp2,
2611 struct xfs_inode *ip1,
2612 struct xfs_inode *ip2,
2613 struct xfs_inode *wip,
2614 struct xfs_inode **i_tab,
2615 int *num_inodes)
2616{
2617 int i, j;
2618
2619 ASSERT(*num_inodes == __XFS_SORT_INODES);
2620 memset(i_tab, 0, *num_inodes * sizeof(struct xfs_inode *));
2621
2622
2623
2624
2625
2626
2627
2628
2629 i = 0;
2630 i_tab[i++] = dp1;
2631 i_tab[i++] = dp2;
2632 i_tab[i++] = ip1;
2633 if (ip2)
2634 i_tab[i++] = ip2;
2635 if (wip)
2636 i_tab[i++] = wip;
2637 *num_inodes = i;
2638
2639
2640
2641
2642
2643 for (i = 0; i < *num_inodes; i++) {
2644 for (j = 1; j < *num_inodes; j++) {
2645 if (i_tab[j]->i_ino < i_tab[j-1]->i_ino) {
2646 struct xfs_inode *temp = i_tab[j];
2647 i_tab[j] = i_tab[j-1];
2648 i_tab[j-1] = temp;
2649 }
2650 }
2651 }
2652}
2653
2654static int
2655xfs_finish_rename(
2656 struct xfs_trans *tp,
2657 struct xfs_defer_ops *dfops)
2658{
2659 int error;
2660
2661
2662
2663
2664
2665 if (tp->t_mountp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
2666 xfs_trans_set_sync(tp);
2667
2668 error = xfs_defer_finish(&tp, dfops);
2669 if (error) {
2670 xfs_defer_cancel(dfops);
2671 xfs_trans_cancel(tp);
2672 return error;
2673 }
2674
2675 return xfs_trans_commit(tp);
2676}
2677
2678
2679
2680
2681
2682
2683STATIC int
2684xfs_cross_rename(
2685 struct xfs_trans *tp,
2686 struct xfs_inode *dp1,
2687 struct xfs_name *name1,
2688 struct xfs_inode *ip1,
2689 struct xfs_inode *dp2,
2690 struct xfs_name *name2,
2691 struct xfs_inode *ip2,
2692 struct xfs_defer_ops *dfops,
2693 xfs_fsblock_t *first_block,
2694 int spaceres)
2695{
2696 int error = 0;
2697 int ip1_flags = 0;
2698 int ip2_flags = 0;
2699 int dp2_flags = 0;
2700
2701
2702 error = xfs_dir_replace(tp, dp1, name1,
2703 ip2->i_ino,
2704 first_block, dfops, spaceres);
2705 if (error)
2706 goto out_trans_abort;
2707
2708
2709 error = xfs_dir_replace(tp, dp2, name2,
2710 ip1->i_ino,
2711 first_block, dfops, spaceres);
2712 if (error)
2713 goto out_trans_abort;
2714
2715
2716
2717
2718
2719
2720 if (dp1 != dp2) {
2721 dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2722
2723 if (S_ISDIR(VFS_I(ip2)->i_mode)) {
2724 error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
2725 dp1->i_ino, first_block,
2726 dfops, spaceres);
2727 if (error)
2728 goto out_trans_abort;
2729
2730
2731 if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
2732 error = xfs_droplink(tp, dp2);
2733 if (error)
2734 goto out_trans_abort;
2735 error = xfs_bumplink(tp, dp1);
2736 if (error)
2737 goto out_trans_abort;
2738 }
2739
2740
2741
2742
2743
2744
2745
2746 ip1_flags |= XFS_ICHGTIME_CHG;
2747 ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2748 }
2749
2750 if (S_ISDIR(VFS_I(ip1)->i_mode)) {
2751 error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
2752 dp2->i_ino, first_block,
2753 dfops, spaceres);
2754 if (error)
2755 goto out_trans_abort;
2756
2757
2758 if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
2759 error = xfs_droplink(tp, dp1);
2760 if (error)
2761 goto out_trans_abort;
2762 error = xfs_bumplink(tp, dp2);
2763 if (error)
2764 goto out_trans_abort;
2765 }
2766
2767
2768
2769
2770
2771
2772
2773 ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
2774 ip2_flags |= XFS_ICHGTIME_CHG;
2775 }
2776 }
2777
2778 if (ip1_flags) {
2779 xfs_trans_ichgtime(tp, ip1, ip1_flags);
2780 xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
2781 }
2782 if (ip2_flags) {
2783 xfs_trans_ichgtime(tp, ip2, ip2_flags);
2784 xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
2785 }
2786 if (dp2_flags) {
2787 xfs_trans_ichgtime(tp, dp2, dp2_flags);
2788 xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
2789 }
2790 xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2791 xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
2792 return xfs_finish_rename(tp, dfops);
2793
2794out_trans_abort:
2795 xfs_defer_cancel(dfops);
2796 xfs_trans_cancel(tp);
2797 return error;
2798}
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808static int
2809xfs_rename_alloc_whiteout(
2810 struct xfs_inode *dp,
2811 struct xfs_inode **wip)
2812{
2813 struct xfs_inode *tmpfile;
2814 int error;
2815
2816 error = xfs_create_tmpfile(dp, NULL, S_IFCHR | WHITEOUT_MODE, &tmpfile);
2817 if (error)
2818 return error;
2819
2820
2821
2822
2823
2824
2825
2826 drop_nlink(VFS_I(tmpfile));
2827 xfs_setup_iops(tmpfile);
2828 xfs_finish_inode_setup(tmpfile);
2829 VFS_I(tmpfile)->i_state |= I_LINKABLE;
2830
2831 *wip = tmpfile;
2832 return 0;
2833}
2834
2835
2836
2837
2838int
2839xfs_rename(
2840 struct xfs_inode *src_dp,
2841 struct xfs_name *src_name,
2842 struct xfs_inode *src_ip,
2843 struct xfs_inode *target_dp,
2844 struct xfs_name *target_name,
2845 struct xfs_inode *target_ip,
2846 unsigned int flags)
2847{
2848 struct xfs_mount *mp = src_dp->i_mount;
2849 struct xfs_trans *tp;
2850 struct xfs_defer_ops dfops;
2851 xfs_fsblock_t first_block;
2852 struct xfs_inode *wip = NULL;
2853 struct xfs_inode *inodes[__XFS_SORT_INODES];
2854 int num_inodes = __XFS_SORT_INODES;
2855 bool new_parent = (src_dp != target_dp);
2856 bool src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
2857 int spaceres;
2858 int error;
2859
2860 trace_xfs_rename(src_dp, target_dp, src_name, target_name);
2861
2862 if ((flags & RENAME_EXCHANGE) && !target_ip)
2863 return -EINVAL;
2864
2865
2866
2867
2868
2869
2870 if (flags & RENAME_WHITEOUT) {
2871 ASSERT(!(flags & (RENAME_NOREPLACE | RENAME_EXCHANGE)));
2872 error = xfs_rename_alloc_whiteout(target_dp, &wip);
2873 if (error)
2874 return error;
2875
2876
2877 src_name->type = XFS_DIR3_FT_CHRDEV;
2878 }
2879
2880 xfs_sort_for_rename(src_dp, target_dp, src_ip, target_ip, wip,
2881 inodes, &num_inodes);
2882
2883 spaceres = XFS_RENAME_SPACE_RES(mp, target_name->len);
2884 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, spaceres, 0, 0, &tp);
2885 if (error == -ENOSPC) {
2886 spaceres = 0;
2887 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_rename, 0, 0, 0,
2888 &tp);
2889 }
2890 if (error)
2891 goto out_release_wip;
2892
2893
2894
2895
2896 error = xfs_qm_vop_rename_dqattach(inodes);
2897 if (error)
2898 goto out_trans_cancel;
2899
2900
2901
2902
2903
2904
2905
2906 if (!new_parent)
2907 xfs_ilock(src_dp, XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2908 else
2909 xfs_lock_two_inodes(src_dp, target_dp,
2910 XFS_IOLOCK_EXCL | XFS_IOLOCK_PARENT);
2911
2912 xfs_lock_inodes(inodes, num_inodes, XFS_ILOCK_EXCL);
2913
2914
2915
2916
2917
2918
2919 xfs_trans_ijoin(tp, src_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2920 if (new_parent)
2921 xfs_trans_ijoin(tp, target_dp, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
2922 xfs_trans_ijoin(tp, src_ip, XFS_ILOCK_EXCL);
2923 if (target_ip)
2924 xfs_trans_ijoin(tp, target_ip, XFS_ILOCK_EXCL);
2925 if (wip)
2926 xfs_trans_ijoin(tp, wip, XFS_ILOCK_EXCL);
2927
2928
2929
2930
2931
2932
2933 if (unlikely((target_dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2934 (xfs_get_projid(target_dp) != xfs_get_projid(src_ip)))) {
2935 error = -EXDEV;
2936 goto out_trans_cancel;
2937 }
2938
2939 xfs_defer_init(&dfops, &first_block);
2940
2941
2942 if (flags & RENAME_EXCHANGE)
2943 return xfs_cross_rename(tp, src_dp, src_name, src_ip,
2944 target_dp, target_name, target_ip,
2945 &dfops, &first_block, spaceres);
2946
2947
2948
2949
2950 if (target_ip == NULL) {
2951
2952
2953
2954
2955 if (!spaceres) {
2956 error = xfs_dir_canenter(tp, target_dp, target_name);
2957 if (error)
2958 goto out_trans_cancel;
2959 }
2960
2961
2962
2963
2964
2965 error = xfs_dir_createname(tp, target_dp, target_name,
2966 src_ip->i_ino, &first_block,
2967 &dfops, spaceres);
2968 if (error)
2969 goto out_bmap_cancel;
2970
2971 xfs_trans_ichgtime(tp, target_dp,
2972 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2973
2974 if (new_parent && src_is_directory) {
2975 error = xfs_bumplink(tp, target_dp);
2976 if (error)
2977 goto out_bmap_cancel;
2978 }
2979 } else {
2980
2981
2982
2983
2984
2985 if (S_ISDIR(VFS_I(target_ip)->i_mode)) {
2986
2987
2988
2989 if (!(xfs_dir_isempty(target_ip)) ||
2990 (VFS_I(target_ip)->i_nlink > 2)) {
2991 error = -EEXIST;
2992 goto out_trans_cancel;
2993 }
2994 }
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005 error = xfs_dir_replace(tp, target_dp, target_name,
3006 src_ip->i_ino,
3007 &first_block, &dfops, spaceres);
3008 if (error)
3009 goto out_bmap_cancel;
3010
3011 xfs_trans_ichgtime(tp, target_dp,
3012 XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3013
3014
3015
3016
3017
3018 error = xfs_droplink(tp, target_ip);
3019 if (error)
3020 goto out_bmap_cancel;
3021
3022 if (src_is_directory) {
3023
3024
3025
3026 error = xfs_droplink(tp, target_ip);
3027 if (error)
3028 goto out_bmap_cancel;
3029 }
3030 }
3031
3032
3033
3034
3035 if (new_parent && src_is_directory) {
3036
3037
3038
3039
3040 error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
3041 target_dp->i_ino,
3042 &first_block, &dfops, spaceres);
3043 ASSERT(error != -EEXIST);
3044 if (error)
3045 goto out_bmap_cancel;
3046 }
3047
3048
3049
3050
3051
3052
3053
3054
3055 xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
3056 xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
3057
3058
3059
3060
3061
3062
3063 if (src_is_directory && (new_parent || target_ip != NULL)) {
3064
3065
3066
3067
3068
3069 error = xfs_droplink(tp, src_dp);
3070 if (error)
3071 goto out_bmap_cancel;
3072 }
3073
3074
3075
3076
3077
3078
3079 if (wip) {
3080 error = xfs_dir_replace(tp, src_dp, src_name, wip->i_ino,
3081 &first_block, &dfops, spaceres);
3082 } else
3083 error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
3084 &first_block, &dfops, spaceres);
3085 if (error)
3086 goto out_bmap_cancel;
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096 if (wip) {
3097 ASSERT(VFS_I(wip)->i_nlink == 0);
3098 error = xfs_bumplink(tp, wip);
3099 if (error)
3100 goto out_bmap_cancel;
3101 error = xfs_iunlink_remove(tp, wip);
3102 if (error)
3103 goto out_bmap_cancel;
3104 xfs_trans_log_inode(tp, wip, XFS_ILOG_CORE);
3105
3106
3107
3108
3109
3110
3111 VFS_I(wip)->i_state &= ~I_LINKABLE;
3112 }
3113
3114 xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3115 xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
3116 if (new_parent)
3117 xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
3118
3119 error = xfs_finish_rename(tp, &dfops);
3120 if (wip)
3121 IRELE(wip);
3122 return error;
3123
3124out_bmap_cancel:
3125 xfs_defer_cancel(&dfops);
3126out_trans_cancel:
3127 xfs_trans_cancel(tp);
3128out_release_wip:
3129 if (wip)
3130 IRELE(wip);
3131 return error;
3132}
3133
3134STATIC int
3135xfs_iflush_cluster(
3136 struct xfs_inode *ip,
3137 struct xfs_buf *bp)
3138{
3139 struct xfs_mount *mp = ip->i_mount;
3140 struct xfs_perag *pag;
3141 unsigned long first_index, mask;
3142 unsigned long inodes_per_cluster;
3143 int cilist_size;
3144 struct xfs_inode **cilist;
3145 struct xfs_inode *cip;
3146 int nr_found;
3147 int clcount = 0;
3148 int bufwasdelwri;
3149 int i;
3150
3151 pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
3152
3153 inodes_per_cluster = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
3154 cilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
3155 cilist = kmem_alloc(cilist_size, KM_MAYFAIL|KM_NOFS);
3156 if (!cilist)
3157 goto out_put;
3158
3159 mask = ~(((mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog)) - 1);
3160 first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
3161 rcu_read_lock();
3162
3163 nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)cilist,
3164 first_index, inodes_per_cluster);
3165 if (nr_found == 0)
3166 goto out_free;
3167
3168 for (i = 0; i < nr_found; i++) {
3169 cip = cilist[i];
3170 if (cip == ip)
3171 continue;
3172
3173
3174
3175
3176
3177
3178
3179 spin_lock(&cip->i_flags_lock);
3180 if (!cip->i_ino ||
3181 __xfs_iflags_test(cip, XFS_ISTALE)) {
3182 spin_unlock(&cip->i_flags_lock);
3183 continue;
3184 }
3185
3186
3187
3188
3189
3190
3191 if ((XFS_INO_TO_AGINO(mp, cip->i_ino) & mask) != first_index) {
3192 spin_unlock(&cip->i_flags_lock);
3193 break;
3194 }
3195 spin_unlock(&cip->i_flags_lock);
3196
3197
3198
3199
3200
3201
3202 if (xfs_inode_clean(cip) && xfs_ipincount(cip) == 0)
3203 continue;
3204
3205
3206
3207
3208
3209
3210 if (!xfs_ilock_nowait(cip, XFS_ILOCK_SHARED))
3211 continue;
3212 if (!xfs_iflock_nowait(cip)) {
3213 xfs_iunlock(cip, XFS_ILOCK_SHARED);
3214 continue;
3215 }
3216 if (xfs_ipincount(cip)) {
3217 xfs_ifunlock(cip);
3218 xfs_iunlock(cip, XFS_ILOCK_SHARED);
3219 continue;
3220 }
3221
3222
3223
3224
3225
3226
3227
3228
3229 if (!cip->i_ino) {
3230 xfs_ifunlock(cip);
3231 xfs_iunlock(cip, XFS_ILOCK_SHARED);
3232 continue;
3233 }
3234
3235
3236
3237
3238
3239 if (!xfs_inode_clean(cip)) {
3240 int error;
3241 error = xfs_iflush_int(cip, bp);
3242 if (error) {
3243 xfs_iunlock(cip, XFS_ILOCK_SHARED);
3244 goto cluster_corrupt_out;
3245 }
3246 clcount++;
3247 } else {
3248 xfs_ifunlock(cip);
3249 }
3250 xfs_iunlock(cip, XFS_ILOCK_SHARED);
3251 }
3252
3253 if (clcount) {
3254 XFS_STATS_INC(mp, xs_icluster_flushcnt);
3255 XFS_STATS_ADD(mp, xs_icluster_flushinode, clcount);
3256 }
3257
3258out_free:
3259 rcu_read_unlock();
3260 kmem_free(cilist);
3261out_put:
3262 xfs_perag_put(pag);
3263 return 0;
3264
3265
3266cluster_corrupt_out:
3267
3268
3269
3270
3271 rcu_read_unlock();
3272
3273
3274
3275
3276
3277 bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
3278 if (bufwasdelwri)
3279 xfs_buf_relse(bp);
3280
3281 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3282
3283 if (!bufwasdelwri) {
3284
3285
3286
3287
3288
3289 if (bp->b_iodone) {
3290 bp->b_flags &= ~XBF_DONE;
3291 xfs_buf_stale(bp);
3292 xfs_buf_ioerror(bp, -EIO);
3293 xfs_buf_ioend(bp);
3294 } else {
3295 xfs_buf_stale(bp);
3296 xfs_buf_relse(bp);
3297 }
3298 }
3299
3300
3301
3302
3303 xfs_iflush_abort(cip, false);
3304 kmem_free(cilist);
3305 xfs_perag_put(pag);
3306 return -EFSCORRUPTED;
3307}
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318int
3319xfs_iflush(
3320 struct xfs_inode *ip,
3321 struct xfs_buf **bpp)
3322{
3323 struct xfs_mount *mp = ip->i_mount;
3324 struct xfs_buf *bp = NULL;
3325 struct xfs_dinode *dip;
3326 int error;
3327
3328 XFS_STATS_INC(mp, xs_iflush_count);
3329
3330 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3331 ASSERT(xfs_isiflocked(ip));
3332 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3333 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
3334
3335 *bpp = NULL;
3336
3337 xfs_iunpin_wait(ip);
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347 if (xfs_iflags_test(ip, XFS_ISTALE)) {
3348 xfs_ifunlock(ip);
3349 return 0;
3350 }
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360 if (XFS_FORCED_SHUTDOWN(mp)) {
3361 error = -EIO;
3362 goto abort_out;
3363 }
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &bp, XBF_TRYLOCK,
3375 0);
3376 if (error == -EAGAIN) {
3377 xfs_ifunlock(ip);
3378 return error;
3379 }
3380 if (error)
3381 goto corrupt_out;
3382
3383
3384
3385
3386 error = xfs_iflush_int(ip, bp);
3387 if (error)
3388 goto corrupt_out;
3389
3390
3391
3392
3393
3394 if (xfs_buf_ispinned(bp))
3395 xfs_log_force(mp, 0);
3396
3397
3398
3399
3400
3401 error = xfs_iflush_cluster(ip, bp);
3402 if (error)
3403 goto cluster_corrupt_out;
3404
3405 *bpp = bp;
3406 return 0;
3407
3408corrupt_out:
3409 if (bp)
3410 xfs_buf_relse(bp);
3411 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
3412cluster_corrupt_out:
3413 error = -EFSCORRUPTED;
3414abort_out:
3415
3416
3417
3418 xfs_iflush_abort(ip, false);
3419 return error;
3420}
3421
3422STATIC int
3423xfs_iflush_int(
3424 struct xfs_inode *ip,
3425 struct xfs_buf *bp)
3426{
3427 struct xfs_inode_log_item *iip = ip->i_itemp;
3428 struct xfs_dinode *dip;
3429 struct xfs_mount *mp = ip->i_mount;
3430
3431 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
3432 ASSERT(xfs_isiflocked(ip));
3433 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
3434 ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
3435 ASSERT(iip != NULL && iip->ili_fields != 0);
3436 ASSERT(ip->i_d.di_version > 1);
3437
3438
3439 dip = xfs_buf_offset(bp, ip->i_imap.im_boffset);
3440
3441 if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
3442 mp, XFS_ERRTAG_IFLUSH_1)) {
3443 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3444 "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
3445 __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
3446 goto corrupt_out;
3447 }
3448 if (S_ISREG(VFS_I(ip)->i_mode)) {
3449 if (XFS_TEST_ERROR(
3450 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3451 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
3452 mp, XFS_ERRTAG_IFLUSH_3)) {
3453 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3454 "%s: Bad regular inode %Lu, ptr 0x%p",
3455 __func__, ip->i_ino, ip);
3456 goto corrupt_out;
3457 }
3458 } else if (S_ISDIR(VFS_I(ip)->i_mode)) {
3459 if (XFS_TEST_ERROR(
3460 (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
3461 (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
3462 (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
3463 mp, XFS_ERRTAG_IFLUSH_4)) {
3464 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3465 "%s: Bad directory inode %Lu, ptr 0x%p",
3466 __func__, ip->i_ino, ip);
3467 goto corrupt_out;
3468 }
3469 }
3470 if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
3471 ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5)) {
3472 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3473 "%s: detected corrupt incore inode %Lu, "
3474 "total extents = %d, nblocks = %Ld, ptr 0x%p",
3475 __func__, ip->i_ino,
3476 ip->i_d.di_nextents + ip->i_d.di_anextents,
3477 ip->i_d.di_nblocks, ip);
3478 goto corrupt_out;
3479 }
3480 if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
3481 mp, XFS_ERRTAG_IFLUSH_6)) {
3482 xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
3483 "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
3484 __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
3485 goto corrupt_out;
3486 }
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497 if (ip->i_d.di_version < 3)
3498 ip->i_d.di_flushiter++;
3499
3500
3501 if (S_ISDIR(VFS_I(ip)->i_mode) &&
3502 ip->i_d.di_format == XFS_DINODE_FMT_LOCAL &&
3503 xfs_dir2_sf_verify(ip))
3504 goto corrupt_out;
3505
3506
3507
3508
3509
3510
3511 xfs_inode_to_disk(ip, dip, iip->ili_item.li_lsn);
3512
3513
3514 if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
3515 ip->i_d.di_flushiter = 0;
3516
3517 xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK);
3518 if (XFS_IFORK_Q(ip))
3519 xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK);
3520 xfs_inobp_check(mp, bp);
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547 iip->ili_last_fields = iip->ili_fields;
3548 iip->ili_fields = 0;
3549 iip->ili_fsync_fields = 0;
3550 iip->ili_logged = 1;
3551
3552 xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
3553 &iip->ili_item.li_lsn);
3554
3555
3556
3557
3558
3559
3560
3561 xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
3562
3563
3564 xfs_dinode_calc_crc(mp, dip);
3565
3566 ASSERT(bp->b_fspriv != NULL);
3567 ASSERT(bp->b_iodone != NULL);
3568 return 0;
3569
3570corrupt_out:
3571 return -EFSCORRUPTED;
3572}
3573