-
Notifications
You must be signed in to change notification settings - Fork 82
/
ufs_lookup.c
1361 lines (1269 loc) · 40.1 KB
/
ufs_lookup.c
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
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: ufs_lookup.c,v 1.147 2017/10/27 09:59:17 utkarsh009 Exp $ */
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)ufs_lookup.c 8.9 (Berkeley) 8/11/94
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ufs_lookup.c,v 1.147 2017/10/27 09:59:17 utkarsh009 Exp $");
#ifdef _KERNEL_OPT
#include "opt_ffs.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/namei.h>
#include <sys/buf.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <sys/kernel.h>
#include <sys/kauth.h>
#include <sys/wapbl.h>
#include <sys/proc.h>
#include <sys/kmem.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/dir.h>
#ifdef UFS_DIRHASH
#include <ufs/ufs/dirhash.h>
#endif
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/ufs_extern.h>
#include <ufs/ufs/ufs_bswap.h>
#include <ufs/ufs/ufs_wapbl.h>
#include <miscfs/genfs/genfs.h>
#ifdef DIAGNOSTIC
int dirchk = 1;
#else
int dirchk = 0;
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
# define ENDIANSWAP(needswap) ((needswap) == 0)
#else
# define ENDIANSWAP(needswap) ((needswap) != 0)
#endif
#define NAMLEN(fsfmt, needswap, dp) \
((fsfmt) && ENDIANSWAP(needswap) ? (dp)->d_type : (dp)->d_namlen)
static void
ufs_dirswap(struct direct *dirp)
{
uint8_t tmp = dirp->d_namlen;
dirp->d_namlen = dirp->d_type;
dirp->d_type = tmp;
}
struct slotinfo {
enum {
NONE, /* need to search a slot for our new entry */
COMPACT, /* a compaction can make a slot in the current
DIRBLKSIZ block */
FOUND, /* found a slot (or no need to search) */
} status;
doff_t offset; /* offset of area with free space.
a special value -1 for invalid */
int size; /* size of area at slotoffset */
int freespace; /* accumulated amount of space free in
the current DIRBLKSIZ block */
int needed; /* size of the entry we're seeking */
};
static void
calc_count(struct ufs_lookup_results *results, int dirblksiz, doff_t prevoff)
{
if ((results->ulr_offset & (dirblksiz - 1)) == 0)
results->ulr_count = 0;
else
results->ulr_count = results->ulr_offset - prevoff;
}
static void
slot_init(struct slotinfo *slot)
{
slot->status = FOUND;
slot->offset = -1;
slot->freespace = slot->size = slot->needed = 0;
}
#ifdef UFS_DIRHASH
static doff_t
slot_findfree(struct slotinfo *slot, struct inode *dp)
{
if (slot->status == FOUND)
return dp->i_size;
slot->offset = ufsdirhash_findfree(dp, slot->needed, &slot->size);
if (slot->offset < 0)
return dp->i_size;
slot->status = COMPACT;
doff_t enduseful = ufsdirhash_enduseful(dp);
if (enduseful < 0)
return dp->i_size;
return enduseful;
}
#endif
static void
slot_white(struct slotinfo *slot, uint16_t reclen,
struct ufs_lookup_results *results)
{
slot->status = FOUND;
slot->offset = results->ulr_offset;
slot->size = reclen;
results->ulr_reclen = slot->size;
}
static void
slot_update(struct slotinfo *slot, int size, uint16_t reclen, doff_t offset)
{
if (size >= slot->needed) {
slot->status = FOUND;
slot->offset = offset;
slot->size = reclen;
} else if (slot->status == NONE) {
slot->freespace += size;
if (slot->offset == -1)
slot->offset = offset;
if (slot->freespace >= slot->needed) {
slot->status = COMPACT;
slot->size = offset + reclen - slot->offset;
}
}
}
/*
* Return an indication of where the new directory entry should be put.
* If we didn't find a slot, then set results->ulr_count to 0 indicating
* that the new slot belongs at the end of the directory. If we found a slot,
* then the new entry can be put in the range from results->ulr_offset to
* results->ulr_offset + results->ulr_count.
*/
static int
slot_estimate(const struct slotinfo *slot, int dirblksiz, int nameiop,
doff_t prevoff, doff_t enduseful, const struct inode *ip,
struct ufs_lookup_results *results)
{
if (slot->status == NONE) {
results->ulr_offset = roundup(ip->i_size, dirblksiz);
results->ulr_count = 0;
enduseful = results->ulr_offset;
} else if (nameiop == DELETE) {
results->ulr_offset = slot->offset;
calc_count(results, dirblksiz, prevoff);
} else {
results->ulr_offset = slot->offset;
results->ulr_count = slot->size;
if (enduseful < slot->offset + slot->size)
enduseful = slot->offset + slot->size;
}
results->ulr_endoff = roundup(enduseful, dirblksiz);
#if 0 /* commented out by dbj. none of the on disk fields changed */
ip->i_flag |= IN_CHANGE | IN_UPDATE;
#endif
return EJUSTRETURN;
}
/*
* Check if we can delete inode tdp in directory vdp with inode ip and creds.
*/
static int
ufs_can_delete(struct vnode *tdp, struct vnode *vdp, struct inode *ip,
kauth_cred_t cred)
{
int error;
/*
* Write access to directory required to delete files.
*/
error = VOP_ACCESS(vdp, VWRITE, cred);
if (error)
goto out;
if (!(ip->i_mode & ISVTX))
return 0;
/*
* If directory is "sticky", then user must own
* the directory, or the file in it, else she
* may not delete it (unless she's root). This
* implements append-only directories.
*/
error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE, tdp, vdp,
genfs_can_sticky(cred, ip->i_uid, VTOI(tdp)->i_uid));
if (error) {
error = EPERM; // Why override?
goto out;
}
return 0;
out:
vrele(tdp);
return error;
}
static int
ufs_getino(struct vnode *vdp, struct inode *ip, ino_t foundino,
struct vnode **tdp, bool same)
{
if (ip->i_number == foundino) {
if (same)
return EISDIR;
vref(vdp);
*tdp = vdp;
return 0;
}
return vcache_get(vdp->v_mount, &foundino, sizeof(foundino), tdp);
}
/*
* Convert a component of a pathname into a pointer to a locked inode.
* This is a very central and rather complicated routine.
* If the file system is not maintained in a strict tree hierarchy,
* this can result in a deadlock situation (see comments in code below).
*
* The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
* on whether the name is to be looked up, created, renamed, or deleted.
* When CREATE, RENAME, or DELETE is specified, information usable in
* creating, renaming, or deleting a directory entry may be calculated.
* If flag has LOCKPARENT or'ed into it and the target of the pathname
* exists, lookup returns both the target and its parent directory locked.
* When creating or renaming and LOCKPARENT is specified, the target may
* not be ".". When deleting and LOCKPARENT is specified, the target may
* be "."., but the caller must check to ensure it does an vrele and vput
* instead of two vputs.
*
* Overall outline of ufs_lookup:
*
* check accessibility of directory
* look for name in cache, if found, then if at end of path
* and deleting or creating, drop it, else return name
* search for name in directory, to found or notfound
* notfound:
* if creating, return locked directory, leaving info on available slots
* else return error
* found:
* if at end of path and deleting, return information to allow delete
* if at end of path and rewriting (RENAME and LOCKPARENT), lock target
* inode and return info to allow rewrite
* if not at end, add name to cache; if at end and neither creating
* nor deleting, add name to cache
*/
int
ufs_lookup(void *v)
{
struct vop_lookup_v2_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
} */ *ap = v;
struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
struct inode *dp = VTOI(vdp); /* inode for directory being searched */
struct buf *bp; /* a buffer of directory entries */
struct direct *ep; /* the current directory entry */
int entryoffsetinblock; /* offset of ep in bp's buffer */
struct slotinfo slot;
int numdirpasses; /* strategy for directory search */
doff_t endsearch; /* offset to end directory search */
doff_t prevoff; /* previous value of ulr_offset */
struct vnode *tdp; /* returned by vcache_get */
doff_t enduseful; /* pointer past last used dir slot.
used for directory truncation. */
u_long bmask; /* block offset mask */
int error;
struct vnode **vpp = ap->a_vpp;
struct componentname *cnp = ap->a_cnp;
kauth_cred_t cred = cnp->cn_cred;
int flags;
int nameiop = cnp->cn_nameiop;
struct ufsmount *ump = dp->i_ump;
const int needswap = UFS_MPNEEDSWAP(ump);
int dirblksiz = ump->um_dirblksiz;
ino_t foundino;
struct ufs_lookup_results *results;
int iswhiteout; /* temp result from cache_lookup() */
const int fsfmt = FSFMT(vdp);
uint16_t reclen;
flags = cnp->cn_flags;
bp = NULL;
*vpp = NULL;
endsearch = 0; /* silence compiler warning */
/*
* Produce the auxiliary lookup results into i_crap. Increment
* its serial number so elsewhere we can tell if we're using
* stale results. This should not be done this way. XXX.
*/
results = &dp->i_crap;
dp->i_crapcounter++;
/*
* Check accessiblity of directory.
*/
if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
return (error);
if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
(nameiop == DELETE || nameiop == RENAME))
return (EROFS);
/*
* We now have a segment name to search for, and a directory to search.
*
* Before tediously performing a linear scan of the directory,
* check the name cache to see if the directory/name pair
* we are looking for is known already.
*/
if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
cnp->cn_nameiop, cnp->cn_flags, &iswhiteout, vpp)) {
if (iswhiteout) {
cnp->cn_flags |= ISWHITEOUT;
}
return *vpp == NULLVP ? ENOENT : 0;
}
if (iswhiteout) {
/*
* The namecache set iswhiteout without finding a
* cache entry. As of this writing (20121014), this
* can happen if there was a whiteout entry that has
* been invalidated by the lookup. It is not clear if
* it is correct to set ISWHITEOUT in this case or
* not; however, doing so retains the prior behavior,
* so we'll go with that until some clearer answer
* appears. XXX
*/
cnp->cn_flags |= ISWHITEOUT;
}
/*
* Suppress search for slots unless creating
* file and at end of pathname, in which case
* we watch for a place to put the new file in
* case it doesn't already exist.
*/
slot_init(&slot);
if ((nameiop == CREATE || nameiop == RENAME) && (flags & ISLASTCN)) {
slot.status = NONE;
slot.needed = UFS_DIRECTSIZ(cnp->cn_namelen);
}
/*
* If there is cached information on a previous search of
* this directory, pick up where we last left off.
* We cache only lookups as these are the most common
* and have the greatest payoff. Caching CREATE has little
* benefit as it usually must search the entire directory
* to determine that the entry does not exist. Caching the
* location of the last DELETE or RENAME has not reduced
* profiling time and hence has been removed in the interest
* of simplicity.
*/
bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
#ifdef UFS_DIRHASH
/*
* Use dirhash for fast operations on large directories. The logic
* to determine whether to hash the directory is contained within
* ufsdirhash_build(); a zero return means that it decided to hash
* this directory and it successfully built up the hash table.
*/
if (ufsdirhash_build(dp) == 0) {
/* Look for a free slot if needed. */
enduseful = slot_findfree(&slot, dp);
/* Look up the component. */
numdirpasses = 1;
entryoffsetinblock = 0; /* silence compiler warning */
switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
&results->ulr_offset, &bp,
nameiop == DELETE ? &prevoff : NULL)) {
case 0:
ep = (void *)((char *)bp->b_data +
(results->ulr_offset & bmask));
reclen = ufs_rw16(ep->d_reclen, needswap);
goto foundentry;
case ENOENT:
results->ulr_offset = roundup(dp->i_size, dirblksiz);
goto notfound;
default:
/* Something failed; just do a linear search. */
break;
}
}
#endif /* UFS_DIRHASH */
if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
results->ulr_diroff >= dp->i_size) {
entryoffsetinblock = 0;
results->ulr_offset = 0;
numdirpasses = 1;
} else {
results->ulr_offset = results->ulr_diroff;
entryoffsetinblock = results->ulr_offset & bmask;
if (entryoffsetinblock != 0 &&
(error = ufs_blkatoff(vdp, (off_t)results->ulr_offset,
NULL, &bp, false)))
goto out;
numdirpasses = 2;
namecache_count_2passes();
}
prevoff = results->ulr_offset;
endsearch = roundup(dp->i_size, dirblksiz);
enduseful = 0;
searchloop:
while (results->ulr_offset < endsearch) {
if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
preempt();
/*
* If necessary, get the next directory block.
*/
if ((results->ulr_offset & bmask) == 0) {
if (bp != NULL)
brelse(bp, 0);
error = ufs_blkatoff(vdp, (off_t)results->ulr_offset,
NULL, &bp, false);
if (error)
goto out;
entryoffsetinblock = 0;
}
/*
* If still looking for a slot, and at a DIRBLKSIZ
* boundary, have to start looking for free space again.
*/
if (slot.status == NONE &&
(entryoffsetinblock & (dirblksiz - 1)) == 0) {
slot.offset = -1;
slot.freespace = 0;
}
/*
* Get pointer to next entry.
* Full validation checks are slow, so we only check
* enough to insure forward progress through the
* directory. Complete checks can be run by patching
* "dirchk" to be true.
*/
KASSERT(bp != NULL);
ep = (void *)((char *)bp->b_data + entryoffsetinblock);
const char *msg;
reclen = ufs_rw16(ep->d_reclen, needswap);
if ((reclen == 0 && (msg = "null entry")) || (dirchk &&
(msg = ufs_dirbadentry(vdp, ep, entryoffsetinblock)))) {
ufs_dirbad(dp, results->ulr_offset, msg);
reclen = dirblksiz -
(entryoffsetinblock & (dirblksiz - 1));
goto next;
}
/*
* If an appropriate sized slot has not yet been found,
* check to see if one is available. Also accumulate space
* in the current block so that we can determine if
* compaction is viable.
*/
if (slot.status != FOUND) {
int size = reclen;
if (ep->d_ino != 0)
size -= UFS_DIRSIZ(fsfmt, ep, needswap);
if (size > 0)
slot_update(&slot, size, reclen,
results->ulr_offset);
}
if (ep->d_ino == 0)
goto next;
/*
* Check for a name match.
*/
const uint16_t namlen = NAMLEN(fsfmt, needswap, ep);
if (namlen != cnp->cn_namelen ||
memcmp(cnp->cn_nameptr, ep->d_name, (size_t)namlen))
goto next;
#ifdef UFS_DIRHASH
foundentry:
#endif
/*
* Save directory entry's inode number and
* reclen, and release directory buffer.
*/
if (!fsfmt && ep->d_type == DT_WHT) {
slot_white(&slot, reclen, results);
/*
* This is used to set results->ulr_endoff, which may
* be used by ufs_direnter() as a length to truncate
* the directory to. Therefore, it must point past the
* end of the last non-empty directory entry. We don't
* know where that is in this case, so we effectively
* disable shrinking by using the existing size of the
* directory.
*
* Note that we wouldn't expect to shrink the
* directory while rewriting an existing entry anyway.
*/
enduseful = endsearch;
cnp->cn_flags |= ISWHITEOUT;
numdirpasses--;
goto notfound;
}
foundino = ufs_rw32(ep->d_ino, needswap);
results->ulr_reclen = reclen;
goto found;
next:
prevoff = results->ulr_offset;
results->ulr_offset += reclen;
entryoffsetinblock += reclen;
if (ep->d_ino)
enduseful = results->ulr_offset;
}
notfound:
/*
* If we started in the middle of the directory and failed
* to find our target, we must check the beginning as well.
*/
if (numdirpasses == 2) {
numdirpasses--;
results->ulr_offset = 0;
endsearch = results->ulr_diroff;
goto searchloop;
}
if (bp != NULL)
brelse(bp, 0);
/*
* If creating, and at end of pathname and current
* directory has not been removed, then can consider
* allowing file to be created.
*/
if ((nameiop == CREATE || nameiop == RENAME ||
(nameiop == DELETE &&
(cnp->cn_flags & DOWHITEOUT) &&
(cnp->cn_flags & ISWHITEOUT))) &&
(flags & ISLASTCN) && dp->i_nlink != 0) {
/*
* Access for write is interpreted as allowing
* creation of files in the directory.
*/
error = VOP_ACCESS(vdp, VWRITE, cred);
if (error)
goto out;
error = slot_estimate(&slot, dirblksiz, nameiop,
prevoff, enduseful, dp, results);
/*
* We return with the directory locked, so that
* the parameters we set up above will still be
* valid if we actually decide to do a direnter().
* We return ni_vp == NULL to indicate that the entry
* does not currently exist; we leave a pointer to
* the (locked) directory inode in ndp->ni_dvp.
*
* NB - if the directory is unlocked, then this
* information cannot be used.
*/
goto out;
}
/*
* Insert name into cache (as non-existent) if appropriate.
*/
if (nameiop != CREATE) {
cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
cnp->cn_flags);
}
error = ENOENT;
goto out;
found:
if (numdirpasses == 2)
namecache_count_pass2();
/*
* Check that directory length properly reflects presence
* of this entry.
*/
const uint64_t newisize =
results->ulr_offset + UFS_DIRSIZ(fsfmt, ep, needswap);
if (newisize > dp->i_size) {
ufs_dirbad(dp, results->ulr_offset, "i_size too small");
dp->i_size = newisize;
DIP_ASSIGN(dp, size, dp->i_size);
dp->i_flag |= IN_CHANGE | IN_UPDATE;
UFS_WAPBL_UPDATE(vdp, NULL, NULL, UPDATE_DIROP);
}
brelse(bp, 0);
/*
* Found component in pathname.
* If the final component of path name, save information
* in the cache as to where the entry was found.
*/
if ((flags & ISLASTCN) && nameiop == LOOKUP)
results->ulr_diroff = results->ulr_offset & ~(dirblksiz - 1);
/*
* If deleting, and at end of pathname, return
* parameters which can be used to remove file.
* Lock the inode, being careful with ".".
*/
if (nameiop == DELETE && (flags & ISLASTCN)) {
/*
* Return pointer to current entry in results->ulr_offset,
* and distance past previous entry (if there
* is a previous entry in this block) in results->ulr_count.
* Save directory inode pointer in ndp->ni_dvp for dirremove().
*/
calc_count(results, dirblksiz, prevoff);
if ((error = ufs_getino(vdp, dp, foundino, &tdp, false)) != 0)
goto out;
if ((error = ufs_can_delete(tdp, vdp, dp, cred)) != 0)
goto out;
*vpp = tdp;
goto out;
}
/*
* If rewriting (RENAME), return the inode and the
* information required to rewrite the present directory
* Must get inode of directory entry to verify it's a
* regular file, or empty directory.
*/
if (nameiop == RENAME && (flags & ISLASTCN)) {
if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0)
goto out;
/*
* Careful about locking second inode.
* This can only occur if the target is ".".
*/
if ((error = ufs_getino(vdp, dp, foundino, &tdp, true)) != 0)
goto out;
*vpp = tdp;
goto out;
}
if ((error = ufs_getino(vdp, dp, foundino, &tdp, false)) != 0)
goto out;
*vpp = tdp;
/*
* Insert name into cache if appropriate.
*/
cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
error = 0;
out:
return error;
}
void
ufs_dirbad(struct inode *ip, doff_t offset, const char *how)
{
struct mount *mp = ITOV(ip)->v_mount;
void (*p)(const char *, ...) __printflike(1, 2) =
(mp->mnt_flag & MNT_RDONLY) == 0 ? panic : (void *)printf;
(*p)("%s: bad dir ino %ju at offset %d: %s\n",
mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
offset, how);
}
/*
* Do consistency checking on a directory entry:
* record length must be multiple of 4
* entry must fit in rest of its DIRBLKSIZ block
* record must be large enough to contain entry
* name is not longer than FFS_MAXNAMLEN
* name must be as long as advertised, and null terminated
*/
const char *
ufs_dirbadentry(const struct vnode *dp, const struct direct *ep,
int entryoffsetinblock)
{
const struct ufsmount *ump = VFSTOUFS(dp->v_mount);
const int needswap = UFS_MPNEEDSWAP(ump);
const int dirblksiz = ump->um_dirblksiz;
const int maxsize = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
const int fsfmt = FSFMT(dp);
const uint8_t namlen = NAMLEN(fsfmt, needswap, ep);
const uint16_t reclen = ufs_rw16(ep->d_reclen, needswap);
const int dirsiz = (int)UFS_DIRSIZ(fsfmt, ep, needswap);
const char *name = ep->d_name;
const char *str;
#ifdef DIAGNOSTIC
static char buf[512];
#endif
if ((reclen & 0x3) != 0)
str = "not rounded";
else if (reclen > maxsize)
str = "too big";
else if (reclen < dirsiz)
str = "too small";
#if FFS_MAXNAMLEN < 255
else if (namlen > FFS_MAXNAMLEN)
str = "long name";
#endif
else
str = NULL;
if (str) {
#ifdef DIAGNOSTIC
snprintf(buf, sizeof(buf), "Bad dir (%s), reclen=%#x, "
"namlen=%d, dirsiz=%d <= reclen=%d <= maxsize=%d, "
"flags=%#x, entryoffsetinblock=%d, dirblksiz=%d",
str, reclen, namlen, dirsiz, reclen, maxsize,
dp->v_mount->mnt_flag, entryoffsetinblock, dirblksiz);
str = buf;
#endif
return str;
}
if (ep->d_ino == 0)
return NULL;
for (uint8_t i = 0; i < namlen; i++)
if (name[i] == '\0') {
str = "NUL in name";
#ifdef DIAGNOSTIC
snprintf(buf, sizeof(buf), "%s [%s] i=%d, namlen=%d",
str, name, i, namlen);
str = buf;
#endif
return str;
}
if (name[namlen]) {
str = "missing NUL in name";
#ifdef DIAGNOSTIC
snprintf(buf, sizeof(buf), "%s [%*.*s] namlen=%d", str,
namlen, namlen, name, namlen);
str = buf;
#endif
return str;
}
return NULL;
}
/*
* Construct a new directory entry after a call to namei, using the
* name in the componentname argument cnp. The argument ip is the
* inode to which the new directory entry will refer.
*/
void
ufs_makedirentry(struct inode *ip, struct componentname *cnp,
struct direct *newdirp)
{
newdirp->d_ino = ip->i_number;
newdirp->d_namlen = cnp->cn_namelen;
memcpy(newdirp->d_name, cnp->cn_nameptr, (size_t)cnp->cn_namelen);
newdirp->d_name[cnp->cn_namelen] = '\0';
if (FSFMT(ITOV(ip)))
newdirp->d_type = 0;
else
newdirp->d_type = IFTODT(ip->i_mode);
}
static int
ufs_dirgrow(struct vnode *dvp, const struct ufs_lookup_results *ulr,
struct vnode *tvp, struct direct *dirp,
struct componentname *cnp, struct buf *newdirbp)
{
const kauth_cred_t cr = cnp->cn_cred;
const struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
const int needswap = UFS_MPNEEDSWAP(ump);
const int dirblksiz = ump->um_dirblksiz;
const int fsfmt = FSFMT(dvp);
const u_int newentrysize = UFS_DIRSIZ(0, dirp, 0);
struct inode *dp = VTOI(dvp);
int error, ret, blkoff;
struct timespec ts;
struct buf *bp;
/*
* If ulr_count is 0, then namei could find no
* space in the directory. Here, ulr_offset will
* be on a directory block boundary and we will write the
* new entry into a fresh block.
*/
if (ulr->ulr_offset & (dirblksiz - 1))
panic("%s: newblk", __func__);
if ((error = UFS_BALLOC(dvp, (off_t)ulr->ulr_offset, dirblksiz,
cr, B_CLRBUF | B_SYNC, &bp)) != 0) {
return error;
}
dp->i_size = ulr->ulr_offset + dirblksiz;
DIP_ASSIGN(dp, size, dp->i_size);
dp->i_flag |= IN_CHANGE | IN_UPDATE;
uvm_vnp_setsize(dvp, dp->i_size);
dirp->d_reclen = ufs_rw16(dirblksiz, needswap);
dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
if (fsfmt && ENDIANSWAP(needswap))
ufs_dirswap(dirp);
blkoff = ulr->ulr_offset & (ump->um_mountp->mnt_stat.f_iosize - 1);
memcpy((char *)bp->b_data + blkoff, dirp, newentrysize);
#ifdef UFS_DIRHASH
if (dp->i_dirhash != NULL) {
ufsdirhash_newblk(dp, ulr->ulr_offset);
ufsdirhash_add(dp, dirp, ulr->ulr_offset);
ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
ulr->ulr_offset);
}
#endif
error = VOP_BWRITE(bp->b_vp, bp);
vfs_timestamp(&ts);
ret = UFS_UPDATE(dvp, &ts, &ts, UPDATE_DIROP);
if (error == 0)
return ret;
return error;
}
static int
#if __GNUC_PREREQ__(5, 3)
/* This gets miscompiled by gcc 5.3 PR/51094 */
__attribute__((__optimize__("no-tree-vrp")))
#endif
ufs_dircompact(struct vnode *dvp, const struct ufs_lookup_results *ulr,
struct vnode *tvp, struct direct *dirp,
struct componentname *cnp, struct buf *newdirbp)
{
const struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
const int needswap = UFS_MPNEEDSWAP(ump);
const int fsfmt = FSFMT(dvp);
const u_int newentrysize = UFS_DIRSIZ(0, dirp, 0);
struct inode *dp = VTOI(dvp);
struct buf *bp;
u_int dsize;
struct direct *ep, *nep;
int error, loc, spacefree;
char *dirbuf;
uint16_t reclen;
UFS_WAPBL_JLOCK_ASSERT(dvp->v_mount);
/*
* If ulr_count is non-zero, then namei found space for the new
* entry in the range ulr_offset to ulr_offset + ulr_count
* in the directory. To use this space, we may have to compact
* the entries located there, by copying them together towards the
* beginning of the block, leaving the free space in one usable
* chunk at the end.
*/
/*
* Increase size of directory if entry eats into new space.
* This should never push the size past a new multiple of
* DIRBLKSIZ.
*
* N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
*/
if (ulr->ulr_offset + ulr->ulr_count > dp->i_size) {
#ifdef DIAGNOSTIC
printf("%s: reached 4.2-only block, not supposed to happen\n",
__func__);
#endif
dp->i_size = ulr->ulr_offset + ulr->ulr_count;
DIP_ASSIGN(dp, size, dp->i_size);
dp->i_flag |= IN_CHANGE | IN_UPDATE;
UFS_WAPBL_UPDATE(dvp, NULL, NULL, UPDATE_DIROP);
}
/*
* Get the block containing the space for the new directory entry.
*/
error = ufs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp, true);
if (error)
return error;
/*
* Find space for the new entry. In the simple case, the entry at
* offset base will have the space. If it does not, then namei
* arranged that compacting the region ulr_offset to
* ulr_offset + ulr_count would yield the space.
*/
ep = (void *)dirbuf;
dsize = (ep->d_ino != 0) ? UFS_DIRSIZ(fsfmt, ep, needswap) : 0;
reclen = ufs_rw16(ep->d_reclen, needswap);
spacefree = reclen - dsize;
for (loc = reclen; loc < ulr->ulr_count; ) {
nep = (void *)(dirbuf + loc);
/* Trim the existing slot (NB: dsize may be zero). */
ep->d_reclen = ufs_rw16(dsize, needswap);
ep = (void *)((char *)ep + dsize);
reclen = ufs_rw16(nep->d_reclen, needswap);
loc += reclen;
if (nep->d_ino == 0) {
/*
* A mid-block unused entry. Such entries are
* never created by the kernel, but fsck_ffs
* can create them (and it doesn't fix them).
*
* Add up the free space, and initialise the
* relocated entry since we don't memcpy it.
*/
spacefree += reclen;
ep->d_ino = 0;
dsize = 0;
continue;
}
dsize = UFS_DIRSIZ(fsfmt, nep, needswap);
spacefree += reclen - dsize;
#ifdef UFS_DIRHASH
if (dp->i_dirhash != NULL)
ufsdirhash_move(dp, nep,
ulr->ulr_offset + ((char *)nep - dirbuf),
ulr->ulr_offset + ((char *)ep - dirbuf));
#endif
memcpy(ep, nep, dsize);
}
/*
* Here, `ep' points to a directory entry containing `dsize' in-use
* bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
* then the entry is completely unused (dsize == 0). The value
* of ep->d_reclen is always indeterminate.
*
* Update the pointer fields in the previous entry (if any),
* copy in the new entry, and write out the block.
*/
if (ep->d_ino == 0 ||
(ufs_rw32(ep->d_ino, needswap) == UFS_WINO &&
memcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
if (spacefree + dsize < newentrysize)
panic("%s: too big", __func__);
dirp->d_reclen = spacefree + dsize;
} else {
if (spacefree < newentrysize)
panic("%s: nospace", __func__);
dirp->d_reclen = spacefree;
ep->d_reclen = ufs_rw16(dsize, needswap);
ep = (void *)((char *)ep + dsize);
}
dirp->d_reclen = ufs_rw16(dirp->d_reclen, needswap);
dirp->d_ino = ufs_rw32(dirp->d_ino, needswap);
if (fsfmt && ENDIANSWAP(needswap))
ufs_dirswap(dirp);
#ifdef UFS_DIRHASH
if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
dirp->d_reclen == spacefree))
ufsdirhash_add(dp, dirp, ulr->ulr_offset + ((char *)ep - dirbuf));
#endif
memcpy(ep, dirp, newentrysize);
#ifdef UFS_DIRHASH
if (dp->i_dirhash != NULL) {
const int dirblkmsk = ump->um_dirblksiz - 1;
ufsdirhash_checkblock(dp, dirbuf -
(ulr->ulr_offset & dirblkmsk),
ulr->ulr_offset & ~dirblkmsk);
}