-
-
Notifications
You must be signed in to change notification settings - Fork 338
/
Inode.cpp
938 lines (742 loc) · 22.5 KB
/
Inode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
/*
* Copyright 2011, Jérôme Duval, korli@users.berlios.de.
* Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
* This file may be used under the terms of the MIT License.
*/
#include "Inode.h"
#include <string.h>
#include <util/AutoLock.h>
#include <NodeMonitor.h>
#include "CachedBlock.h"
#include "DataStream.h"
#include "DirectoryIterator.h"
#include "ExtentStream.h"
#include "HTree.h"
#include "Utility.h"
#undef ASSERT
//#define TRACE_EXT2
#ifdef TRACE_EXT2
# define TRACE(x...) dprintf("\33[34mext2:\33[0m " x)
# define ASSERT(x) { if (!(x)) kernel_debugger("ext2: assert failed: " #x "\n"); }
#else
# define TRACE(x...) ;
# define ASSERT(x) ;
#endif
#define ERROR(x...) dprintf("\33[34mext2:\33[0m " x)
Inode::Inode(Volume* volume, ino_t id)
:
fVolume(volume),
fID(id),
fCache(NULL),
fMap(NULL),
fCached(false),
fHasExtraAttributes(false)
{
rw_lock_init(&fLock, "ext2 inode");
recursive_lock_init(&fSmallDataLock, "ext2 inode small data");
TRACE("Inode::Inode(): ext2_inode: %lu, disk inode: %" B_PRIu32
"\n", sizeof(ext2_inode), fVolume->InodeSize());
fNodeSize = sizeof(ext2_inode) > fVolume->InodeSize()
? fVolume->InodeSize() : sizeof(ext2_inode);
fInitStatus = UpdateNodeFromDisk();
if (fInitStatus == B_OK) {
fHasExtraAttributes = (fNodeSize == sizeof(ext2_inode)
&& fNode.ExtraInodeSize() + EXT2_INODE_NORMAL_SIZE
== sizeof(ext2_inode));
if (IsDirectory() || (IsSymLink() && Size() < 60)) {
TRACE("Inode::Inode(): Not creating the file cache\n");
fCached = false;
fInitStatus = B_OK;
} else
fInitStatus = EnableFileCache();
} else
TRACE("Inode: Failed initialization\n");
}
Inode::Inode(Volume* volume)
:
fVolume(volume),
fID(0),
fCache(NULL),
fMap(NULL),
fCached(false),
fInitStatus(B_NO_INIT)
{
rw_lock_init(&fLock, "ext2 inode");
recursive_lock_init(&fSmallDataLock, "ext2 inode small data");
TRACE("Inode::Inode(): ext2_inode: %lu, disk inode: %" B_PRIu32 "\n",
sizeof(ext2_inode), fVolume->InodeSize());
fNodeSize = sizeof(ext2_inode) > fVolume->InodeSize()
? fVolume->InodeSize() : sizeof(ext2_inode);
}
Inode::~Inode()
{
TRACE("Inode destructor\n");
if (fCached) {
TRACE("Deleting the file cache and file map\n");
file_cache_delete(FileCache());
file_map_delete(Map());
}
TRACE("Inode destructor: Done\n");
}
status_t
Inode::InitCheck()
{
return fInitStatus;
}
void
Inode::WriteLockInTransaction(Transaction& transaction)
{
acquire_vnode(fVolume->FSVolume(), ID());
TRACE("Inode::WriteLockInTransaction(): Locking\n");
rw_lock_write_lock(&fLock);
transaction.AddListener(this);
}
status_t
Inode::WriteBack(Transaction& transaction)
{
off_t blockNum;
status_t status = fVolume->GetInodeBlock(fID, blockNum);
if (status != B_OK)
return status;
if (Node().Size() > 0x7fffffffLL) {
status = fVolume->ActivateLargeFiles(transaction);
if (status != B_OK)
return status;
}
CachedBlock cached(fVolume);
uint8* inodeBlockData = cached.SetToWritable(transaction, blockNum);
if (inodeBlockData == NULL)
return B_IO_ERROR;
TRACE("Inode::WriteBack(): Inode ID: %" B_PRIdINO ", inode block: %"
B_PRIdOFF ", data: %p, index: %" B_PRIu32 ", inode size: %" B_PRIu32
", node size: %" B_PRIu32 ", this: %p, node: %p\n",
fID, blockNum, inodeBlockData, fVolume->InodeBlockIndex(fID),
fVolume->InodeSize(), fNodeSize, this, &fNode);
memcpy(inodeBlockData +
fVolume->InodeBlockIndex(fID) * fVolume->InodeSize(),
(uint8*)&fNode, fNodeSize);
TRACE("Inode::WriteBack() finished %" B_PRId32 "\n", Node().stream.direct[0]);
return B_OK;
}
status_t
Inode::UpdateNodeFromDisk()
{
off_t blockNum;
status_t status = fVolume->GetInodeBlock(fID, blockNum);
if (status != B_OK)
return status;
TRACE("inode %" B_PRIdINO " at block %" B_PRIdOFF "\n", fID, blockNum);
CachedBlock cached(fVolume);
const uint8* inodeBlock = cached.SetTo(blockNum);
if (inodeBlock == NULL)
return B_IO_ERROR;
TRACE("Inode size: %" B_PRIu32 ", inode index: %" B_PRIu32 "\n",
fVolume->InodeSize(), fVolume->InodeBlockIndex(fID));
ext2_inode* inode = (ext2_inode*)(inodeBlock
+ fVolume->InodeBlockIndex(fID) * fVolume->InodeSize());
TRACE("Attempting to copy inode data from %p to %p, ext2_inode "
"size: %" B_PRIu32 "\n", inode, &fNode, fNodeSize);
memcpy(&fNode, inode, fNodeSize);
uint32 numLinks = fNode.NumLinks();
fUnlinked = numLinks == 0 || (IsDirectory() && numLinks == 1);
return B_OK;
}
status_t
Inode::CheckPermissions(int accessMode) const
{
// you never have write access to a read-only volume
if ((accessMode & W_OK) != 0 && fVolume->IsReadOnly())
return B_READ_ONLY_DEVICE;
// get node permissions
mode_t mode = Mode();
int userPermissions = (mode & S_IRWXU) >> 6;
int groupPermissions = (mode & S_IRWXG) >> 3;
int otherPermissions = mode & S_IRWXO;
// get the node permissions for this uid/gid
int permissions = 0;
uid_t uid = geteuid();
gid_t gid = getegid();
if (uid == 0) {
// user is root
// root has always read/write permission, but at least one of the
// X bits must be set for execute permission
permissions = userPermissions | groupPermissions | otherPermissions
| R_OK | W_OK;
} else if (uid == (uid_t)fNode.UserID()) {
// user is node owner
permissions = userPermissions;
} else if (gid == (gid_t)fNode.GroupID()) {
// user is in owning group
permissions = groupPermissions;
} else {
// user is one of the others
permissions = otherPermissions;
}
return (accessMode & ~permissions) == 0 ? B_OK : B_NOT_ALLOWED;
}
status_t
Inode::FindBlock(off_t offset, fsblock_t& block, uint32 *_count)
{
if (Flags() & EXT2_INODE_EXTENTS) {
ExtentStream stream(fVolume, &fNode.extent_stream, Size());
return stream.FindBlock(offset, block, _count);
}
DataStream stream(fVolume, &fNode.stream, Size());
return stream.FindBlock(offset, block, _count);
}
status_t
Inode::ReadAt(off_t pos, uint8* buffer, size_t* _length)
{
size_t length = *_length;
// set/check boundaries for pos/length
if (pos < 0) {
ERROR("inode %" B_PRIdINO ": ReadAt failed(pos %" B_PRIdOFF
", length %" B_PRIuSIZE ")\n", ID(), pos, length);
return B_BAD_VALUE;
}
if (pos >= Size() || length == 0) {
TRACE("inode %" B_PRIdINO ": ReadAt 0 (pos %" B_PRIdOFF ", length %"
B_PRIuSIZE ")\n", ID(), pos, length);
*_length = 0;
return B_NO_ERROR;
}
return file_cache_read(FileCache(), NULL, pos, buffer, _length);
}
status_t
Inode::WriteAt(Transaction& transaction, off_t pos, const uint8* buffer,
size_t* _length)
{
TRACE("Inode::WriteAt(%" B_PRIdOFF ", %p, *(%p) = %" B_PRIuSIZE ")\n", pos,
buffer, _length, *_length);
ReadLocker readLocker(fLock);
if (IsFileCacheDisabled())
return B_BAD_VALUE;
if (pos < 0)
return B_BAD_VALUE;
readLocker.Unlock();
TRACE("Inode::WriteAt(): Starting transaction\n");
transaction.Start(fVolume->GetJournal());
WriteLocker writeLocker(fLock);
TRACE("Inode::WriteAt(): Updating modification time\n");
struct timespec timespec;
_BigtimeToTimespec(real_time_clock_usecs(), ×pec);
SetModificationTime(×pec);
// NOTE: Debugging info to find why sometimes resize doesn't happen
size_t length = *_length;
#ifdef TRACE_EXT2
off_t oldEnd = pos + length;
TRACE("Inode::WriteAt(): Old calc for end? %x:%x\n",
(int)(oldEnd >> 32), (int)(oldEnd & 0xFFFFFFFF));
#endif
off_t end = pos + (off_t)length;
off_t oldSize = Size();
TRACE("Inode::WriteAt(): Old size: %" B_PRIdOFF ":%" B_PRIdOFF
", new size: %" B_PRIdOFF ":%" B_PRIdOFF "\n",
oldSize >> 32, oldSize & 0xFFFFFFFF,
end >> 32, end & 0xFFFFFFFF);
if (end > oldSize) {
status_t status = Resize(transaction, end);
if (status != B_OK) {
*_length = 0;
WriteLockInTransaction(transaction);
return status;
}
status = WriteBack(transaction);
if (status != B_OK) {
*_length = 0;
WriteLockInTransaction(transaction);
return status;
}
}
writeLocker.Unlock();
if (oldSize < pos)
FillGapWithZeros(oldSize, pos);
if (length == 0) {
// Probably just changed the file size with the pos parameter
return B_OK;
}
TRACE("Inode::WriteAt(): Performing write: %p, %" B_PRIdOFF ", %p, %"
B_PRIuSIZE "\n", FileCache(), pos, buffer, *_length);
status_t status = file_cache_write(FileCache(), NULL, pos, buffer,
_length);
WriteLockInTransaction(transaction);
TRACE("Inode::WriteAt(): Done\n");
return status;
}
status_t
Inode::FillGapWithZeros(off_t start, off_t end)
{
TRACE("Inode::FileGapWithZeros(%" B_PRIdOFF " - %" B_PRIdOFF ")\n", start,
end);
while (start < end) {
size_t size;
if (end > start + 1024 * 1024 * 1024)
size = 1024 * 1024 * 1024;
else
size = end - start;
TRACE("Inode::FillGapWithZeros(): Calling file_cache_write(%p, NULL, "
"%" B_PRIdOFF ", NULL, &(%" B_PRIuSIZE ") = %p)\n", fCache, start,
size, &size);
status_t status = file_cache_write(fCache, NULL, start, NULL,
&size);
if (status != B_OK)
return status;
start += size;
}
return B_OK;
}
status_t
Inode::Resize(Transaction& transaction, off_t size)
{
TRACE("Inode::Resize() ID:%" B_PRIdINO " size: %" B_PRIdOFF "\n", ID(),
size);
if (size < 0)
return B_BAD_VALUE;
off_t oldSize = Size();
if (size == oldSize)
return B_OK;
TRACE("Inode::Resize(): old size: %" B_PRIdOFF ", new size: %" B_PRIdOFF
"\n", oldSize, size);
status_t status;
if (size > oldSize) {
status = _EnlargeDataStream(transaction, size);
if (status != B_OK) {
// Restore original size
_ShrinkDataStream(transaction, oldSize);
}
} else
status = _ShrinkDataStream(transaction, size);
TRACE("Inode::Resize(): Updating file map and cache\n");
if (status != B_OK)
return status;
file_cache_set_size(FileCache(), size);
file_map_set_size(Map(), size);
TRACE("Inode::Resize(): Writing back inode changes. Size: %" B_PRIdOFF
"\n", Size());
return WriteBack(transaction);
}
status_t
Inode::InitDirectory(Transaction& transaction, Inode* parent)
{
TRACE("Inode::InitDirectory()\n");
uint32 blockSize = fVolume->BlockSize();
status_t status = Resize(transaction, blockSize);
if (status != B_OK)
return status;
fsblock_t blockNum;
if (Flags() & EXT2_INODE_EXTENTS) {
ExtentStream stream(fVolume, &fNode.extent_stream, Size());
status = stream.FindBlock(0, blockNum);
} else {
DataStream stream(fVolume, &fNode.stream, Size());
status = stream.FindBlock(0, blockNum);
}
if (status != B_OK)
return status;
CachedBlock cached(fVolume);
uint8* block = cached.SetToWritable(transaction, blockNum, true);
HTreeRoot* root = (HTreeRoot*)block;
root->dot.inode_id = fID;
root->dot.entry_length = 12;
root->dot.name_length = 1;
root->dot.file_type = EXT2_TYPE_DIRECTORY;
root->dot_entry_name[0] = '.';
root->dotdot.inode_id = parent == NULL ? fID : parent->ID();
root->dotdot.entry_length = blockSize - 12;
root->dotdot.name_length = 2;
root->dotdot.file_type = EXT2_TYPE_DIRECTORY;
root->dotdot_entry_name[0] = '.';
root->dotdot_entry_name[1] = '.';
parent->IncrementNumLinks(transaction);
return parent->WriteBack(transaction);
}
status_t
Inode::Unlink(Transaction& transaction)
{
uint32 numLinks = fNode.NumLinks();
TRACE("Inode::Unlink(): Current links: %" B_PRIu32 "\n", numLinks);
if (numLinks == 0)
return B_BAD_VALUE;
if ((IsDirectory() && numLinks == 2) || (numLinks == 1)) {
fUnlinked = true;
TRACE("Inode::Unlink(): Putting inode in orphan list\n");
ino_t firstOrphanID;
status_t status = fVolume->SaveOrphan(transaction, fID, firstOrphanID);
if (status != B_OK)
return status;
if (firstOrphanID != 0) {
Vnode firstOrphan(fVolume, firstOrphanID);
Inode* nextOrphan;
status = firstOrphan.Get(&nextOrphan);
if (status != B_OK)
return status;
fNode.SetNextOrphan(nextOrphan->ID());
} else {
// Next orphan link is stored in deletion time
fNode.deletion_time = 0;
}
fNode.num_links = 0;
status = remove_vnode(fVolume->FSVolume(), fID);
if (status != B_OK)
return status;
} else
fNode.SetNumLinks(--numLinks);
return WriteBack(transaction);
}
/*static*/ status_t
Inode::Create(Transaction& transaction, Inode* parent, const char* name,
int32 mode, int openMode, uint8 type, bool* _created, ino_t* _id,
Inode** _inode, fs_vnode_ops* vnodeOps, uint32 publishFlags)
{
TRACE("Inode::Create()\n");
Volume* volume = transaction.GetVolume();
DirectoryIterator* entries = NULL;
ObjectDeleter<DirectoryIterator> entriesDeleter;
if (parent != NULL) {
parent->WriteLockInTransaction(transaction);
TRACE("Inode::Create(): Looking up entry destination\n");
HTree htree(volume, parent);
status_t status = htree.Lookup(name, &entries);
if (status == B_ENTRY_NOT_FOUND) {
panic("We need to add the first node.\n");
return B_ERROR;
}
if (status != B_OK)
return status;
entriesDeleter.SetTo(entries);
TRACE("Inode::Create(): Looking up to see if file already exists\n");
ino_t entryID;
status = entries->FindEntry(name, &entryID);
if (status == B_OK) {
// File already exists
TRACE("Inode::Create(): File already exists\n");
if (S_ISDIR(mode) || S_ISLNK(mode) || (openMode & O_EXCL) != 0)
return B_FILE_EXISTS;
Vnode vnode(volume, entryID);
Inode* inode;
status = vnode.Get(&inode);
if (status != B_OK) {
TRACE("Inode::Create() Failed to get the inode from the "
"vnode\n");
return B_ENTRY_NOT_FOUND;
}
if (inode->IsDirectory() && (openMode & O_RWMASK) != O_RDONLY)
return B_IS_A_DIRECTORY;
if ((openMode & O_DIRECTORY) != 0 && !inode->IsDirectory())
return B_NOT_A_DIRECTORY;
if (inode->CheckPermissions(open_mode_to_access(openMode)
| ((openMode & O_TRUNC) != 0 ? W_OK : 0)) != B_OK)
return B_NOT_ALLOWED;
if ((openMode & O_TRUNC) != 0) {
// Truncate requested
TRACE("Inode::Create(): Truncating file\n");
inode->WriteLockInTransaction(transaction);
status = inode->Resize(transaction, 0);
if (status != B_OK)
return status;
}
if (_created != NULL)
*_created = false;
if (_id != NULL)
*_id = inode->ID();
if (_inode != NULL)
*_inode = inode;
if (_id != NULL || _inode != NULL)
vnode.Keep();
TRACE("Inode::Create(): Done opening file\n");
return B_OK;
/*} else if ((mode & S_ATTR_DIR) == 0) {
TRACE("Inode::Create(): (mode & S_ATTR_DIR) == 0\n");
return B_BAD_VALUE;*/
} else if ((openMode & O_DIRECTORY) != 0) {
TRACE("Inode::Create(): (openMode & O_DIRECTORY) != 0\n");
return B_ENTRY_NOT_FOUND;
}
// Return to initial position
TRACE("Inode::Create(): Restarting iterator\n");
entries->Restart();
}
status_t status;
if (parent != NULL) {
status = parent->CheckPermissions(W_OK);
if (status != B_OK)
return status;
}
TRACE("Inode::Create(): Allocating inode\n");
ino_t id;
status = volume->AllocateInode(transaction, parent, mode, id);
if (status != B_OK) {
ERROR("Inode::Create(): AllocateInode() failed\n");
return status;
}
if (entries != NULL) {
size_t nameLength = strlen(name);
status = entries->AddEntry(transaction, name, nameLength, id, type);
if (status != B_OK) {
ERROR("Inode::Create(): AddEntry() failed\n");
return status;
}
}
TRACE("Inode::Create(): Creating inode\n");
Inode* inode = new(std::nothrow) Inode(volume);
if (inode == NULL)
return B_NO_MEMORY;
TRACE("Inode::Create(): Getting node structure\n");
ext2_inode& node = inode->Node();
TRACE("Inode::Create(): Initializing inode data\n");
memset(&node, 0, sizeof(ext2_inode));
node.SetMode(mode);
node.SetUserID(geteuid());
node.SetGroupID(parent != NULL ? parent->Node().GroupID() : getegid());
node.SetNumLinks(inode->IsDirectory() ? 2 : 1);
TRACE("Inode::Create(): Updating time\n");
struct timespec timespec;
_BigtimeToTimespec(real_time_clock_usecs(), ×pec);
inode->SetAccessTime(×pec);
inode->SetCreationTime(×pec);
inode->SetModificationTime(×pec);
if (parent != NULL)
node.SetFlags(parent->Flags() & EXT2_INODE_INHERITED);
if (volume->HasExtentsFeature()
&& (inode->IsDirectory() || inode->IsFile())) {
node.SetFlag(EXT2_INODE_EXTENTS);
ExtentStream stream(volume, &node.extent_stream, 0);
stream.Init();
ASSERT(stream.Check());
}
if (sizeof(ext2_inode) < volume->InodeSize())
node.SetExtraInodeSize(sizeof(ext2_inode) - EXT2_INODE_NORMAL_SIZE);
TRACE("Inode::Create(): Updating ID\n");
inode->fID = id;
if (inode->IsDirectory()) {
TRACE("Inode::Create(): Initializing directory\n");
status = inode->InitDirectory(transaction, parent);
if (status != B_OK) {
ERROR("Inode::Create(): InitDirectory() failed\n");
delete inode;
return status;
}
}
// TODO: Maybe it can be better
/*if (volume->HasExtendedAttributes()) {
TRACE("Inode::Create(): Initializing extended attributes\n");
uint32 blockGroup = 0;
uint32 pos = 0;
uint32 allocated;
status = volume->AllocateBlocks(transaction, 1, 1, blockGroup, pos,
allocated);
if (status != B_OK)
return status;
// Clear the new block
uint32 blockNum = volume->FirstDataBlock() + pos +
volume->BlocksPerGroup() * blockGroup;
CachedBlock cached(volume);
cached.SetToWritable(transaction, blockNum, true);
node.SetExtendedAttributesBlock(blockNum);
}*/
TRACE("Inode::Create(): Saving inode\n");
status = inode->WriteBack(transaction);
if (status != B_OK) {
delete inode;
return status;
}
TRACE("Inode::Create(): Creating vnode\n");
Vnode vnode;
status = vnode.Publish(transaction, inode, vnodeOps, publishFlags);
if (status != B_OK)
return status;
if (!inode->IsSymLink()) {
// Vnode::Publish doesn't publish symlinks
if (!inode->IsDirectory()) {
status = inode->EnableFileCache();
if (status != B_OK)
return status;
}
inode->WriteLockInTransaction(transaction);
}
if (_created)
*_created = true;
if (_id != NULL)
*_id = id;
if (_inode != NULL)
*_inode = inode;
if (_id != NULL || _inode != NULL)
vnode.Keep();
TRACE("Inode::Create(): Deleting entries iterator\n");
DirectoryIterator* iterator = entriesDeleter.Detach();
TRACE("Inode::Create(): Entries iterator: %p\n", entries);
delete iterator;
TRACE("Inode::Create(): Done\n");
return B_OK;
}
status_t
Inode::EnableFileCache()
{
TRACE("Inode::EnableFileCache()\n");
if (fCached)
return B_OK;
if (fCache != NULL) {
fCached = true;
return B_OK;
}
TRACE("Inode::EnableFileCache(): Creating file cache: %" B_PRIu32 ", %"
B_PRIdINO ", %" B_PRIdOFF "\n", fVolume->ID(), ID(), Size());
fCache = file_cache_create(fVolume->ID(), ID(), Size());
fMap = file_map_create(fVolume->ID(), ID(), Size());
if (fCache == NULL) {
ERROR("Inode::EnableFileCache(): Failed to create file cache\n");
fCached = false;
return B_ERROR;
}
fCached = true;
TRACE("Inode::EnableFileCache(): Done\n");
return B_OK;
}
status_t
Inode::DisableFileCache()
{
TRACE("Inode::DisableFileCache()\n");
if (!fCached)
return B_OK;
file_cache_delete(FileCache());
file_map_delete(Map());
fCached = false;
return B_OK;
}
status_t
Inode::Sync()
{
if (!IsFileCacheDisabled())
return file_cache_sync(fCache);
return B_OK;
}
void
Inode::TransactionDone(bool success)
{
if (!success) {
// Revert any changes to the inode
if (fInitStatus == B_OK && UpdateNodeFromDisk() != B_OK)
panic("Failed to reload inode from disk!\n");
else if (fInitStatus == B_NO_INIT) {
// TODO: Unpublish vnode?
panic("Failed to finish creating inode\n");
}
} else {
if (fInitStatus == B_NO_INIT) {
TRACE("Inode::TransactionDone(): Inode creation succeeded\n");
fInitStatus = B_OK;
}
}
}
void
Inode::RemovedFromTransaction()
{
TRACE("Inode::RemovedFromTransaction(): Unlocking\n");
rw_lock_write_unlock(&fLock);
put_vnode(fVolume->FSVolume(), ID());
}
status_t
Inode::_EnlargeDataStream(Transaction& transaction, off_t size)
{
if (size < 0)
return B_BAD_VALUE;
TRACE("Inode::_EnlargeDataStream()\n");
uint32 blockSize = fVolume->BlockSize();
off_t oldSize = Size();
off_t maxSize = oldSize;
if (maxSize % blockSize != 0)
maxSize += blockSize - maxSize % blockSize;
if (size <= maxSize) {
// No need to allocate more blocks
TRACE("Inode::_EnlargeDataStream(): No need to allocate more blocks\n");
TRACE("Inode::_EnlargeDataStream(): Setting size to %" B_PRIdOFF "\n",
size);
fNode.SetSize(size);
return B_OK;
}
off_t end = size == 0 ? 0 : (size - 1) / fVolume->BlockSize() + 1;
if (Flags() & EXT2_INODE_EXTENTS) {
ExtentStream stream(fVolume, &fNode.extent_stream, Size());
stream.Enlarge(transaction, end);
ASSERT(stream.Check());
} else {
DataStream stream(fVolume, &fNode.stream, oldSize);
stream.Enlarge(transaction, end);
}
TRACE("Inode::_EnlargeDataStream(): Setting size to %" B_PRIdOFF "\n",
size);
fNode.SetSize(size);
TRACE("Inode::_EnlargeDataStream(): Setting allocated block count to %"
B_PRIdOFF "\n", end);
return _SetNumBlocks(_NumBlocks() + end * (fVolume->BlockSize() / 512));
}
status_t
Inode::_ShrinkDataStream(Transaction& transaction, off_t size)
{
TRACE("Inode::_ShrinkDataStream()\n");
if (size < 0)
return B_BAD_VALUE;
uint32 blockSize = fVolume->BlockSize();
off_t oldSize = Size();
off_t lastByte = oldSize == 0 ? 0 : oldSize - 1;
off_t minSize = (lastByte / blockSize + 1) * blockSize;
// Minimum size that doesn't require freeing blocks
if (size > minSize) {
// No need to allocate more blocks
TRACE("Inode::_ShrinkDataStream(): No need to allocate more blocks\n");
TRACE("Inode::_ShrinkDataStream(): Setting size to %" B_PRIdOFF "\n",
size);
fNode.SetSize(size);
return B_OK;
}
off_t end = size == 0 ? 0 : (size - 1) / fVolume->BlockSize() + 1;
if (Flags() & EXT2_INODE_EXTENTS) {
ExtentStream stream(fVolume, &fNode.extent_stream, Size());
stream.Shrink(transaction, end);
ASSERT(stream.Check());
} else {
DataStream stream(fVolume, &fNode.stream, oldSize);
stream.Shrink(transaction, end);
}
fNode.SetSize(size);
return _SetNumBlocks(_NumBlocks() - end * (fVolume->BlockSize() / 512));
}
uint64
Inode::_NumBlocks()
{
if (fVolume->HugeFiles()) {
if (fNode.Flags() & EXT2_INODE_HUGE_FILE)
return fNode.NumBlocks64() * (fVolume->BlockSize() / 512);
else
return fNode.NumBlocks64();
} else
return fNode.NumBlocks();
}
status_t
Inode::_SetNumBlocks(uint64 numBlocks)
{
if (numBlocks <= 0xffffffff) {
fNode.SetNumBlocks(numBlocks);
fNode.ClearFlag(EXT2_INODE_HUGE_FILE);
return B_OK;
}
if (!fVolume->HugeFiles())
return E2BIG;
if (numBlocks > 0xffffffffffffULL) {
fNode.SetFlag(EXT2_INODE_HUGE_FILE);
numBlocks /= (fVolume->BlockSize() / 512);
} else
fNode.ClearFlag(EXT2_INODE_HUGE_FILE);
fNode.SetNumBlocks64(numBlocks);
return B_OK;
}
void
Inode::IncrementNumLinks(Transaction& transaction)
{
fNode.SetNumLinks(fNode.NumLinks() + 1);
if (IsIndexed() && (fNode.NumLinks() >= EXT2_INODE_MAX_LINKS
|| fNode.NumLinks() == 2)) {
fNode.SetNumLinks(1);
fVolume->ActivateDirNLink(transaction);
}
}