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
|
/*
* Copyright (c) 2012-2013 Paulo Alcantara <pcacjr@zytor.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <cache.h>
#include <core.h>
#include <fs.h>
#include "xfs_types.h"
#include "xfs_sb.h"
#include "xfs_ag.h"
#include "misc.h"
#include "xfs.h"
#include "xfs_dinode.h"
#include "xfs_dir2.h"
#include "xfs_readdir.h"
static int fill_dirent(struct fs_info *fs, struct dirent *dirent,
uint32_t offset, xfs_ino_t ino, char *name,
size_t namelen)
{
xfs_dinode_t *core;
xfs_debug("fs %p, dirent %p offset %lu ino %llu name %s namelen %llu", fs,
dirent, offset, ino, name, namelen);
dirent->d_ino = ino;
dirent->d_off = offset;
dirent->d_reclen = offsetof(struct dirent, d_name) + namelen + 1;
core = xfs_dinode_get_core(fs, ino);
if (!core) {
xfs_error("Failed to get dinode from disk (ino 0x%llx)", ino);
return -1;
}
if (be16_to_cpu(core->di_mode) & S_IFDIR)
dirent->d_type = DT_DIR;
else if (be16_to_cpu(core->di_mode) & S_IFREG)
dirent->d_type = DT_REG;
else if (be16_to_cpu(core->di_mode) & S_IFLNK)
dirent->d_type = DT_LNK;
memcpy(dirent->d_name, name, namelen);
dirent->d_name[namelen] = '\0';
return 0;
}
int xfs_readdir_dir2_local(struct file *file, struct dirent *dirent,
xfs_dinode_t *core)
{
xfs_dir2_sf_t *sf = (xfs_dir2_sf_t *)&core->di_literal_area[0];
xfs_dir2_sf_entry_t *sf_entry;
uint8_t count = sf->hdr.i8count ? sf->hdr.i8count : sf->hdr.count;
uint32_t offset = file->offset;
uint8_t *start_name;
uint8_t *end_name;
xfs_ino_t ino;
struct fs_info *fs = file->fs;
int retval = 0;
xfs_debug("file %p dirent %p core %p", file, dirent, core);
xfs_debug("count %hhu i8count %hhu", sf->hdr.count, sf->hdr.i8count);
if (file->offset + 1 > count)
goto out;
file->offset++;
sf_entry = (xfs_dir2_sf_entry_t *)((uint8_t *)&sf->list[0] -
(!sf->hdr.i8count ? 4 : 0));
if (file->offset - 1) {
offset = file->offset;
while (--offset) {
sf_entry = (xfs_dir2_sf_entry_t *)(
(uint8_t *)sf_entry +
offsetof(struct xfs_dir2_sf_entry,
name[0]) +
sf_entry->namelen +
(sf->hdr.i8count ? 8 : 4));
}
}
start_name = &sf_entry->name[0];
end_name = start_name + sf_entry->namelen;
ino = xfs_dir2_sf_get_inumber(sf, (xfs_dir2_inou_t *)(
(uint8_t *)sf_entry +
offsetof(struct xfs_dir2_sf_entry,
name[0]) +
sf_entry->namelen));
retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
end_name - start_name);
if (retval)
xfs_error("Failed to fill in dirent structure");
return retval;
out:
xfs_dir2_dirblks_flush_cache();
return -1;
}
int xfs_readdir_dir2_block(struct file *file, struct dirent *dirent,
xfs_dinode_t *core)
{
xfs_bmbt_irec_t r;
block_t dir_blk;
struct fs_info *fs = file->fs;
const uint8_t *dirblk_buf;
uint8_t *p;
uint32_t offset;
xfs_dir2_data_hdr_t *hdr;
xfs_dir2_block_tail_t *btp;
xfs_dir2_data_unused_t *dup;
xfs_dir2_data_entry_t *dep;
uint8_t *start_name;
uint8_t *end_name;
xfs_ino_t ino;
int retval = 0;
xfs_debug("file %p dirent %p core %p", file, dirent, core);
bmbt_irec_get(&r, (xfs_bmbt_rec_t *)&core->di_literal_area[0]);
dir_blk = fsblock_to_bytes(fs, r.br_startblock) >> BLOCK_SHIFT(fs);
dirblk_buf = xfs_dir2_dirblks_get_cached(fs, dir_blk, r.br_blockcount);
hdr = (xfs_dir2_data_hdr_t *)dirblk_buf;
if (be32_to_cpu(hdr->magic) != XFS_DIR2_BLOCK_MAGIC) {
xfs_error("Block directory header's magic number does not match!");
xfs_debug("hdr->magic: 0x%lx", be32_to_cpu(hdr->magic));
goto out;
}
btp = xfs_dir2_block_tail_p(XFS_INFO(fs), hdr);
if (file->offset + 1 > be32_to_cpu(btp->count))
goto out;
file->offset++;
p = (uint8_t *)(hdr + 1);
if (file->offset - 1) {
offset = file->offset;
while (--offset) {
dep = (xfs_dir2_data_entry_t *)p;
dup = (xfs_dir2_data_unused_t *)p;
if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) {
p += be16_to_cpu(dup->length);
continue;
}
p += xfs_dir2_data_entsize(dep->namelen);
}
}
dep = (xfs_dir2_data_entry_t *)p;
start_name = &dep->name[0];
end_name = start_name + dep->namelen;
ino = be64_to_cpu(dep->inumber);
retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
end_name - start_name);
if (retval)
xfs_error("Failed to fill in dirent structure");
return retval;
out:
xfs_dir2_dirblks_flush_cache();
return -1;
}
int xfs_readdir_dir2_leaf(struct file *file, struct dirent *dirent,
xfs_dinode_t *core)
{
xfs_bmbt_irec_t irec;
struct fs_info *fs = file->fs;
xfs_dir2_leaf_t *leaf;
block_t leaf_blk, dir_blk;
xfs_dir2_leaf_entry_t *lep;
uint32_t db;
unsigned int offset;
xfs_dir2_data_entry_t *dep;
xfs_dir2_data_hdr_t *data_hdr;
uint8_t *start_name;
uint8_t *end_name;
xfs_intino_t ino;
const uint8_t *buf = NULL;
int retval = 0;
xfs_debug("file %p dirent %p core %p", file, dirent, core);
bmbt_irec_get(&irec, ((xfs_bmbt_rec_t *)&core->di_literal_area[0]) +
be32_to_cpu(core->di_nextents) - 1);
leaf_blk = fsblock_to_bytes(fs, irec.br_startblock) >>
BLOCK_SHIFT(file->fs);
leaf = (xfs_dir2_leaf_t *)xfs_dir2_dirblks_get_cached(fs, leaf_blk,
irec.br_blockcount);
if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR2_LEAF1_MAGIC) {
xfs_error("Single leaf block header's magic number does not match!");
goto out;
}
if (!leaf->hdr.count)
goto out;
if (file->offset + 1 > be16_to_cpu(leaf->hdr.count))
goto out;
lep = &leaf->ents[file->offset++];
/* Skip over stale leaf entries */
for ( ; be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR;
lep++, file->offset++);
db = xfs_dir2_dataptr_to_db(fs, be32_to_cpu(lep->address));
bmbt_irec_get(&irec, (xfs_bmbt_rec_t *)&core->di_literal_area[0] + db);
dir_blk = fsblock_to_bytes(fs, irec.br_startblock) >> BLOCK_SHIFT(fs);
buf = xfs_dir2_dirblks_get_cached(fs, dir_blk, irec.br_blockcount);
data_hdr = (xfs_dir2_data_hdr_t *)buf;
if (be32_to_cpu(data_hdr->magic) != XFS_DIR2_DATA_MAGIC) {
xfs_error("Leaf directory's data magic number does not match!");
goto out;
}
offset = xfs_dir2_dataptr_to_off(fs, be32_to_cpu(lep->address));
dep = (xfs_dir2_data_entry_t *)((uint8_t *)buf + offset);
start_name = &dep->name[0];
end_name = start_name + dep->namelen;
ino = be64_to_cpu(dep->inumber);
retval = fill_dirent(fs, dirent, file->offset, ino, (char *)start_name,
end_name - start_name);
if (retval)
xfs_error("Failed to fill in dirent structure");
return retval;
out:
xfs_dir2_dirblks_flush_cache();
return -1;
}
int xfs_readdir_dir2_node(struct file *file, struct dirent *dirent,
xfs_dinode_t *core)
{
struct fs_info *fs = file->fs;
xfs_bmbt_irec_t irec;
uint32_t node_off = 0;
block_t fsblkno;
xfs_da_intnode_t *node = NULL;
struct inode *inode = file->inode;
int error;
xfs_dir2_data_hdr_t *data_hdr;
xfs_dir2_leaf_t *leaf;
xfs_dir2_leaf_entry_t *lep;
unsigned int offset;
xfs_dir2_data_entry_t *dep;
uint8_t *start_name;
uint8_t *end_name;
uint32_t db;
const uint8_t *buf = NULL;
int retval = 0;
xfs_debug("file %p dirent %p core %p", file, dirent, core);
do {
bmbt_irec_get(&irec, (xfs_bmbt_rec_t *)&core->di_literal_area[0] +
++node_off);
} while (irec.br_startoff < xfs_dir2_byte_to_db(fs, XFS_DIR2_LEAF_OFFSET));
fsblkno = fsblock_to_bytes(fs, irec.br_startblock) >> BLOCK_SHIFT(fs);
node = (xfs_da_intnode_t *)xfs_dir2_dirblks_get_cached(fs, fsblkno, 1);
if (be16_to_cpu(node->hdr.info.magic) != XFS_DA_NODE_MAGIC) {
xfs_error("Node's magic number does not match!");
goto out;
}
try_next_btree:
if (!node->hdr.count ||
XFS_PVT(inode)->i_btree_offset >= be16_to_cpu(node->hdr.count))
goto out;
fsblkno = be32_to_cpu(node->btree[XFS_PVT(inode)->i_btree_offset].before);
fsblkno = xfs_dir2_get_right_blk(fs, core, fsblkno, &error);
if (error) {
xfs_error("Cannot find leaf rec!");
goto out;
}
leaf = (xfs_dir2_leaf_t*)xfs_dir2_dirblks_get_cached(fs, fsblkno, 1);
if (be16_to_cpu(leaf->hdr.info.magic) != XFS_DIR2_LEAFN_MAGIC) {
xfs_error("Leaf's magic number does not match!");
goto out;
}
if (!leaf->hdr.count ||
XFS_PVT(inode)->i_leaf_ent_offset >= be16_to_cpu(leaf->hdr.count)) {
XFS_PVT(inode)->i_btree_offset++;
XFS_PVT(inode)->i_leaf_ent_offset = 0;
goto try_next_btree;
}
lep = &leaf->ents[XFS_PVT(inode)->i_leaf_ent_offset];
/* Skip over stale leaf entries */
for ( ; XFS_PVT(inode)->i_leaf_ent_offset < be16_to_cpu(leaf->hdr.count) &&
be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR;
lep++, XFS_PVT(inode)->i_leaf_ent_offset++);
if (XFS_PVT(inode)->i_leaf_ent_offset == be16_to_cpu(leaf->hdr.count)) {
XFS_PVT(inode)->i_btree_offset++;
XFS_PVT(inode)->i_leaf_ent_offset = 0;
goto try_next_btree;
} else {
XFS_PVT(inode)->i_leaf_ent_offset++;
}
db = xfs_dir2_dataptr_to_db(fs, be32_to_cpu(lep->address));
fsblkno = xfs_dir2_get_right_blk(fs, core, db, &error);
if (error) {
xfs_error("Cannot find data block!");
goto out;
}
buf = xfs_dir2_dirblks_get_cached(fs, fsblkno, 1);
data_hdr = (xfs_dir2_data_hdr_t *)buf;
if (be32_to_cpu(data_hdr->magic) != XFS_DIR2_DATA_MAGIC) {
xfs_error("Leaf directory's data magic No. does not match!");
goto out;
}
offset = xfs_dir2_dataptr_to_off(fs, be32_to_cpu(lep->address));
dep = (xfs_dir2_data_entry_t *)((uint8_t *)buf + offset);
start_name = &dep->name[0];
end_name = start_name + dep->namelen;
retval = fill_dirent(fs, dirent, 0, be64_to_cpu(dep->inumber),
(char *)start_name, end_name - start_name);
if (retval)
xfs_error("Failed to fill in dirent structure");
return retval;
out:
xfs_dir2_dirblks_flush_cache();
XFS_PVT(inode)->i_btree_offset = 0;
XFS_PVT(inode)->i_leaf_ent_offset = 0;
return -1;
}
|