1
2
3
4
5
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
11#include "xfs_mount.h"
12#include "xfs_defer.h"
13#include "xfs_da_format.h"
14#include "xfs_da_btree.h"
15#include "xfs_inode.h"
16#include "xfs_trans.h"
17#include "xfs_inode_item.h"
18#include "xfs_bmap.h"
19#include "xfs_dir2.h"
20#include "xfs_dir2_priv.h"
21#include "xfs_ialloc.h"
22#include "xfs_errortag.h"
23#include "xfs_error.h"
24#include "xfs_trace.h"
25
26struct xfs_name xfs_name_dotdot = { (unsigned char *)"..", 2, XFS_DIR3_FT_DIR };
27
28
29
30
31unsigned char
32xfs_mode_to_ftype(
33 int mode)
34{
35 switch (mode & S_IFMT) {
36 case S_IFREG:
37 return XFS_DIR3_FT_REG_FILE;
38 case S_IFDIR:
39 return XFS_DIR3_FT_DIR;
40 case S_IFCHR:
41 return XFS_DIR3_FT_CHRDEV;
42 case S_IFBLK:
43 return XFS_DIR3_FT_BLKDEV;
44 case S_IFIFO:
45 return XFS_DIR3_FT_FIFO;
46 case S_IFSOCK:
47 return XFS_DIR3_FT_SOCK;
48 case S_IFLNK:
49 return XFS_DIR3_FT_SYMLINK;
50 default:
51 return XFS_DIR3_FT_UNKNOWN;
52 }
53}
54
55
56
57
58
59STATIC xfs_dahash_t
60xfs_ascii_ci_hashname(
61 struct xfs_name *name)
62{
63 xfs_dahash_t hash;
64 int i;
65
66 for (i = 0, hash = 0; i < name->len; i++)
67 hash = tolower(name->name[i]) ^ rol32(hash, 7);
68
69 return hash;
70}
71
72STATIC enum xfs_dacmp
73xfs_ascii_ci_compname(
74 struct xfs_da_args *args,
75 const unsigned char *name,
76 int len)
77{
78 enum xfs_dacmp result;
79 int i;
80
81 if (args->namelen != len)
82 return XFS_CMP_DIFFERENT;
83
84 result = XFS_CMP_EXACT;
85 for (i = 0; i < len; i++) {
86 if (args->name[i] == name[i])
87 continue;
88 if (tolower(args->name[i]) != tolower(name[i]))
89 return XFS_CMP_DIFFERENT;
90 result = XFS_CMP_CASE;
91 }
92
93 return result;
94}
95
96static const struct xfs_nameops xfs_ascii_ci_nameops = {
97 .hashname = xfs_ascii_ci_hashname,
98 .compname = xfs_ascii_ci_compname,
99};
100
101int
102xfs_da_mount(
103 struct xfs_mount *mp)
104{
105 struct xfs_da_geometry *dageo;
106 int nodehdr_size;
107
108
109 ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
110 ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
111
112 mp->m_dir_inode_ops = xfs_dir_get_ops(mp, NULL);
113 mp->m_nondir_inode_ops = xfs_nondir_get_ops(mp, NULL);
114
115 nodehdr_size = mp->m_dir_inode_ops->node_hdr_size;
116 mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
117 KM_SLEEP | KM_MAYFAIL);
118 mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
119 KM_SLEEP | KM_MAYFAIL);
120 if (!mp->m_dir_geo || !mp->m_attr_geo) {
121 kmem_free(mp->m_dir_geo);
122 kmem_free(mp->m_attr_geo);
123 return -ENOMEM;
124 }
125
126
127 dageo = mp->m_dir_geo;
128 dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
129 dageo->fsblog = mp->m_sb.sb_blocklog;
130 dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
131 dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
132
133
134
135
136
137 dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
138 dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
139 dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
140 dageo->node_ents = (dageo->blksize - nodehdr_size) /
141 (uint)sizeof(xfs_da_node_entry_t);
142 dageo->magicpct = (dageo->blksize * 37) / 100;
143
144
145 dageo = mp->m_attr_geo;
146 dageo->blklog = mp->m_sb.sb_blocklog;
147 dageo->fsblog = mp->m_sb.sb_blocklog;
148 dageo->blksize = 1 << dageo->blklog;
149 dageo->fsbcount = 1;
150 dageo->node_ents = (dageo->blksize - nodehdr_size) /
151 (uint)sizeof(xfs_da_node_entry_t);
152 dageo->magicpct = (dageo->blksize * 37) / 100;
153
154 if (xfs_sb_version_hasasciici(&mp->m_sb))
155 mp->m_dirnameops = &xfs_ascii_ci_nameops;
156 else
157 mp->m_dirnameops = &xfs_default_nameops;
158
159 return 0;
160}
161
162void
163xfs_da_unmount(
164 struct xfs_mount *mp)
165{
166 kmem_free(mp->m_dir_geo);
167 kmem_free(mp->m_attr_geo);
168}
169
170
171
172
173int
174xfs_dir_isempty(
175 xfs_inode_t *dp)
176{
177 xfs_dir2_sf_hdr_t *sfp;
178
179 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
180 if (dp->i_d.di_size == 0)
181 return 1;
182 if (dp->i_d.di_size > XFS_IFORK_DSIZE(dp))
183 return 0;
184 sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
185 return !sfp->count;
186}
187
188
189
190
191int
192xfs_dir_ino_validate(
193 xfs_mount_t *mp,
194 xfs_ino_t ino)
195{
196 bool ino_ok = xfs_verify_dir_ino(mp, ino);
197
198 if (unlikely(XFS_TEST_ERROR(!ino_ok, mp, XFS_ERRTAG_DIR_INO_VALIDATE))) {
199 xfs_warn(mp, "Invalid inode number 0x%Lx",
200 (unsigned long long) ino);
201 XFS_ERROR_REPORT("xfs_dir_ino_validate", XFS_ERRLEVEL_LOW, mp);
202 return -EFSCORRUPTED;
203 }
204 return 0;
205}
206
207
208
209
210int
211xfs_dir_init(
212 xfs_trans_t *tp,
213 xfs_inode_t *dp,
214 xfs_inode_t *pdp)
215{
216 struct xfs_da_args *args;
217 int error;
218
219 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
220 error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
221 if (error)
222 return error;
223
224 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
225 if (!args)
226 return -ENOMEM;
227
228 args->geo = dp->i_mount->m_dir_geo;
229 args->dp = dp;
230 args->trans = tp;
231 error = xfs_dir2_sf_create(args, pdp->i_ino);
232 kmem_free(args);
233 return error;
234}
235
236
237
238
239
240int
241xfs_dir_createname(
242 struct xfs_trans *tp,
243 struct xfs_inode *dp,
244 struct xfs_name *name,
245 xfs_ino_t inum,
246 xfs_extlen_t total)
247{
248 struct xfs_da_args *args;
249 int rval;
250 int v;
251
252 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
253
254 if (inum) {
255 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
256 if (rval)
257 return rval;
258 XFS_STATS_INC(dp->i_mount, xs_dir_create);
259 }
260
261 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
262 if (!args)
263 return -ENOMEM;
264
265 args->geo = dp->i_mount->m_dir_geo;
266 args->name = name->name;
267 args->namelen = name->len;
268 args->filetype = name->type;
269 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
270 args->inumber = inum;
271 args->dp = dp;
272 args->total = total;
273 args->whichfork = XFS_DATA_FORK;
274 args->trans = tp;
275 args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
276 if (!inum)
277 args->op_flags |= XFS_DA_OP_JUSTCHECK;
278
279 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
280 rval = xfs_dir2_sf_addname(args);
281 goto out_free;
282 }
283
284 rval = xfs_dir2_isblock(args, &v);
285 if (rval)
286 goto out_free;
287 if (v) {
288 rval = xfs_dir2_block_addname(args);
289 goto out_free;
290 }
291
292 rval = xfs_dir2_isleaf(args, &v);
293 if (rval)
294 goto out_free;
295 if (v)
296 rval = xfs_dir2_leaf_addname(args);
297 else
298 rval = xfs_dir2_node_addname(args);
299
300out_free:
301 kmem_free(args);
302 return rval;
303}
304
305
306
307
308
309int
310xfs_dir_cilookup_result(
311 struct xfs_da_args *args,
312 const unsigned char *name,
313 int len)
314{
315 if (args->cmpresult == XFS_CMP_DIFFERENT)
316 return -ENOENT;
317 if (args->cmpresult != XFS_CMP_CASE ||
318 !(args->op_flags & XFS_DA_OP_CILOOKUP))
319 return -EEXIST;
320
321 args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
322 if (!args->value)
323 return -ENOMEM;
324
325 memcpy(args->value, name, len);
326 args->valuelen = len;
327 return -EEXIST;
328}
329
330
331
332
333
334
335
336int
337xfs_dir_lookup(
338 xfs_trans_t *tp,
339 xfs_inode_t *dp,
340 struct xfs_name *name,
341 xfs_ino_t *inum,
342 struct xfs_name *ci_name)
343{
344 struct xfs_da_args *args;
345 int rval;
346 int v;
347 int lock_mode;
348
349 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
350 XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
351
352
353
354
355
356
357
358
359
360 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
361 args->geo = dp->i_mount->m_dir_geo;
362 args->name = name->name;
363 args->namelen = name->len;
364 args->filetype = name->type;
365 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
366 args->dp = dp;
367 args->whichfork = XFS_DATA_FORK;
368 args->trans = tp;
369 args->op_flags = XFS_DA_OP_OKNOENT;
370 if (ci_name)
371 args->op_flags |= XFS_DA_OP_CILOOKUP;
372
373 lock_mode = xfs_ilock_data_map_shared(dp);
374 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
375 rval = xfs_dir2_sf_lookup(args);
376 goto out_check_rval;
377 }
378
379 rval = xfs_dir2_isblock(args, &v);
380 if (rval)
381 goto out_free;
382 if (v) {
383 rval = xfs_dir2_block_lookup(args);
384 goto out_check_rval;
385 }
386
387 rval = xfs_dir2_isleaf(args, &v);
388 if (rval)
389 goto out_free;
390 if (v)
391 rval = xfs_dir2_leaf_lookup(args);
392 else
393 rval = xfs_dir2_node_lookup(args);
394
395out_check_rval:
396 if (rval == -EEXIST)
397 rval = 0;
398 if (!rval) {
399 *inum = args->inumber;
400 if (ci_name) {
401 ci_name->name = args->value;
402 ci_name->len = args->valuelen;
403 }
404 }
405out_free:
406 xfs_iunlock(dp, lock_mode);
407 kmem_free(args);
408 return rval;
409}
410
411
412
413
414int
415xfs_dir_removename(
416 struct xfs_trans *tp,
417 struct xfs_inode *dp,
418 struct xfs_name *name,
419 xfs_ino_t ino,
420 xfs_extlen_t total)
421{
422 struct xfs_da_args *args;
423 int rval;
424 int v;
425
426 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
427 XFS_STATS_INC(dp->i_mount, xs_dir_remove);
428
429 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
430 if (!args)
431 return -ENOMEM;
432
433 args->geo = dp->i_mount->m_dir_geo;
434 args->name = name->name;
435 args->namelen = name->len;
436 args->filetype = name->type;
437 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
438 args->inumber = ino;
439 args->dp = dp;
440 args->total = total;
441 args->whichfork = XFS_DATA_FORK;
442 args->trans = tp;
443
444 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
445 rval = xfs_dir2_sf_removename(args);
446 goto out_free;
447 }
448
449 rval = xfs_dir2_isblock(args, &v);
450 if (rval)
451 goto out_free;
452 if (v) {
453 rval = xfs_dir2_block_removename(args);
454 goto out_free;
455 }
456
457 rval = xfs_dir2_isleaf(args, &v);
458 if (rval)
459 goto out_free;
460 if (v)
461 rval = xfs_dir2_leaf_removename(args);
462 else
463 rval = xfs_dir2_node_removename(args);
464out_free:
465 kmem_free(args);
466 return rval;
467}
468
469
470
471
472int
473xfs_dir_replace(
474 struct xfs_trans *tp,
475 struct xfs_inode *dp,
476 struct xfs_name *name,
477 xfs_ino_t inum,
478 xfs_extlen_t total)
479{
480 struct xfs_da_args *args;
481 int rval;
482 int v;
483
484 ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
485
486 rval = xfs_dir_ino_validate(tp->t_mountp, inum);
487 if (rval)
488 return rval;
489
490 args = kmem_zalloc(sizeof(*args), KM_SLEEP | KM_NOFS);
491 if (!args)
492 return -ENOMEM;
493
494 args->geo = dp->i_mount->m_dir_geo;
495 args->name = name->name;
496 args->namelen = name->len;
497 args->filetype = name->type;
498 args->hashval = dp->i_mount->m_dirnameops->hashname(name);
499 args->inumber = inum;
500 args->dp = dp;
501 args->total = total;
502 args->whichfork = XFS_DATA_FORK;
503 args->trans = tp;
504
505 if (dp->i_d.di_format == XFS_DINODE_FMT_LOCAL) {
506 rval = xfs_dir2_sf_replace(args);
507 goto out_free;
508 }
509
510 rval = xfs_dir2_isblock(args, &v);
511 if (rval)
512 goto out_free;
513 if (v) {
514 rval = xfs_dir2_block_replace(args);
515 goto out_free;
516 }
517
518 rval = xfs_dir2_isleaf(args, &v);
519 if (rval)
520 goto out_free;
521 if (v)
522 rval = xfs_dir2_leaf_replace(args);
523 else
524 rval = xfs_dir2_node_replace(args);
525out_free:
526 kmem_free(args);
527 return rval;
528}
529
530
531
532
533int
534xfs_dir_canenter(
535 xfs_trans_t *tp,
536 xfs_inode_t *dp,
537 struct xfs_name *name)
538{
539 return xfs_dir_createname(tp, dp, name, 0, 0);
540}
541
542
543
544
545
546
547
548
549
550
551
552int
553xfs_dir2_grow_inode(
554 struct xfs_da_args *args,
555 int space,
556 xfs_dir2_db_t *dbp)
557{
558 struct xfs_inode *dp = args->dp;
559 struct xfs_mount *mp = dp->i_mount;
560 xfs_fileoff_t bno;
561 int count;
562 int error;
563
564 trace_xfs_dir2_grow_inode(args, space);
565
566
567
568
569 bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
570 count = args->geo->fsbcount;
571
572 error = xfs_da_grow_inode_int(args, &bno, count);
573 if (error)
574 return error;
575
576 *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
577
578
579
580
581 if (space == XFS_DIR2_DATA_SPACE) {
582 xfs_fsize_t size;
583
584 size = XFS_FSB_TO_B(mp, bno + count);
585 if (size > dp->i_d.di_size) {
586 dp->i_d.di_size = size;
587 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
588 }
589 }
590 return 0;
591}
592
593
594
595
596int
597xfs_dir2_isblock(
598 struct xfs_da_args *args,
599 int *vp)
600{
601 xfs_fileoff_t last;
602 int rval;
603
604 if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
605 return rval;
606 rval = XFS_FSB_TO_B(args->dp->i_mount, last) == args->geo->blksize;
607 if (rval != 0 && args->dp->i_d.di_size != args->geo->blksize)
608 return -EFSCORRUPTED;
609 *vp = rval;
610 return 0;
611}
612
613
614
615
616int
617xfs_dir2_isleaf(
618 struct xfs_da_args *args,
619 int *vp)
620{
621 xfs_fileoff_t last;
622 int rval;
623
624 if ((rval = xfs_bmap_last_offset(args->dp, &last, XFS_DATA_FORK)))
625 return rval;
626 *vp = last == args->geo->leafblk + args->geo->fsbcount;
627 return 0;
628}
629
630
631
632
633
634
635int
636xfs_dir2_shrink_inode(
637 struct xfs_da_args *args,
638 xfs_dir2_db_t db,
639 struct xfs_buf *bp)
640{
641 xfs_fileoff_t bno;
642 xfs_dablk_t da;
643 int done;
644 struct xfs_inode *dp;
645 int error;
646 struct xfs_mount *mp;
647 struct xfs_trans *tp;
648
649 trace_xfs_dir2_shrink_inode(args, db);
650
651 dp = args->dp;
652 mp = dp->i_mount;
653 tp = args->trans;
654 da = xfs_dir2_db_to_da(args->geo, db);
655
656
657 error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
658 if (error) {
659
660
661
662
663
664
665
666
667
668
669
670
671 return error;
672 }
673 ASSERT(done);
674
675
676
677 xfs_trans_binval(tp, bp);
678
679
680
681 if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
682 return 0;
683
684
685
686 if (dp->i_d.di_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
687 return 0;
688 bno = da;
689 if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
690
691
692
693 return error;
694 }
695 if (db == args->geo->datablk)
696 ASSERT(bno == 0);
697 else
698 ASSERT(bno > 0);
699
700
701
702 dp->i_d.di_size = XFS_FSB_TO_B(mp, bno);
703 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
704 return 0;
705}
706