| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 4 |
* Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* Implements Extendible Hashing as described in: |
| 9 |
* "Extendible Hashing" by Fagin, et al in |
| 10 |
* __ACM Trans. on Database Systems__, Sept 1979. |
| 11 |
* |
| 12 |
* |
| 13 |
* Here's the layout of dirents which is essentially the same as that of ext2 |
| 14 |
* within a single block. The field de_name_len is the number of bytes |
| 15 |
* actually required for the name (no null terminator). The field de_rec_len |
| 16 |
* is the number of bytes allocated to the dirent. The offset of the next |
| 17 |
* dirent in the block is (dirent + dirent->de_rec_len). When a dirent is |
| 18 |
* deleted, the preceding dirent inherits its allocated space, ie |
| 19 |
* prev->de_rec_len += deleted->de_rec_len. Since the next dirent is obtained |
| 20 |
* by adding de_rec_len to the current dirent, this essentially causes the |
| 21 |
* deleted dirent to get jumped over when iterating through all the dirents. |
| 22 |
* |
| 23 |
* When deleting the first dirent in a block, there is no previous dirent so |
| 24 |
* the field de_ino is set to zero to designate it as deleted. When allocating |
| 25 |
* a dirent, gfs2_dirent_alloc iterates through the dirents in a block. If the |
| 26 |
* first dirent has (de_ino == 0) and de_rec_len is large enough, this first |
| 27 |
* dirent is allocated. Otherwise it must go through all the 'used' dirents |
| 28 |
* searching for one in which the amount of total space minus the amount of |
| 29 |
* used space will provide enough space for the new dirent. |
| 30 |
* |
| 31 |
* There are two types of blocks in which dirents reside. In a stuffed dinode, |
| 32 |
* the dirents begin at offset sizeof(struct gfs2_dinode) from the beginning of |
| 33 |
* the block. In leaves, they begin at offset sizeof(struct gfs2_leaf) from the |
| 34 |
* beginning of the leaf block. The dirents reside in leaves when |
| 35 |
* |
| 36 |
* dip->i_diskflags & GFS2_DIF_EXHASH is true |
| 37 |
* |
| 38 |
* Otherwise, the dirents are "linear", within a single stuffed dinode block. |
| 39 |
* |
| 40 |
* When the dirents are in leaves, the actual contents of the directory file are |
| 41 |
* used as an array of 64-bit block pointers pointing to the leaf blocks. The |
| 42 |
* dirents are NOT in the directory file itself. There can be more than one |
| 43 |
* block pointer in the array that points to the same leaf. In fact, when a |
| 44 |
* directory is first converted from linear to exhash, all of the pointers |
| 45 |
* point to the same leaf. |
| 46 |
* |
| 47 |
* When a leaf is completely full, the size of the hash table can be |
| 48 |
* doubled unless it is already at the maximum size which is hard coded into |
| 49 |
* GFS2_DIR_MAX_DEPTH. After that, leaves are chained together in a linked list, |
| 50 |
* but never before the maximum hash table size has been reached. |
| 51 |
*/ |
| 52 |
|
| 53 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 54 |
|
| 55 |
#include <linux/slab.h> |
| 56 |
#include <linux/spinlock.h> |
| 57 |
#include <linux/buffer_head.h> |
| 58 |
#include <linux/sort.h> |
| 59 |
#include <linux/gfs2_ondisk.h> |
| 60 |
#include <linux/crc32.h> |
| 61 |
#include <linux/vmalloc.h> |
| 62 |
#include <linux/bio.h> |
| 63 |
#include <linux/log2.h> |
| 64 |
|
| 65 |
#include "gfs2.h" |
| 66 |
#include "incore.h" |
| 67 |
#include "dir.h" |
| 68 |
#include "glock.h" |
| 69 |
#include "inode.h" |
| 70 |
#include "meta_io.h" |
| 71 |
#include "quota.h" |
| 72 |
#include "rgrp.h" |
| 73 |
#include "trans.h" |
| 74 |
#include "bmap.h" |
| 75 |
#include "util.h" |
| 76 |
|
| 77 |
#define MAX_RA_BLOCKS 32 /* max read-ahead blocks */ |
| 78 |
|
| 79 |
#define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1) |
| 80 |
#define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1)) |
| 81 |
#define GFS2_HASH_INDEX_MASK 0xffffc000 |
| 82 |
#define GFS2_USE_HASH_FLAG 0x2000 |
| 83 |
|
| 84 |
struct qstr gfs2_qdot __read_mostly; |
| 85 |
struct qstr gfs2_qdotdot __read_mostly; |
| 86 |
|
| 87 |
typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent, |
| 88 |
const struct qstr *name, void *opaque); |
| 89 |
|
| 90 |
int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, |
| 91 |
struct buffer_head **bhp) |
| 92 |
{ |
| 93 |
struct buffer_head *bh; |
| 94 |
|
| 95 |
bh = gfs2_meta_new(ip->i_gl, block); |
| 96 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 97 |
gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD); |
| 98 |
gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
| 99 |
*bhp = bh; |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
static int gfs2_dir_get_existing_buffer(struct gfs2_inode *ip, u64 block, |
| 104 |
struct buffer_head **bhp) |
| 105 |
{ |
| 106 |
struct buffer_head *bh; |
| 107 |
int error; |
| 108 |
|
| 109 |
error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, 0, &bh); |
| 110 |
if (error) |
| 111 |
return error; |
| 112 |
if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_JD)) { |
| 113 |
brelse(bh); |
| 114 |
return -EIO; |
| 115 |
} |
| 116 |
*bhp = bh; |
| 117 |
return 0; |
| 118 |
} |
| 119 |
|
| 120 |
static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf, |
| 121 |
unsigned int offset, unsigned int size) |
| 122 |
{ |
| 123 |
struct buffer_head *dibh; |
| 124 |
int error; |
| 125 |
|
| 126 |
error = gfs2_meta_inode_buffer(ip, &dibh); |
| 127 |
if (error) |
| 128 |
return error; |
| 129 |
|
| 130 |
gfs2_trans_add_meta(ip->i_gl, dibh); |
| 131 |
memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); |
| 132 |
if (ip->i_inode.i_size < offset + size) |
| 133 |
i_size_write(&ip->i_inode, offset + size); |
| 134 |
inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); |
| 135 |
gfs2_dinode_out(ip, dibh->b_data); |
| 136 |
|
| 137 |
brelse(dibh); |
| 138 |
|
| 139 |
return size; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* gfs2_dir_write_data - Write directory information to the inode |
| 146 |
* @ip: The GFS2 inode |
| 147 |
* @buf: The buffer containing information to be written |
| 148 |
* @offset: The file offset to start writing at |
| 149 |
* @size: The amount of data to write |
| 150 |
* |
| 151 |
* Returns: The number of bytes correctly written or error code |
| 152 |
*/ |
| 153 |
static int gfs2_dir_write_data(struct gfs2_inode *ip, const char *buf, |
| 154 |
u64 offset, unsigned int size) |
| 155 |
{ |
| 156 |
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 157 |
struct buffer_head *dibh; |
| 158 |
u64 lblock, dblock; |
| 159 |
u32 extlen = 0; |
| 160 |
unsigned int o; |
| 161 |
int copied = 0; |
| 162 |
int error = 0; |
| 163 |
bool new = false; |
| 164 |
|
| 165 |
if (!size) |
| 166 |
return 0; |
| 167 |
|
| 168 |
if (gfs2_is_stuffed(ip) && offset + size <= gfs2_max_stuffed_size(ip)) |
| 169 |
return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, |
| 170 |
size); |
| 171 |
|
| 172 |
if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) |
| 173 |
return -EINVAL; |
| 174 |
|
| 175 |
if (gfs2_is_stuffed(ip)) { |
| 176 |
error = gfs2_unstuff_dinode(ip); |
| 177 |
if (error) |
| 178 |
return error; |
| 179 |
} |
| 180 |
|
| 181 |
lblock = offset; |
| 182 |
o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); |
| 183 |
|
| 184 |
while (copied < size) { |
| 185 |
unsigned int amount; |
| 186 |
struct buffer_head *bh; |
| 187 |
|
| 188 |
amount = size - copied; |
| 189 |
if (amount > sdp->sd_sb.sb_bsize - o) |
| 190 |
amount = sdp->sd_sb.sb_bsize - o; |
| 191 |
|
| 192 |
if (!extlen) { |
| 193 |
extlen = 1; |
| 194 |
error = gfs2_alloc_extent(&ip->i_inode, lblock, &dblock, |
| 195 |
&extlen, &new); |
| 196 |
if (error) |
| 197 |
goto fail; |
| 198 |
error = -EIO; |
| 199 |
if (gfs2_assert_withdraw(sdp, dblock)) |
| 200 |
goto fail; |
| 201 |
} |
| 202 |
|
| 203 |
if (amount == sdp->sd_jbsize || new) |
| 204 |
error = gfs2_dir_get_new_buffer(ip, dblock, &bh); |
| 205 |
else |
| 206 |
error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); |
| 207 |
|
| 208 |
if (error) |
| 209 |
goto fail; |
| 210 |
|
| 211 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 212 |
memcpy(bh->b_data + o, buf, amount); |
| 213 |
brelse(bh); |
| 214 |
|
| 215 |
buf += amount; |
| 216 |
copied += amount; |
| 217 |
lblock++; |
| 218 |
dblock++; |
| 219 |
extlen--; |
| 220 |
|
| 221 |
o = sizeof(struct gfs2_meta_header); |
| 222 |
} |
| 223 |
|
| 224 |
out: |
| 225 |
error = gfs2_meta_inode_buffer(ip, &dibh); |
| 226 |
if (error) |
| 227 |
return error; |
| 228 |
|
| 229 |
if (ip->i_inode.i_size < offset + copied) |
| 230 |
i_size_write(&ip->i_inode, offset + copied); |
| 231 |
inode_set_mtime_to_ts(&ip->i_inode, inode_set_ctime_current(&ip->i_inode)); |
| 232 |
|
| 233 |
gfs2_trans_add_meta(ip->i_gl, dibh); |
| 234 |
gfs2_dinode_out(ip, dibh->b_data); |
| 235 |
brelse(dibh); |
| 236 |
|
| 237 |
return copied; |
| 238 |
fail: |
| 239 |
if (copied) |
| 240 |
goto out; |
| 241 |
return error; |
| 242 |
} |
| 243 |
|
| 244 |
static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, __be64 *buf, |
| 245 |
unsigned int size) |
| 246 |
{ |
| 247 |
struct buffer_head *dibh; |
| 248 |
int error; |
| 249 |
|
| 250 |
error = gfs2_meta_inode_buffer(ip, &dibh); |
| 251 |
if (!error) { |
| 252 |
memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size); |
| 253 |
brelse(dibh); |
| 254 |
} |
| 255 |
|
| 256 |
return (error) ? error : size; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* gfs2_dir_read_data - Read a data from a directory inode |
| 262 |
* @ip: The GFS2 Inode |
| 263 |
* @buf: The buffer to place result into |
| 264 |
* @size: Amount of data to transfer |
| 265 |
* |
| 266 |
* Returns: The amount of data actually copied or the error |
| 267 |
*/ |
| 268 |
static int gfs2_dir_read_data(struct gfs2_inode *ip, __be64 *buf, |
| 269 |
unsigned int size) |
| 270 |
{ |
| 271 |
struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
| 272 |
u64 lblock, dblock; |
| 273 |
u32 extlen = 0; |
| 274 |
unsigned int o; |
| 275 |
int copied = 0; |
| 276 |
int error = 0; |
| 277 |
|
| 278 |
if (gfs2_is_stuffed(ip)) |
| 279 |
return gfs2_dir_read_stuffed(ip, buf, size); |
| 280 |
|
| 281 |
if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) |
| 282 |
return -EINVAL; |
| 283 |
|
| 284 |
lblock = 0; |
| 285 |
o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); |
| 286 |
|
| 287 |
while (copied < size) { |
| 288 |
unsigned int amount; |
| 289 |
struct buffer_head *bh; |
| 290 |
|
| 291 |
amount = size - copied; |
| 292 |
if (amount > sdp->sd_sb.sb_bsize - o) |
| 293 |
amount = sdp->sd_sb.sb_bsize - o; |
| 294 |
|
| 295 |
if (!extlen) { |
| 296 |
extlen = 32; |
| 297 |
error = gfs2_get_extent(&ip->i_inode, lblock, |
| 298 |
&dblock, &extlen); |
| 299 |
if (error || !dblock) |
| 300 |
goto fail; |
| 301 |
BUG_ON(extlen < 1); |
| 302 |
bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
| 303 |
} else { |
| 304 |
error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, 0, &bh); |
| 305 |
if (error) |
| 306 |
goto fail; |
| 307 |
} |
| 308 |
error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD); |
| 309 |
if (error) { |
| 310 |
brelse(bh); |
| 311 |
goto fail; |
| 312 |
} |
| 313 |
dblock++; |
| 314 |
extlen--; |
| 315 |
memcpy(buf, bh->b_data + o, amount); |
| 316 |
brelse(bh); |
| 317 |
buf += (amount/sizeof(__be64)); |
| 318 |
copied += amount; |
| 319 |
lblock++; |
| 320 |
o = sizeof(struct gfs2_meta_header); |
| 321 |
} |
| 322 |
|
| 323 |
return copied; |
| 324 |
fail: |
| 325 |
return (copied) ? copied : error; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* gfs2_dir_get_hash_table - Get pointer to the dir hash table |
| 330 |
* @ip: The inode in question |
| 331 |
* |
| 332 |
* Returns: The hash table or an error |
| 333 |
*/ |
| 334 |
|
| 335 |
static __be64 *gfs2_dir_get_hash_table(struct gfs2_inode *ip) |
| 336 |
{ |
| 337 |
struct inode *inode = &ip->i_inode; |
| 338 |
int ret; |
| 339 |
u32 hsize; |
| 340 |
__be64 *hc; |
| 341 |
|
| 342 |
BUG_ON(!(ip->i_diskflags & GFS2_DIF_EXHASH)); |
| 343 |
|
| 344 |
hc = ip->i_hash_cache; |
| 345 |
if (hc) |
| 346 |
return hc; |
| 347 |
|
| 348 |
hsize = BIT(ip->i_depth); |
| 349 |
hsize *= sizeof(__be64); |
| 350 |
if (hsize != i_size_read(&ip->i_inode)) { |
| 351 |
gfs2_consist_inode(ip); |
| 352 |
return ERR_PTR(-EIO); |
| 353 |
} |
| 354 |
|
| 355 |
hc = kmalloc(hsize, GFP_NOFS | __GFP_NOWARN); |
| 356 |
if (hc == NULL) |
| 357 |
hc = __vmalloc(hsize, GFP_NOFS); |
| 358 |
|
| 359 |
if (hc == NULL) |
| 360 |
return ERR_PTR(-ENOMEM); |
| 361 |
|
| 362 |
ret = gfs2_dir_read_data(ip, hc, hsize); |
| 363 |
if (ret < 0) { |
| 364 |
kvfree(hc); |
| 365 |
return ERR_PTR(ret); |
| 366 |
} |
| 367 |
|
| 368 |
spin_lock(&inode->i_lock); |
| 369 |
if (likely(!ip->i_hash_cache)) { |
| 370 |
ip->i_hash_cache = hc; |
| 371 |
hc = NULL; |
| 372 |
} |
| 373 |
spin_unlock(&inode->i_lock); |
| 374 |
kvfree(hc); |
| 375 |
|
| 376 |
return ip->i_hash_cache; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* gfs2_dir_hash_inval - Invalidate dir hash |
| 381 |
* @ip: The directory inode |
| 382 |
* |
| 383 |
* Must be called with an exclusive glock, or during glock invalidation. |
| 384 |
*/ |
| 385 |
void gfs2_dir_hash_inval(struct gfs2_inode *ip) |
| 386 |
{ |
| 387 |
__be64 *hc; |
| 388 |
|
| 389 |
spin_lock(&ip->i_inode.i_lock); |
| 390 |
hc = ip->i_hash_cache; |
| 391 |
ip->i_hash_cache = NULL; |
| 392 |
spin_unlock(&ip->i_inode.i_lock); |
| 393 |
|
| 394 |
kvfree(hc); |
| 395 |
} |
| 396 |
|
| 397 |
static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent) |
| 398 |
{ |
| 399 |
return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0; |
| 400 |
} |
| 401 |
|
| 402 |
static inline int __gfs2_dirent_find(const struct gfs2_dirent *dent, |
| 403 |
const struct qstr *name, int ret) |
| 404 |
{ |
| 405 |
if (!gfs2_dirent_sentinel(dent) && |
| 406 |
be32_to_cpu(dent->de_hash) == name->hash && |
| 407 |
be16_to_cpu(dent->de_name_len) == name->len && |
| 408 |
memcmp(dent+1, name->name, name->len) == 0) |
| 409 |
return ret; |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
static int gfs2_dirent_find(const struct gfs2_dirent *dent, |
| 414 |
const struct qstr *name, |
| 415 |
void *opaque) |
| 416 |
{ |
| 417 |
return __gfs2_dirent_find(dent, name, 1); |
| 418 |
} |
| 419 |
|
| 420 |
static int gfs2_dirent_prev(const struct gfs2_dirent *dent, |
| 421 |
const struct qstr *name, |
| 422 |
void *opaque) |
| 423 |
{ |
| 424 |
return __gfs2_dirent_find(dent, name, 2); |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
* name->name holds ptr to start of block. |
| 429 |
* name->len holds size of block. |
| 430 |
*/ |
| 431 |
static int gfs2_dirent_last(const struct gfs2_dirent *dent, |
| 432 |
const struct qstr *name, |
| 433 |
void *opaque) |
| 434 |
{ |
| 435 |
const char *start = name->name; |
| 436 |
const char *end = (const char *)dent + be16_to_cpu(dent->de_rec_len); |
| 437 |
if (name->len == (end - start)) |
| 438 |
return 1; |
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
/* Look for the dirent that contains the offset specified in data. Once we |
| 443 |
* find that dirent, there must be space available there for the new dirent */ |
| 444 |
static int gfs2_dirent_find_offset(const struct gfs2_dirent *dent, |
| 445 |
const struct qstr *name, |
| 446 |
void *ptr) |
| 447 |
{ |
| 448 |
unsigned required = GFS2_DIRENT_SIZE(name->len); |
| 449 |
unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); |
| 450 |
unsigned totlen = be16_to_cpu(dent->de_rec_len); |
| 451 |
|
| 452 |
if (ptr < (void *)dent || ptr >= (void *)dent + totlen) |
| 453 |
return 0; |
| 454 |
if (gfs2_dirent_sentinel(dent)) |
| 455 |
actual = 0; |
| 456 |
if (ptr < (void *)dent + actual) |
| 457 |
return -1; |
| 458 |
if ((void *)dent + totlen >= ptr + required) |
| 459 |
return 1; |
| 460 |
return -1; |
| 461 |
} |
| 462 |
|
| 463 |
static int gfs2_dirent_find_space(const struct gfs2_dirent *dent, |
| 464 |
const struct qstr *name, |
| 465 |
void *opaque) |
| 466 |
{ |
| 467 |
unsigned required = GFS2_DIRENT_SIZE(name->len); |
| 468 |
unsigned actual = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); |
| 469 |
unsigned totlen = be16_to_cpu(dent->de_rec_len); |
| 470 |
|
| 471 |
if (gfs2_dirent_sentinel(dent)) |
| 472 |
actual = 0; |
| 473 |
if (totlen - actual >= required) |
| 474 |
return 1; |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
struct dirent_gather { |
| 479 |
const struct gfs2_dirent **pdent; |
| 480 |
unsigned offset; |
| 481 |
}; |
| 482 |
|
| 483 |
static int gfs2_dirent_gather(const struct gfs2_dirent *dent, |
| 484 |
const struct qstr *name, |
| 485 |
void *opaque) |
| 486 |
{ |
| 487 |
struct dirent_gather *g = opaque; |
| 488 |
if (!gfs2_dirent_sentinel(dent)) { |
| 489 |
g->pdent[g->offset++] = dent; |
| 490 |
} |
| 491 |
return 0; |
| 492 |
} |
| 493 |
|
| 494 |
/* |
| 495 |
* Other possible things to check: |
| 496 |
* - Inode located within filesystem size (and on valid block) |
| 497 |
* - Valid directory entry type |
| 498 |
* Not sure how heavy-weight we want to make this... could also check |
| 499 |
* hash is correct for example, but that would take a lot of extra time. |
| 500 |
* For now the most important thing is to check that the various sizes |
| 501 |
* are correct. |
| 502 |
*/ |
| 503 |
static int gfs2_check_dirent(struct gfs2_sbd *sdp, |
| 504 |
struct gfs2_dirent *dent, unsigned int offset, |
| 505 |
unsigned int size, unsigned int len, int first) |
| 506 |
{ |
| 507 |
const char *msg = "gfs2_dirent too small"; |
| 508 |
if (unlikely(size < sizeof(struct gfs2_dirent))) |
| 509 |
goto error; |
| 510 |
msg = "gfs2_dirent misaligned"; |
| 511 |
if (unlikely(offset & 0x7)) |
| 512 |
goto error; |
| 513 |
msg = "gfs2_dirent points beyond end of block"; |
| 514 |
if (unlikely(offset + size > len)) |
| 515 |
goto error; |
| 516 |
msg = "zero inode number"; |
| 517 |
if (unlikely(!first && gfs2_dirent_sentinel(dent))) |
| 518 |
goto error; |
| 519 |
msg = "name length is greater than space in dirent"; |
| 520 |
if (!gfs2_dirent_sentinel(dent) && |
| 521 |
unlikely(sizeof(struct gfs2_dirent)+be16_to_cpu(dent->de_name_len) > |
| 522 |
size)) |
| 523 |
goto error; |
| 524 |
return 0; |
| 525 |
error: |
| 526 |
fs_warn(sdp, "%s: %s (%s)\n", |
| 527 |
__func__, msg, first ? "first in block" : "not first in block"); |
| 528 |
return -EIO; |
| 529 |
} |
| 530 |
|
| 531 |
static int gfs2_dirent_offset(struct gfs2_sbd *sdp, const void *buf) |
| 532 |
{ |
| 533 |
const struct gfs2_meta_header *h = buf; |
| 534 |
int offset; |
| 535 |
|
| 536 |
BUG_ON(buf == NULL); |
| 537 |
|
| 538 |
switch(be32_to_cpu(h->mh_type)) { |
| 539 |
case GFS2_METATYPE_LF: |
| 540 |
offset = sizeof(struct gfs2_leaf); |
| 541 |
break; |
| 542 |
case GFS2_METATYPE_DI: |
| 543 |
offset = sizeof(struct gfs2_dinode); |
| 544 |
break; |
| 545 |
default: |
| 546 |
goto wrong_type; |
| 547 |
} |
| 548 |
return offset; |
| 549 |
wrong_type: |
| 550 |
fs_warn(sdp, "%s: wrong block type %u\n", __func__, |
| 551 |
be32_to_cpu(h->mh_type)); |
| 552 |
return -1; |
| 553 |
} |
| 554 |
|
| 555 |
static struct gfs2_dirent *gfs2_dirent_scan(struct inode *inode, void *buf, |
| 556 |
unsigned int len, gfs2_dscan_t scan, |
| 557 |
const struct qstr *name, |
| 558 |
void *opaque) |
| 559 |
{ |
| 560 |
struct gfs2_dirent *dent, *prev; |
| 561 |
unsigned offset; |
| 562 |
unsigned size; |
| 563 |
int ret = 0; |
| 564 |
|
| 565 |
ret = gfs2_dirent_offset(GFS2_SB(inode), buf); |
| 566 |
if (ret < 0) { |
| 567 |
gfs2_consist_inode(GFS2_I(inode)); |
| 568 |
return ERR_PTR(-EIO); |
| 569 |
} |
| 570 |
offset = ret; |
| 571 |
prev = NULL; |
| 572 |
dent = buf + offset; |
| 573 |
size = be16_to_cpu(dent->de_rec_len); |
| 574 |
if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size, len, 1)) { |
| 575 |
gfs2_consist_inode(GFS2_I(inode)); |
| 576 |
return ERR_PTR(-EIO); |
| 577 |
} |
| 578 |
do { |
| 579 |
ret = scan(dent, name, opaque); |
| 580 |
if (ret) |
| 581 |
break; |
| 582 |
offset += size; |
| 583 |
if (offset == len) |
| 584 |
break; |
| 585 |
prev = dent; |
| 586 |
dent = buf + offset; |
| 587 |
size = be16_to_cpu(dent->de_rec_len); |
| 588 |
if (gfs2_check_dirent(GFS2_SB(inode), dent, offset, size, |
| 589 |
len, 0)) { |
| 590 |
gfs2_consist_inode(GFS2_I(inode)); |
| 591 |
return ERR_PTR(-EIO); |
| 592 |
} |
| 593 |
} while(1); |
| 594 |
|
| 595 |
switch(ret) { |
| 596 |
case 0: |
| 597 |
return NULL; |
| 598 |
case 1: |
| 599 |
return dent; |
| 600 |
case 2: |
| 601 |
return prev ? prev : dent; |
| 602 |
default: |
| 603 |
BUG_ON(ret > 0); |
| 604 |
return ERR_PTR(ret); |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
static int dirent_check_reclen(struct gfs2_inode *dip, |
| 609 |
const struct gfs2_dirent *d, const void *end_p) |
| 610 |
{ |
| 611 |
const void *ptr = d; |
| 612 |
u16 rec_len = be16_to_cpu(d->de_rec_len); |
| 613 |
|
| 614 |
if (unlikely(rec_len < sizeof(struct gfs2_dirent))) { |
| 615 |
gfs2_consist_inode(dip); |
| 616 |
return -EIO; |
| 617 |
} |
| 618 |
ptr += rec_len; |
| 619 |
if (ptr < end_p) |
| 620 |
return rec_len; |
| 621 |
if (ptr == end_p) |
| 622 |
return -ENOENT; |
| 623 |
|
| 624 |
gfs2_consist_inode(dip); |
| 625 |
return -EIO; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* dirent_next - Next dirent |
| 630 |
* @dip: the directory |
| 631 |
* @bh: The buffer |
| 632 |
* @dent: Pointer to list of dirents |
| 633 |
* |
| 634 |
* Returns: 0 on success, error code otherwise |
| 635 |
*/ |
| 636 |
|
| 637 |
static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, |
| 638 |
struct gfs2_dirent **dent) |
| 639 |
{ |
| 640 |
struct gfs2_dirent *cur = *dent, *tmp; |
| 641 |
char *bh_end = bh->b_data + bh->b_size; |
| 642 |
int ret; |
| 643 |
|
| 644 |
ret = dirent_check_reclen(dip, cur, bh_end); |
| 645 |
if (ret < 0) |
| 646 |
return ret; |
| 647 |
|
| 648 |
tmp = (void *)cur + ret; |
| 649 |
ret = dirent_check_reclen(dip, tmp, bh_end); |
| 650 |
if (ret == -EIO) |
| 651 |
return ret; |
| 652 |
|
| 653 |
/* Only the first dent could ever have de_inum.no_addr == 0 */ |
| 654 |
if (gfs2_dirent_sentinel(tmp)) { |
| 655 |
gfs2_consist_inode(dip); |
| 656 |
return -EIO; |
| 657 |
} |
| 658 |
|
| 659 |
*dent = tmp; |
| 660 |
return 0; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* dirent_del - Delete a dirent |
| 665 |
* @dip: The GFS2 inode |
| 666 |
* @bh: The buffer |
| 667 |
* @prev: The previous dirent |
| 668 |
* @cur: The current dirent |
| 669 |
* |
| 670 |
*/ |
| 671 |
|
| 672 |
static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh, |
| 673 |
struct gfs2_dirent *prev, struct gfs2_dirent *cur) |
| 674 |
{ |
| 675 |
u16 cur_rec_len, prev_rec_len; |
| 676 |
|
| 677 |
if (gfs2_dirent_sentinel(cur)) { |
| 678 |
gfs2_consist_inode(dip); |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
gfs2_trans_add_meta(dip->i_gl, bh); |
| 683 |
|
| 684 |
/* If there is no prev entry, this is the first entry in the block. |
| 685 |
The de_rec_len is already as big as it needs to be. Just zero |
| 686 |
out the inode number and return. */ |
| 687 |
|
| 688 |
if (!prev) { |
| 689 |
cur->de_inum.no_addr = 0; |
| 690 |
cur->de_inum.no_formal_ino = 0; |
| 691 |
return; |
| 692 |
} |
| 693 |
|
| 694 |
/* Combine this dentry with the previous one. */ |
| 695 |
|
| 696 |
prev_rec_len = be16_to_cpu(prev->de_rec_len); |
| 697 |
cur_rec_len = be16_to_cpu(cur->de_rec_len); |
| 698 |
|
| 699 |
if ((char *)prev + prev_rec_len != (char *)cur) |
| 700 |
gfs2_consist_inode(dip); |
| 701 |
if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size) |
| 702 |
gfs2_consist_inode(dip); |
| 703 |
|
| 704 |
prev_rec_len += cur_rec_len; |
| 705 |
prev->de_rec_len = cpu_to_be16(prev_rec_len); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
static struct gfs2_dirent *do_init_dirent(struct inode *inode, |
| 710 |
struct gfs2_dirent *dent, |
| 711 |
const struct qstr *name, |
| 712 |
struct buffer_head *bh, |
| 713 |
unsigned offset) |
| 714 |
{ |
| 715 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 716 |
struct gfs2_dirent *ndent; |
| 717 |
unsigned totlen; |
| 718 |
|
| 719 |
totlen = be16_to_cpu(dent->de_rec_len); |
| 720 |
BUG_ON(offset + name->len > totlen); |
| 721 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 722 |
ndent = (struct gfs2_dirent *)((char *)dent + offset); |
| 723 |
dent->de_rec_len = cpu_to_be16(offset); |
| 724 |
gfs2_qstr2dirent(name, totlen - offset, ndent); |
| 725 |
return ndent; |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
/* |
| 730 |
* Takes a dent from which to grab space as an argument. Returns the |
| 731 |
* newly created dent. |
| 732 |
*/ |
| 733 |
static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode, |
| 734 |
struct gfs2_dirent *dent, |
| 735 |
const struct qstr *name, |
| 736 |
struct buffer_head *bh) |
| 737 |
{ |
| 738 |
unsigned offset = 0; |
| 739 |
|
| 740 |
if (!gfs2_dirent_sentinel(dent)) |
| 741 |
offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); |
| 742 |
return do_init_dirent(inode, dent, name, bh, offset); |
| 743 |
} |
| 744 |
|
| 745 |
static struct gfs2_dirent *gfs2_dirent_split_alloc(struct inode *inode, |
| 746 |
struct buffer_head *bh, |
| 747 |
const struct qstr *name, |
| 748 |
void *ptr) |
| 749 |
{ |
| 750 |
struct gfs2_dirent *dent; |
| 751 |
dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
| 752 |
gfs2_dirent_find_offset, name, ptr); |
| 753 |
if (IS_ERR_OR_NULL(dent)) |
| 754 |
return dent; |
| 755 |
return do_init_dirent(inode, dent, name, bh, |
| 756 |
(unsigned)(ptr - (void *)dent)); |
| 757 |
} |
| 758 |
|
| 759 |
static int get_leaf(struct gfs2_inode *dip, u64 leaf_no, |
| 760 |
struct buffer_head **bhp) |
| 761 |
{ |
| 762 |
int error; |
| 763 |
|
| 764 |
error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, 0, bhp); |
| 765 |
if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) { |
| 766 |
/* pr_info("block num=%llu\n", leaf_no); */ |
| 767 |
error = -EIO; |
| 768 |
} |
| 769 |
|
| 770 |
return error; |
| 771 |
} |
| 772 |
|
| 773 |
/** |
| 774 |
* get_leaf_nr - Get a leaf number associated with the index |
| 775 |
* @dip: The GFS2 inode |
| 776 |
* @index: hash table index of the targeted leaf |
| 777 |
* @leaf_out: Resulting leaf block number |
| 778 |
* |
| 779 |
* Returns: 0 on success, error code otherwise |
| 780 |
*/ |
| 781 |
|
| 782 |
static int get_leaf_nr(struct gfs2_inode *dip, u32 index, u64 *leaf_out) |
| 783 |
{ |
| 784 |
__be64 *hash; |
| 785 |
int error; |
| 786 |
|
| 787 |
hash = gfs2_dir_get_hash_table(dip); |
| 788 |
error = PTR_ERR_OR_ZERO(hash); |
| 789 |
|
| 790 |
if (!error) |
| 791 |
*leaf_out = be64_to_cpu(*(hash + index)); |
| 792 |
|
| 793 |
return error; |
| 794 |
} |
| 795 |
|
| 796 |
static int get_first_leaf(struct gfs2_inode *dip, u32 index, |
| 797 |
struct buffer_head **bh_out) |
| 798 |
{ |
| 799 |
u64 leaf_no; |
| 800 |
int error; |
| 801 |
|
| 802 |
error = get_leaf_nr(dip, index, &leaf_no); |
| 803 |
if (!error) |
| 804 |
error = get_leaf(dip, leaf_no, bh_out); |
| 805 |
|
| 806 |
return error; |
| 807 |
} |
| 808 |
|
| 809 |
static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode, |
| 810 |
const struct qstr *name, |
| 811 |
gfs2_dscan_t scan, |
| 812 |
struct buffer_head **pbh) |
| 813 |
{ |
| 814 |
struct buffer_head *bh; |
| 815 |
struct gfs2_dirent *dent; |
| 816 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 817 |
int error; |
| 818 |
|
| 819 |
if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
| 820 |
struct gfs2_leaf *leaf; |
| 821 |
unsigned int hsize = BIT(ip->i_depth); |
| 822 |
unsigned int index; |
| 823 |
u64 ln; |
| 824 |
if (hsize * sizeof(u64) != i_size_read(inode)) { |
| 825 |
gfs2_consist_inode(ip); |
| 826 |
return ERR_PTR(-EIO); |
| 827 |
} |
| 828 |
|
| 829 |
index = name->hash >> (32 - ip->i_depth); |
| 830 |
error = get_first_leaf(ip, index, &bh); |
| 831 |
if (error) |
| 832 |
return ERR_PTR(error); |
| 833 |
do { |
| 834 |
dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
| 835 |
scan, name, NULL); |
| 836 |
if (dent) |
| 837 |
goto got_dent; |
| 838 |
leaf = (struct gfs2_leaf *)bh->b_data; |
| 839 |
ln = be64_to_cpu(leaf->lf_next); |
| 840 |
brelse(bh); |
| 841 |
if (!ln) |
| 842 |
break; |
| 843 |
|
| 844 |
error = get_leaf(ip, ln, &bh); |
| 845 |
} while(!error); |
| 846 |
|
| 847 |
return error ? ERR_PTR(error) : NULL; |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
error = gfs2_meta_inode_buffer(ip, &bh); |
| 852 |
if (error) |
| 853 |
return ERR_PTR(error); |
| 854 |
dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, scan, name, NULL); |
| 855 |
got_dent: |
| 856 |
if (IS_ERR_OR_NULL(dent)) { |
| 857 |
brelse(bh); |
| 858 |
bh = NULL; |
| 859 |
} |
| 860 |
*pbh = bh; |
| 861 |
return dent; |
| 862 |
} |
| 863 |
|
| 864 |
static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth) |
| 865 |
{ |
| 866 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 867 |
unsigned int n = 1; |
| 868 |
u64 bn; |
| 869 |
int error; |
| 870 |
struct buffer_head *bh; |
| 871 |
struct gfs2_leaf *leaf; |
| 872 |
struct gfs2_dirent *dent; |
| 873 |
struct timespec64 tv = current_time(inode); |
| 874 |
|
| 875 |
error = gfs2_alloc_blocks(ip, &bn, &n, 0); |
| 876 |
if (error) |
| 877 |
return NULL; |
| 878 |
bh = gfs2_meta_new(ip->i_gl, bn); |
| 879 |
if (!bh) |
| 880 |
return NULL; |
| 881 |
|
| 882 |
gfs2_trans_remove_revoke(GFS2_SB(inode), bn, 1); |
| 883 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 884 |
gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); |
| 885 |
leaf = (struct gfs2_leaf *)bh->b_data; |
| 886 |
leaf->lf_depth = cpu_to_be16(depth); |
| 887 |
leaf->lf_entries = 0; |
| 888 |
leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE); |
| 889 |
leaf->lf_next = 0; |
| 890 |
leaf->lf_inode = cpu_to_be64(ip->i_no_addr); |
| 891 |
leaf->lf_dist = cpu_to_be32(1); |
| 892 |
leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); |
| 893 |
leaf->lf_sec = cpu_to_be64(tv.tv_sec); |
| 894 |
memset(leaf->lf_reserved2, 0, sizeof(leaf->lf_reserved2)); |
| 895 |
dent = (struct gfs2_dirent *)(leaf+1); |
| 896 |
gfs2_qstr2dirent(&empty_name, bh->b_size - sizeof(struct gfs2_leaf), dent); |
| 897 |
*pbh = bh; |
| 898 |
return leaf; |
| 899 |
} |
| 900 |
|
| 901 |
/** |
| 902 |
* dir_make_exhash - Convert a stuffed directory into an ExHash directory |
| 903 |
* @inode: The directory inode to be converted to exhash |
| 904 |
* |
| 905 |
* Returns: 0 on success, error code otherwise |
| 906 |
*/ |
| 907 |
|
| 908 |
static int dir_make_exhash(struct inode *inode) |
| 909 |
{ |
| 910 |
struct gfs2_inode *dip = GFS2_I(inode); |
| 911 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 912 |
struct gfs2_dirent *dent; |
| 913 |
struct qstr args; |
| 914 |
struct buffer_head *bh, *dibh; |
| 915 |
struct gfs2_leaf *leaf; |
| 916 |
u32 x; |
| 917 |
__be64 *lp; |
| 918 |
u64 bn; |
| 919 |
int error; |
| 920 |
|
| 921 |
error = gfs2_meta_inode_buffer(dip, &dibh); |
| 922 |
if (error) |
| 923 |
return error; |
| 924 |
|
| 925 |
/* Turn over a new leaf */ |
| 926 |
|
| 927 |
leaf = new_leaf(inode, &bh, 0); |
| 928 |
if (!leaf) |
| 929 |
return -ENOSPC; |
| 930 |
bn = bh->b_blocknr; |
| 931 |
|
| 932 |
gfs2_assert(sdp, dip->i_entries < BIT(16)); |
| 933 |
leaf->lf_entries = cpu_to_be16(dip->i_entries); |
| 934 |
|
| 935 |
/* Copy dirents */ |
| 936 |
|
| 937 |
gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh, |
| 938 |
sizeof(struct gfs2_dinode)); |
| 939 |
|
| 940 |
/* Find last entry */ |
| 941 |
|
| 942 |
x = 0; |
| 943 |
args.len = bh->b_size - sizeof(struct gfs2_dinode) + |
| 944 |
sizeof(struct gfs2_leaf); |
| 945 |
args.name = bh->b_data; |
| 946 |
dent = gfs2_dirent_scan(&dip->i_inode, bh->b_data, bh->b_size, |
| 947 |
gfs2_dirent_last, &args, NULL); |
| 948 |
if (!dent) { |
| 949 |
brelse(bh); |
| 950 |
brelse(dibh); |
| 951 |
return -EIO; |
| 952 |
} |
| 953 |
if (IS_ERR(dent)) { |
| 954 |
brelse(bh); |
| 955 |
brelse(dibh); |
| 956 |
return PTR_ERR(dent); |
| 957 |
} |
| 958 |
|
| 959 |
/* Adjust the last dirent's record length |
| 960 |
(Remember that dent still points to the last entry.) */ |
| 961 |
|
| 962 |
dent->de_rec_len = cpu_to_be16(be16_to_cpu(dent->de_rec_len) + |
| 963 |
sizeof(struct gfs2_dinode) - |
| 964 |
sizeof(struct gfs2_leaf)); |
| 965 |
|
| 966 |
brelse(bh); |
| 967 |
|
| 968 |
/* We're done with the new leaf block, now setup the new |
| 969 |
hash table. */ |
| 970 |
|
| 971 |
gfs2_trans_add_meta(dip->i_gl, dibh); |
| 972 |
gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
| 973 |
|
| 974 |
lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode)); |
| 975 |
|
| 976 |
for (x = sdp->sd_hash_ptrs; x--; lp++) |
| 977 |
*lp = cpu_to_be64(bn); |
| 978 |
|
| 979 |
i_size_write(inode, sdp->sd_sb.sb_bsize / 2); |
| 980 |
gfs2_add_inode_blocks(&dip->i_inode, 1); |
| 981 |
dip->i_diskflags |= GFS2_DIF_EXHASH; |
| 982 |
dip->i_depth = ilog2(sdp->sd_hash_ptrs); |
| 983 |
|
| 984 |
gfs2_dinode_out(dip, dibh->b_data); |
| 985 |
|
| 986 |
brelse(dibh); |
| 987 |
|
| 988 |
return 0; |
| 989 |
} |
| 990 |
|
| 991 |
/** |
| 992 |
* dir_split_leaf - Split a leaf block into two |
| 993 |
* @inode: The directory inode to be split |
| 994 |
* @name: name of the dirent we're trying to insert |
| 995 |
* |
| 996 |
* Returns: 0 on success, error code on failure |
| 997 |
*/ |
| 998 |
|
| 999 |
static int dir_split_leaf(struct inode *inode, const struct qstr *name) |
| 1000 |
{ |
| 1001 |
struct gfs2_inode *dip = GFS2_I(inode); |
| 1002 |
struct buffer_head *nbh, *obh, *dibh; |
| 1003 |
struct gfs2_leaf *nleaf, *oleaf; |
| 1004 |
struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new; |
| 1005 |
u32 start, len, half_len, divider; |
| 1006 |
u64 bn, leaf_no; |
| 1007 |
__be64 *lp; |
| 1008 |
u32 index; |
| 1009 |
int x; |
| 1010 |
int error; |
| 1011 |
|
| 1012 |
index = name->hash >> (32 - dip->i_depth); |
| 1013 |
error = get_leaf_nr(dip, index, &leaf_no); |
| 1014 |
if (error) |
| 1015 |
return error; |
| 1016 |
|
| 1017 |
/* Get the old leaf block */ |
| 1018 |
error = get_leaf(dip, leaf_no, &obh); |
| 1019 |
if (error) |
| 1020 |
return error; |
| 1021 |
|
| 1022 |
oleaf = (struct gfs2_leaf *)obh->b_data; |
| 1023 |
if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) { |
| 1024 |
brelse(obh); |
| 1025 |
return 1; /* can't split */ |
| 1026 |
} |
| 1027 |
|
| 1028 |
gfs2_trans_add_meta(dip->i_gl, obh); |
| 1029 |
|
| 1030 |
nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1); |
| 1031 |
if (!nleaf) { |
| 1032 |
brelse(obh); |
| 1033 |
return -ENOSPC; |
| 1034 |
} |
| 1035 |
bn = nbh->b_blocknr; |
| 1036 |
|
| 1037 |
/* Compute the start and len of leaf pointers in the hash table. */ |
| 1038 |
len = BIT(dip->i_depth - be16_to_cpu(oleaf->lf_depth)); |
| 1039 |
half_len = len >> 1; |
| 1040 |
if (!half_len) { |
| 1041 |
fs_warn(GFS2_SB(inode), "i_depth %u lf_depth %u index %u\n", |
| 1042 |
dip->i_depth, be16_to_cpu(oleaf->lf_depth), index); |
| 1043 |
gfs2_consist_inode(dip); |
| 1044 |
error = -EIO; |
| 1045 |
goto fail_brelse; |
| 1046 |
} |
| 1047 |
|
| 1048 |
start = (index & ~(len - 1)); |
| 1049 |
|
| 1050 |
/* Change the pointers. |
| 1051 |
Don't bother distinguishing stuffed from non-stuffed. |
| 1052 |
This code is complicated enough already. */ |
| 1053 |
lp = kmalloc_array(half_len, sizeof(__be64), GFP_NOFS); |
| 1054 |
if (!lp) { |
| 1055 |
error = -ENOMEM; |
| 1056 |
goto fail_brelse; |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* Change the pointers */ |
| 1060 |
for (x = 0; x < half_len; x++) |
| 1061 |
lp[x] = cpu_to_be64(bn); |
| 1062 |
|
| 1063 |
gfs2_dir_hash_inval(dip); |
| 1064 |
|
| 1065 |
error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64), |
| 1066 |
half_len * sizeof(u64)); |
| 1067 |
if (error != half_len * sizeof(u64)) { |
| 1068 |
if (error >= 0) |
| 1069 |
error = -EIO; |
| 1070 |
goto fail_lpfree; |
| 1071 |
} |
| 1072 |
|
| 1073 |
kfree(lp); |
| 1074 |
|
| 1075 |
/* Compute the divider */ |
| 1076 |
divider = (start + half_len) << (32 - dip->i_depth); |
| 1077 |
|
| 1078 |
/* Copy the entries */ |
| 1079 |
dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf)); |
| 1080 |
|
| 1081 |
do { |
| 1082 |
next = dent; |
| 1083 |
if (dirent_next(dip, obh, &next)) |
| 1084 |
next = NULL; |
| 1085 |
|
| 1086 |
if (!gfs2_dirent_sentinel(dent) && |
| 1087 |
be32_to_cpu(dent->de_hash) < divider) { |
| 1088 |
struct qstr str; |
| 1089 |
void *ptr = ((char *)dent - obh->b_data) + nbh->b_data; |
| 1090 |
str.name = (char*)(dent+1); |
| 1091 |
str.len = be16_to_cpu(dent->de_name_len); |
| 1092 |
str.hash = be32_to_cpu(dent->de_hash); |
| 1093 |
new = gfs2_dirent_split_alloc(inode, nbh, &str, ptr); |
| 1094 |
if (IS_ERR(new)) { |
| 1095 |
error = PTR_ERR(new); |
| 1096 |
break; |
| 1097 |
} |
| 1098 |
|
| 1099 |
new->de_inum = dent->de_inum; /* No endian worries */ |
| 1100 |
new->de_type = dent->de_type; /* No endian worries */ |
| 1101 |
be16_add_cpu(&nleaf->lf_entries, 1); |
| 1102 |
|
| 1103 |
dirent_del(dip, obh, prev, dent); |
| 1104 |
|
| 1105 |
if (!oleaf->lf_entries) |
| 1106 |
gfs2_consist_inode(dip); |
| 1107 |
be16_add_cpu(&oleaf->lf_entries, -1); |
| 1108 |
|
| 1109 |
if (!prev) |
| 1110 |
prev = dent; |
| 1111 |
} else { |
| 1112 |
prev = dent; |
| 1113 |
} |
| 1114 |
dent = next; |
| 1115 |
} while (dent); |
| 1116 |
|
| 1117 |
oleaf->lf_depth = nleaf->lf_depth; |
| 1118 |
|
| 1119 |
error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1120 |
if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) { |
| 1121 |
gfs2_trans_add_meta(dip->i_gl, dibh); |
| 1122 |
gfs2_add_inode_blocks(&dip->i_inode, 1); |
| 1123 |
gfs2_dinode_out(dip, dibh->b_data); |
| 1124 |
brelse(dibh); |
| 1125 |
} |
| 1126 |
|
| 1127 |
brelse(obh); |
| 1128 |
brelse(nbh); |
| 1129 |
|
| 1130 |
return error; |
| 1131 |
|
| 1132 |
fail_lpfree: |
| 1133 |
kfree(lp); |
| 1134 |
|
| 1135 |
fail_brelse: |
| 1136 |
brelse(obh); |
| 1137 |
brelse(nbh); |
| 1138 |
return error; |
| 1139 |
} |
| 1140 |
|
| 1141 |
/** |
| 1142 |
* dir_double_exhash - Double size of ExHash table |
| 1143 |
* @dip: The GFS2 dinode |
| 1144 |
* |
| 1145 |
* Returns: 0 on success, error code on failure |
| 1146 |
*/ |
| 1147 |
|
| 1148 |
static int dir_double_exhash(struct gfs2_inode *dip) |
| 1149 |
{ |
| 1150 |
struct buffer_head *dibh; |
| 1151 |
u32 hsize; |
| 1152 |
u32 hsize_bytes; |
| 1153 |
__be64 *hc; |
| 1154 |
__be64 *hc2, *h; |
| 1155 |
int x; |
| 1156 |
int error = 0; |
| 1157 |
|
| 1158 |
hsize = BIT(dip->i_depth); |
| 1159 |
hsize_bytes = hsize * sizeof(__be64); |
| 1160 |
|
| 1161 |
hc = gfs2_dir_get_hash_table(dip); |
| 1162 |
if (IS_ERR(hc)) |
| 1163 |
return PTR_ERR(hc); |
| 1164 |
|
| 1165 |
hc2 = kmalloc_array(hsize_bytes, 2, GFP_NOFS | __GFP_NOWARN); |
| 1166 |
if (hc2 == NULL) |
| 1167 |
hc2 = __vmalloc(hsize_bytes * 2, GFP_NOFS); |
| 1168 |
|
| 1169 |
if (!hc2) |
| 1170 |
return -ENOMEM; |
| 1171 |
|
| 1172 |
h = hc2; |
| 1173 |
error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1174 |
if (error) |
| 1175 |
goto out_kfree; |
| 1176 |
|
| 1177 |
for (x = 0; x < hsize; x++) { |
| 1178 |
*h++ = *hc; |
| 1179 |
*h++ = *hc; |
| 1180 |
hc++; |
| 1181 |
} |
| 1182 |
|
| 1183 |
error = gfs2_dir_write_data(dip, (char *)hc2, 0, hsize_bytes * 2); |
| 1184 |
if (error != (hsize_bytes * 2)) |
| 1185 |
goto fail; |
| 1186 |
|
| 1187 |
gfs2_dir_hash_inval(dip); |
| 1188 |
dip->i_hash_cache = hc2; |
| 1189 |
dip->i_depth++; |
| 1190 |
gfs2_dinode_out(dip, dibh->b_data); |
| 1191 |
brelse(dibh); |
| 1192 |
return 0; |
| 1193 |
|
| 1194 |
fail: |
| 1195 |
/* Replace original hash table & size */ |
| 1196 |
gfs2_dir_write_data(dip, (char *)hc, 0, hsize_bytes); |
| 1197 |
i_size_write(&dip->i_inode, hsize_bytes); |
| 1198 |
gfs2_dinode_out(dip, dibh->b_data); |
| 1199 |
brelse(dibh); |
| 1200 |
out_kfree: |
| 1201 |
kvfree(hc2); |
| 1202 |
return error; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/** |
| 1206 |
* compare_dents - compare directory entries by hash value |
| 1207 |
* @a: first dent |
| 1208 |
* @b: second dent |
| 1209 |
* |
| 1210 |
* When comparing the hash entries of @a to @b: |
| 1211 |
* gt: returns 1 |
| 1212 |
* lt: returns -1 |
| 1213 |
* eq: returns 0 |
| 1214 |
*/ |
| 1215 |
|
| 1216 |
static int compare_dents(const void *a, const void *b) |
| 1217 |
{ |
| 1218 |
const struct gfs2_dirent *dent_a, *dent_b; |
| 1219 |
u32 hash_a, hash_b; |
| 1220 |
int ret = 0; |
| 1221 |
|
| 1222 |
dent_a = *(const struct gfs2_dirent **)a; |
| 1223 |
hash_a = dent_a->de_cookie; |
| 1224 |
|
| 1225 |
dent_b = *(const struct gfs2_dirent **)b; |
| 1226 |
hash_b = dent_b->de_cookie; |
| 1227 |
|
| 1228 |
if (hash_a > hash_b) |
| 1229 |
ret = 1; |
| 1230 |
else if (hash_a < hash_b) |
| 1231 |
ret = -1; |
| 1232 |
else { |
| 1233 |
unsigned int len_a = be16_to_cpu(dent_a->de_name_len); |
| 1234 |
unsigned int len_b = be16_to_cpu(dent_b->de_name_len); |
| 1235 |
|
| 1236 |
if (len_a > len_b) |
| 1237 |
ret = 1; |
| 1238 |
else if (len_a < len_b) |
| 1239 |
ret = -1; |
| 1240 |
else |
| 1241 |
ret = memcmp(dent_a + 1, dent_b + 1, len_a); |
| 1242 |
} |
| 1243 |
|
| 1244 |
return ret; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/** |
| 1248 |
* do_filldir_main - read out directory entries |
| 1249 |
* @dip: The GFS2 inode |
| 1250 |
* @ctx: what to feed the entries to |
| 1251 |
* @darr: an array of struct gfs2_dirent pointers to read |
| 1252 |
* @entries: the number of entries in darr |
| 1253 |
* @sort_start: index of the directory array to start our sort |
| 1254 |
* @copied: pointer to int that's non-zero if a entry has been copied out |
| 1255 |
* |
| 1256 |
* Jump through some hoops to make sure that if there are hash collsions, |
| 1257 |
* they are read out at the beginning of a buffer. We want to minimize |
| 1258 |
* the possibility that they will fall into different readdir buffers or |
| 1259 |
* that someone will want to seek to that location. |
| 1260 |
* |
| 1261 |
* Returns: errno, >0 if the actor tells you to stop |
| 1262 |
*/ |
| 1263 |
|
| 1264 |
static int do_filldir_main(struct gfs2_inode *dip, struct dir_context *ctx, |
| 1265 |
struct gfs2_dirent **darr, u32 entries, |
| 1266 |
u32 sort_start, int *copied) |
| 1267 |
{ |
| 1268 |
const struct gfs2_dirent *dent, *dent_next; |
| 1269 |
u64 off, off_next; |
| 1270 |
unsigned int x, y; |
| 1271 |
int run = 0; |
| 1272 |
|
| 1273 |
if (sort_start < entries) |
| 1274 |
sort(&darr[sort_start], entries - sort_start, |
| 1275 |
sizeof(struct gfs2_dirent *), compare_dents, NULL); |
| 1276 |
|
| 1277 |
dent_next = darr[0]; |
| 1278 |
off_next = dent_next->de_cookie; |
| 1279 |
|
| 1280 |
for (x = 0, y = 1; x < entries; x++, y++) { |
| 1281 |
dent = dent_next; |
| 1282 |
off = off_next; |
| 1283 |
|
| 1284 |
if (y < entries) { |
| 1285 |
dent_next = darr[y]; |
| 1286 |
off_next = dent_next->de_cookie; |
| 1287 |
|
| 1288 |
if (off < ctx->pos) |
| 1289 |
continue; |
| 1290 |
ctx->pos = off; |
| 1291 |
|
| 1292 |
if (off_next == off) { |
| 1293 |
if (*copied && !run) |
| 1294 |
return 1; |
| 1295 |
run = 1; |
| 1296 |
} else |
| 1297 |
run = 0; |
| 1298 |
} else { |
| 1299 |
if (off < ctx->pos) |
| 1300 |
continue; |
| 1301 |
ctx->pos = off; |
| 1302 |
} |
| 1303 |
|
| 1304 |
if (!dir_emit(ctx, (const char *)(dent + 1), |
| 1305 |
be16_to_cpu(dent->de_name_len), |
| 1306 |
be64_to_cpu(dent->de_inum.no_addr), |
| 1307 |
be16_to_cpu(dent->de_type))) |
| 1308 |
return 1; |
| 1309 |
|
| 1310 |
*copied = 1; |
| 1311 |
} |
| 1312 |
|
| 1313 |
/* Increment the ctx->pos by one, so the next time we come into the |
| 1314 |
do_filldir fxn, we get the next entry instead of the last one in the |
| 1315 |
current leaf */ |
| 1316 |
|
| 1317 |
ctx->pos++; |
| 1318 |
|
| 1319 |
return 0; |
| 1320 |
} |
| 1321 |
|
| 1322 |
static void *gfs2_alloc_sort_buffer(unsigned size) |
| 1323 |
{ |
| 1324 |
void *ptr = NULL; |
| 1325 |
|
| 1326 |
if (size < KMALLOC_MAX_SIZE) |
| 1327 |
ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN); |
| 1328 |
if (!ptr) |
| 1329 |
ptr = __vmalloc(size, GFP_NOFS); |
| 1330 |
return ptr; |
| 1331 |
} |
| 1332 |
|
| 1333 |
|
| 1334 |
static int gfs2_set_cookies(struct gfs2_sbd *sdp, struct buffer_head *bh, |
| 1335 |
unsigned leaf_nr, struct gfs2_dirent **darr, |
| 1336 |
unsigned entries) |
| 1337 |
{ |
| 1338 |
int sort_id = -1; |
| 1339 |
int i; |
| 1340 |
|
| 1341 |
for (i = 0; i < entries; i++) { |
| 1342 |
unsigned offset; |
| 1343 |
|
| 1344 |
darr[i]->de_cookie = be32_to_cpu(darr[i]->de_hash); |
| 1345 |
darr[i]->de_cookie = gfs2_disk_hash2offset(darr[i]->de_cookie); |
| 1346 |
|
| 1347 |
if (!sdp->sd_args.ar_loccookie) |
| 1348 |
continue; |
| 1349 |
offset = (char *)(darr[i]) - |
| 1350 |
(bh->b_data + gfs2_dirent_offset(sdp, bh->b_data)); |
| 1351 |
offset /= GFS2_MIN_DIRENT_SIZE; |
| 1352 |
offset += leaf_nr * sdp->sd_max_dents_per_leaf; |
| 1353 |
if (offset >= GFS2_USE_HASH_FLAG || |
| 1354 |
leaf_nr >= GFS2_USE_HASH_FLAG) { |
| 1355 |
darr[i]->de_cookie |= GFS2_USE_HASH_FLAG; |
| 1356 |
if (sort_id < 0) |
| 1357 |
sort_id = i; |
| 1358 |
continue; |
| 1359 |
} |
| 1360 |
darr[i]->de_cookie &= GFS2_HASH_INDEX_MASK; |
| 1361 |
darr[i]->de_cookie |= offset; |
| 1362 |
} |
| 1363 |
return sort_id; |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
static int gfs2_dir_read_leaf(struct inode *inode, struct dir_context *ctx, |
| 1368 |
int *copied, unsigned *depth, |
| 1369 |
u64 leaf_no) |
| 1370 |
{ |
| 1371 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1372 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1373 |
struct buffer_head *bh; |
| 1374 |
struct gfs2_leaf *lf; |
| 1375 |
unsigned entries = 0, entries2 = 0; |
| 1376 |
unsigned leaves = 0, leaf = 0, offset, sort_offset; |
| 1377 |
struct gfs2_dirent **darr, *dent; |
| 1378 |
struct dirent_gather g; |
| 1379 |
struct buffer_head **larr; |
| 1380 |
int error, i, need_sort = 0, sort_id; |
| 1381 |
u64 lfn = leaf_no; |
| 1382 |
|
| 1383 |
do { |
| 1384 |
error = get_leaf(ip, lfn, &bh); |
| 1385 |
if (error) |
| 1386 |
goto out; |
| 1387 |
lf = (struct gfs2_leaf *)bh->b_data; |
| 1388 |
if (leaves == 0) |
| 1389 |
*depth = be16_to_cpu(lf->lf_depth); |
| 1390 |
entries += be16_to_cpu(lf->lf_entries); |
| 1391 |
leaves++; |
| 1392 |
lfn = be64_to_cpu(lf->lf_next); |
| 1393 |
brelse(bh); |
| 1394 |
} while(lfn); |
| 1395 |
|
| 1396 |
if (*depth < GFS2_DIR_MAX_DEPTH || !sdp->sd_args.ar_loccookie) { |
| 1397 |
need_sort = 1; |
| 1398 |
sort_offset = 0; |
| 1399 |
} |
| 1400 |
|
| 1401 |
if (!entries) |
| 1402 |
return 0; |
| 1403 |
|
| 1404 |
error = -ENOMEM; |
| 1405 |
/* |
| 1406 |
* The extra 99 entries are not normally used, but are a buffer |
| 1407 |
* zone in case the number of entries in the leaf is corrupt. |
| 1408 |
* 99 is the maximum number of entries that can fit in a single |
| 1409 |
* leaf block. |
| 1410 |
*/ |
| 1411 |
larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *)); |
| 1412 |
if (!larr) |
| 1413 |
goto out; |
| 1414 |
darr = (struct gfs2_dirent **)(larr + leaves); |
| 1415 |
g.pdent = (const struct gfs2_dirent **)darr; |
| 1416 |
g.offset = 0; |
| 1417 |
lfn = leaf_no; |
| 1418 |
|
| 1419 |
do { |
| 1420 |
error = get_leaf(ip, lfn, &bh); |
| 1421 |
if (error) |
| 1422 |
goto out_free; |
| 1423 |
lf = (struct gfs2_leaf *)bh->b_data; |
| 1424 |
lfn = be64_to_cpu(lf->lf_next); |
| 1425 |
if (lf->lf_entries) { |
| 1426 |
offset = g.offset; |
| 1427 |
entries2 += be16_to_cpu(lf->lf_entries); |
| 1428 |
dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
| 1429 |
gfs2_dirent_gather, NULL, &g); |
| 1430 |
error = PTR_ERR(dent); |
| 1431 |
if (IS_ERR(dent)) |
| 1432 |
goto out_free; |
| 1433 |
if (entries2 != g.offset) { |
| 1434 |
fs_warn(sdp, "Number of entries corrupt in dir " |
| 1435 |
"leaf %llu, entries2 (%u) != " |
| 1436 |
"g.offset (%u)\n", |
| 1437 |
(unsigned long long)bh->b_blocknr, |
| 1438 |
entries2, g.offset); |
| 1439 |
gfs2_consist_inode(ip); |
| 1440 |
error = -EIO; |
| 1441 |
goto out_free; |
| 1442 |
} |
| 1443 |
error = 0; |
| 1444 |
sort_id = gfs2_set_cookies(sdp, bh, leaf, &darr[offset], |
| 1445 |
be16_to_cpu(lf->lf_entries)); |
| 1446 |
if (!need_sort && sort_id >= 0) { |
| 1447 |
need_sort = 1; |
| 1448 |
sort_offset = offset + sort_id; |
| 1449 |
} |
| 1450 |
larr[leaf++] = bh; |
| 1451 |
} else { |
| 1452 |
larr[leaf++] = NULL; |
| 1453 |
brelse(bh); |
| 1454 |
} |
| 1455 |
} while(lfn); |
| 1456 |
|
| 1457 |
BUG_ON(entries2 != entries); |
| 1458 |
error = do_filldir_main(ip, ctx, darr, entries, need_sort ? |
| 1459 |
sort_offset : entries, copied); |
| 1460 |
out_free: |
| 1461 |
for(i = 0; i < leaf; i++) |
| 1462 |
brelse(larr[i]); |
| 1463 |
kvfree(larr); |
| 1464 |
out: |
| 1465 |
return error; |
| 1466 |
} |
| 1467 |
|
| 1468 |
/** |
| 1469 |
* gfs2_dir_readahead - Issue read-ahead requests for leaf blocks. |
| 1470 |
* @inode: the directory inode |
| 1471 |
* @hsize: hash table size |
| 1472 |
* @index: index into the hash table |
| 1473 |
* @f_ra: read-ahead parameters |
| 1474 |
* |
| 1475 |
* Note: we can't calculate each index like dir_e_read can because we don't |
| 1476 |
* have the leaf, and therefore we don't have the depth, and therefore we |
| 1477 |
* don't have the length. So we have to just read enough ahead to make up |
| 1478 |
* for the loss of information. |
| 1479 |
*/ |
| 1480 |
static void gfs2_dir_readahead(struct inode *inode, unsigned hsize, u32 index, |
| 1481 |
struct file_ra_state *f_ra) |
| 1482 |
{ |
| 1483 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1484 |
struct gfs2_glock *gl = ip->i_gl; |
| 1485 |
struct buffer_head *bh; |
| 1486 |
u64 blocknr = 0, last; |
| 1487 |
unsigned count; |
| 1488 |
|
| 1489 |
/* First check if we've already read-ahead for the whole range. */ |
| 1490 |
if (index + MAX_RA_BLOCKS < f_ra->start) |
| 1491 |
return; |
| 1492 |
|
| 1493 |
f_ra->start = max((pgoff_t)index, f_ra->start); |
| 1494 |
for (count = 0; count < MAX_RA_BLOCKS; count++) { |
| 1495 |
if (f_ra->start >= hsize) /* if exceeded the hash table */ |
| 1496 |
break; |
| 1497 |
|
| 1498 |
last = blocknr; |
| 1499 |
blocknr = be64_to_cpu(ip->i_hash_cache[f_ra->start]); |
| 1500 |
f_ra->start++; |
| 1501 |
if (blocknr == last) |
| 1502 |
continue; |
| 1503 |
|
| 1504 |
bh = gfs2_getbuf(gl, blocknr, 1); |
| 1505 |
if (trylock_buffer(bh)) { |
| 1506 |
if (buffer_uptodate(bh)) { |
| 1507 |
unlock_buffer(bh); |
| 1508 |
brelse(bh); |
| 1509 |
continue; |
| 1510 |
} |
| 1511 |
bh->b_end_io = end_buffer_read_sync; |
| 1512 |
submit_bh(REQ_OP_READ | REQ_RAHEAD | REQ_META | |
| 1513 |
REQ_PRIO, bh); |
| 1514 |
continue; |
| 1515 |
} |
| 1516 |
brelse(bh); |
| 1517 |
} |
| 1518 |
} |
| 1519 |
|
| 1520 |
/** |
| 1521 |
* dir_e_read - Reads the entries from a directory into a filldir buffer |
| 1522 |
* @inode: the directory inode |
| 1523 |
* @ctx: actor to feed the entries to |
| 1524 |
* @f_ra: read-ahead parameters |
| 1525 |
* |
| 1526 |
* Returns: errno |
| 1527 |
*/ |
| 1528 |
|
| 1529 |
static int dir_e_read(struct inode *inode, struct dir_context *ctx, |
| 1530 |
struct file_ra_state *f_ra) |
| 1531 |
{ |
| 1532 |
struct gfs2_inode *dip = GFS2_I(inode); |
| 1533 |
u32 hsize, len = 0; |
| 1534 |
u32 hash, index; |
| 1535 |
__be64 *lp; |
| 1536 |
int copied = 0; |
| 1537 |
int error = 0; |
| 1538 |
unsigned depth = 0; |
| 1539 |
|
| 1540 |
hsize = BIT(dip->i_depth); |
| 1541 |
hash = gfs2_dir_offset2hash(ctx->pos); |
| 1542 |
index = hash >> (32 - dip->i_depth); |
| 1543 |
|
| 1544 |
if (dip->i_hash_cache == NULL) |
| 1545 |
f_ra->start = 0; |
| 1546 |
lp = gfs2_dir_get_hash_table(dip); |
| 1547 |
if (IS_ERR(lp)) |
| 1548 |
return PTR_ERR(lp); |
| 1549 |
|
| 1550 |
gfs2_dir_readahead(inode, hsize, index, f_ra); |
| 1551 |
|
| 1552 |
while (index < hsize) { |
| 1553 |
error = gfs2_dir_read_leaf(inode, ctx, |
| 1554 |
&copied, &depth, |
| 1555 |
be64_to_cpu(lp[index])); |
| 1556 |
if (error) |
| 1557 |
break; |
| 1558 |
|
| 1559 |
len = BIT(dip->i_depth - depth); |
| 1560 |
index = (index & ~(len - 1)) + len; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (error > 0) |
| 1564 |
error = 0; |
| 1565 |
return error; |
| 1566 |
} |
| 1567 |
|
| 1568 |
int gfs2_dir_read(struct inode *inode, struct dir_context *ctx, |
| 1569 |
struct file_ra_state *f_ra) |
| 1570 |
{ |
| 1571 |
struct gfs2_inode *dip = GFS2_I(inode); |
| 1572 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 1573 |
struct dirent_gather g; |
| 1574 |
struct gfs2_dirent **darr, *dent; |
| 1575 |
struct buffer_head *dibh; |
| 1576 |
int copied = 0; |
| 1577 |
int error; |
| 1578 |
|
| 1579 |
if (!dip->i_entries) |
| 1580 |
return 0; |
| 1581 |
|
| 1582 |
if (dip->i_diskflags & GFS2_DIF_EXHASH) |
| 1583 |
return dir_e_read(inode, ctx, f_ra); |
| 1584 |
|
| 1585 |
if (!gfs2_is_stuffed(dip)) { |
| 1586 |
gfs2_consist_inode(dip); |
| 1587 |
return -EIO; |
| 1588 |
} |
| 1589 |
|
| 1590 |
error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1591 |
if (error) |
| 1592 |
return error; |
| 1593 |
|
| 1594 |
error = -ENOMEM; |
| 1595 |
/* 96 is max number of dirents which can be stuffed into an inode */ |
| 1596 |
darr = kmalloc_objs(struct gfs2_dirent *, 96, GFP_NOFS); |
| 1597 |
if (darr) { |
| 1598 |
g.pdent = (const struct gfs2_dirent **)darr; |
| 1599 |
g.offset = 0; |
| 1600 |
dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size, |
| 1601 |
gfs2_dirent_gather, NULL, &g); |
| 1602 |
if (IS_ERR(dent)) { |
| 1603 |
error = PTR_ERR(dent); |
| 1604 |
goto out; |
| 1605 |
} |
| 1606 |
if (dip->i_entries != g.offset) { |
| 1607 |
fs_warn(sdp, "Number of entries corrupt in dir %llu, " |
| 1608 |
"ip->i_entries (%u) != g.offset (%u)\n", |
| 1609 |
(unsigned long long)dip->i_no_addr, |
| 1610 |
dip->i_entries, |
| 1611 |
g.offset); |
| 1612 |
gfs2_consist_inode(dip); |
| 1613 |
error = -EIO; |
| 1614 |
goto out; |
| 1615 |
} |
| 1616 |
gfs2_set_cookies(sdp, dibh, 0, darr, dip->i_entries); |
| 1617 |
error = do_filldir_main(dip, ctx, darr, |
| 1618 |
dip->i_entries, 0, &copied); |
| 1619 |
out: |
| 1620 |
kfree(darr); |
| 1621 |
} |
| 1622 |
|
| 1623 |
if (error > 0) |
| 1624 |
error = 0; |
| 1625 |
|
| 1626 |
brelse(dibh); |
| 1627 |
|
| 1628 |
return error; |
| 1629 |
} |
| 1630 |
|
| 1631 |
/** |
| 1632 |
* gfs2_dir_search - Search a directory |
| 1633 |
* @dir: The GFS2 directory inode |
| 1634 |
* @name: The name we are looking up |
| 1635 |
* @fail_on_exist: Fail if the name exists rather than looking it up |
| 1636 |
* |
| 1637 |
* This routine searches a directory for a file or another directory. |
| 1638 |
* Assumes a glock is held on dip. |
| 1639 |
* |
| 1640 |
* Returns: errno |
| 1641 |
*/ |
| 1642 |
|
| 1643 |
struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name, |
| 1644 |
bool fail_on_exist) |
| 1645 |
{ |
| 1646 |
struct buffer_head *bh; |
| 1647 |
struct gfs2_dirent *dent; |
| 1648 |
u64 addr, formal_ino; |
| 1649 |
u16 dtype; |
| 1650 |
|
| 1651 |
dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); |
| 1652 |
if (dent) { |
| 1653 |
struct inode *inode; |
| 1654 |
u16 rahead; |
| 1655 |
|
| 1656 |
if (IS_ERR(dent)) |
| 1657 |
return ERR_CAST(dent); |
| 1658 |
dtype = be16_to_cpu(dent->de_type); |
| 1659 |
rahead = be16_to_cpu(dent->de_rahead); |
| 1660 |
addr = be64_to_cpu(dent->de_inum.no_addr); |
| 1661 |
formal_ino = be64_to_cpu(dent->de_inum.no_formal_ino); |
| 1662 |
brelse(bh); |
| 1663 |
if (fail_on_exist) |
| 1664 |
return ERR_PTR(-EEXIST); |
| 1665 |
inode = gfs2_inode_lookup(dir->i_sb, dtype, addr, formal_ino, |
| 1666 |
GFS2_BLKST_FREE /* ignore */); |
| 1667 |
if (!IS_ERR(inode)) |
| 1668 |
GFS2_I(inode)->i_rahead = rahead; |
| 1669 |
return inode; |
| 1670 |
} |
| 1671 |
return ERR_PTR(-ENOENT); |
| 1672 |
} |
| 1673 |
|
| 1674 |
int gfs2_dir_check(struct inode *dir, const struct qstr *name, |
| 1675 |
const struct gfs2_inode *ip) |
| 1676 |
{ |
| 1677 |
struct buffer_head *bh; |
| 1678 |
struct gfs2_dirent *dent; |
| 1679 |
int ret = -ENOENT; |
| 1680 |
|
| 1681 |
dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); |
| 1682 |
if (dent) { |
| 1683 |
if (IS_ERR(dent)) |
| 1684 |
return PTR_ERR(dent); |
| 1685 |
if (ip) { |
| 1686 |
if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr) |
| 1687 |
goto out; |
| 1688 |
if (be64_to_cpu(dent->de_inum.no_formal_ino) != |
| 1689 |
ip->i_no_formal_ino) |
| 1690 |
goto out; |
| 1691 |
if (unlikely(IF2DT(ip->i_inode.i_mode) != |
| 1692 |
be16_to_cpu(dent->de_type))) { |
| 1693 |
gfs2_consist_inode(GFS2_I(dir)); |
| 1694 |
ret = -EIO; |
| 1695 |
goto out; |
| 1696 |
} |
| 1697 |
} |
| 1698 |
ret = 0; |
| 1699 |
out: |
| 1700 |
brelse(bh); |
| 1701 |
} |
| 1702 |
return ret; |
| 1703 |
} |
| 1704 |
|
| 1705 |
/** |
| 1706 |
* dir_new_leaf - Add a new leaf onto hash chain |
| 1707 |
* @inode: The directory |
| 1708 |
* @name: The name we are adding |
| 1709 |
* |
| 1710 |
* This adds a new dir leaf onto an existing leaf when there is not |
| 1711 |
* enough space to add a new dir entry. This is a last resort after |
| 1712 |
* we've expanded the hash table to max size and also split existing |
| 1713 |
* leaf blocks, so it will only occur for very large directories. |
| 1714 |
* |
| 1715 |
* The dist parameter is set to 1 for leaf blocks directly attached |
| 1716 |
* to the hash table, 2 for one layer of indirection, 3 for two layers |
| 1717 |
* etc. We are thus able to tell the difference between an old leaf |
| 1718 |
* with dist set to zero (i.e. "don't know") and a new one where we |
| 1719 |
* set this information for debug/fsck purposes. |
| 1720 |
* |
| 1721 |
* Returns: 0 on success, or -ve on error |
| 1722 |
*/ |
| 1723 |
|
| 1724 |
static int dir_new_leaf(struct inode *inode, const struct qstr *name) |
| 1725 |
{ |
| 1726 |
struct buffer_head *bh, *obh; |
| 1727 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1728 |
struct gfs2_leaf *leaf, *oleaf; |
| 1729 |
u32 dist = 1; |
| 1730 |
int error; |
| 1731 |
u32 index; |
| 1732 |
u64 bn; |
| 1733 |
|
| 1734 |
index = name->hash >> (32 - ip->i_depth); |
| 1735 |
error = get_first_leaf(ip, index, &obh); |
| 1736 |
if (error) |
| 1737 |
return error; |
| 1738 |
do { |
| 1739 |
dist++; |
| 1740 |
oleaf = (struct gfs2_leaf *)obh->b_data; |
| 1741 |
bn = be64_to_cpu(oleaf->lf_next); |
| 1742 |
if (!bn) |
| 1743 |
break; |
| 1744 |
brelse(obh); |
| 1745 |
error = get_leaf(ip, bn, &obh); |
| 1746 |
if (error) |
| 1747 |
return error; |
| 1748 |
} while(1); |
| 1749 |
|
| 1750 |
gfs2_trans_add_meta(ip->i_gl, obh); |
| 1751 |
|
| 1752 |
leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth)); |
| 1753 |
if (!leaf) { |
| 1754 |
brelse(obh); |
| 1755 |
return -ENOSPC; |
| 1756 |
} |
| 1757 |
leaf->lf_dist = cpu_to_be32(dist); |
| 1758 |
oleaf->lf_next = cpu_to_be64(bh->b_blocknr); |
| 1759 |
brelse(bh); |
| 1760 |
brelse(obh); |
| 1761 |
|
| 1762 |
error = gfs2_meta_inode_buffer(ip, &bh); |
| 1763 |
if (error) |
| 1764 |
return error; |
| 1765 |
gfs2_trans_add_meta(ip->i_gl, bh); |
| 1766 |
gfs2_add_inode_blocks(&ip->i_inode, 1); |
| 1767 |
gfs2_dinode_out(ip, bh->b_data); |
| 1768 |
brelse(bh); |
| 1769 |
return 0; |
| 1770 |
} |
| 1771 |
|
| 1772 |
static u16 gfs2_inode_ra_len(const struct gfs2_inode *ip) |
| 1773 |
{ |
| 1774 |
u64 where = ip->i_no_addr + 1; |
| 1775 |
if (ip->i_eattr == where) |
| 1776 |
return 1; |
| 1777 |
return 0; |
| 1778 |
} |
| 1779 |
|
| 1780 |
/** |
| 1781 |
* gfs2_dir_add - Add new filename into directory |
| 1782 |
* @inode: The directory inode |
| 1783 |
* @name: The new name |
| 1784 |
* @nip: The GFS2 inode to be linked in to the directory |
| 1785 |
* @da: The directory addition info |
| 1786 |
* |
| 1787 |
* If the call to gfs2_diradd_alloc_required resulted in there being |
| 1788 |
* no need to allocate any new directory blocks, then it will contain |
| 1789 |
* a pointer to the directory entry and the bh in which it resides. We |
| 1790 |
* can use that without having to repeat the search. If there was no |
| 1791 |
* free space, then we must now create more space. |
| 1792 |
* |
| 1793 |
* Returns: 0 on success, error code on failure |
| 1794 |
*/ |
| 1795 |
|
| 1796 |
int gfs2_dir_add(struct inode *inode, const struct qstr *name, |
| 1797 |
const struct gfs2_inode *nip, struct gfs2_diradd *da) |
| 1798 |
{ |
| 1799 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 1800 |
struct buffer_head *bh = da->bh; |
| 1801 |
struct gfs2_dirent *dent = da->dent; |
| 1802 |
struct timespec64 tv; |
| 1803 |
struct gfs2_leaf *leaf; |
| 1804 |
int error; |
| 1805 |
|
| 1806 |
while(1) { |
| 1807 |
if (da->bh == NULL) { |
| 1808 |
dent = gfs2_dirent_search(inode, name, |
| 1809 |
gfs2_dirent_find_space, &bh); |
| 1810 |
} |
| 1811 |
if (dent) { |
| 1812 |
if (IS_ERR(dent)) |
| 1813 |
return PTR_ERR(dent); |
| 1814 |
dent = gfs2_init_dirent(inode, dent, name, bh); |
| 1815 |
gfs2_inum_out(nip, dent); |
| 1816 |
dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode)); |
| 1817 |
dent->de_rahead = cpu_to_be16(gfs2_inode_ra_len(nip)); |
| 1818 |
tv = inode_set_ctime_current(&ip->i_inode); |
| 1819 |
if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
| 1820 |
leaf = (struct gfs2_leaf *)bh->b_data; |
| 1821 |
be16_add_cpu(&leaf->lf_entries, 1); |
| 1822 |
leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); |
| 1823 |
leaf->lf_sec = cpu_to_be64(tv.tv_sec); |
| 1824 |
} |
| 1825 |
da->dent = NULL; |
| 1826 |
da->bh = NULL; |
| 1827 |
brelse(bh); |
| 1828 |
ip->i_entries++; |
| 1829 |
inode_set_mtime_to_ts(&ip->i_inode, tv); |
| 1830 |
if (S_ISDIR(nip->i_inode.i_mode)) |
| 1831 |
inc_nlink(&ip->i_inode); |
| 1832 |
mark_inode_dirty(inode); |
| 1833 |
error = 0; |
| 1834 |
break; |
| 1835 |
} |
| 1836 |
if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) { |
| 1837 |
error = dir_make_exhash(inode); |
| 1838 |
if (error) |
| 1839 |
break; |
| 1840 |
continue; |
| 1841 |
} |
| 1842 |
error = dir_split_leaf(inode, name); |
| 1843 |
if (error == 0) |
| 1844 |
continue; |
| 1845 |
if (error < 0) |
| 1846 |
break; |
| 1847 |
if (ip->i_depth < GFS2_DIR_MAX_DEPTH) { |
| 1848 |
error = dir_double_exhash(ip); |
| 1849 |
if (error) |
| 1850 |
break; |
| 1851 |
error = dir_split_leaf(inode, name); |
| 1852 |
if (error < 0) |
| 1853 |
break; |
| 1854 |
if (error == 0) |
| 1855 |
continue; |
| 1856 |
} |
| 1857 |
error = dir_new_leaf(inode, name); |
| 1858 |
if (!error) |
| 1859 |
continue; |
| 1860 |
error = -ENOSPC; |
| 1861 |
break; |
| 1862 |
} |
| 1863 |
return error; |
| 1864 |
} |
| 1865 |
|
| 1866 |
|
| 1867 |
/** |
| 1868 |
* gfs2_dir_del - Delete a directory entry |
| 1869 |
* @dip: The GFS2 inode |
| 1870 |
* @dentry: The directory entry we want to delete |
| 1871 |
* |
| 1872 |
* Returns: 0 on success, error code on failure |
| 1873 |
*/ |
| 1874 |
|
| 1875 |
int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry) |
| 1876 |
{ |
| 1877 |
const struct qstr *name = &dentry->d_name; |
| 1878 |
struct gfs2_dirent *dent, *prev = NULL; |
| 1879 |
struct buffer_head *bh; |
| 1880 |
struct timespec64 tv; |
| 1881 |
|
| 1882 |
/* Returns _either_ the entry (if its first in block) or the |
| 1883 |
previous entry otherwise */ |
| 1884 |
dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh); |
| 1885 |
if (!dent) { |
| 1886 |
gfs2_consist_inode(dip); |
| 1887 |
return -EIO; |
| 1888 |
} |
| 1889 |
if (IS_ERR(dent)) { |
| 1890 |
gfs2_consist_inode(dip); |
| 1891 |
return PTR_ERR(dent); |
| 1892 |
} |
| 1893 |
/* If not first in block, adjust pointers accordingly */ |
| 1894 |
if (gfs2_dirent_find(dent, name, NULL) == 0) { |
| 1895 |
prev = dent; |
| 1896 |
dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len)); |
| 1897 |
} |
| 1898 |
|
| 1899 |
dirent_del(dip, bh, prev, dent); |
| 1900 |
tv = inode_set_ctime_current(&dip->i_inode); |
| 1901 |
if (dip->i_diskflags & GFS2_DIF_EXHASH) { |
| 1902 |
struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; |
| 1903 |
u16 entries = be16_to_cpu(leaf->lf_entries); |
| 1904 |
if (!entries) |
| 1905 |
gfs2_consist_inode(dip); |
| 1906 |
leaf->lf_entries = cpu_to_be16(--entries); |
| 1907 |
leaf->lf_nsec = cpu_to_be32(tv.tv_nsec); |
| 1908 |
leaf->lf_sec = cpu_to_be64(tv.tv_sec); |
| 1909 |
} |
| 1910 |
brelse(bh); |
| 1911 |
|
| 1912 |
if (!dip->i_entries) |
| 1913 |
gfs2_consist_inode(dip); |
| 1914 |
dip->i_entries--; |
| 1915 |
inode_set_mtime_to_ts(&dip->i_inode, tv); |
| 1916 |
if (d_is_dir(dentry)) |
| 1917 |
drop_nlink(&dip->i_inode); |
| 1918 |
mark_inode_dirty(&dip->i_inode); |
| 1919 |
|
| 1920 |
return 0; |
| 1921 |
} |
| 1922 |
|
| 1923 |
/** |
| 1924 |
* gfs2_dir_mvino - Change inode number of directory entry |
| 1925 |
* @dip: The GFS2 directory inode |
| 1926 |
* @filename: the filename to be moved |
| 1927 |
* @nip: the new GFS2 inode |
| 1928 |
* @new_type: the de_type of the new dirent |
| 1929 |
* |
| 1930 |
* This routine changes the inode number of a directory entry. It's used |
| 1931 |
* by rename to change ".." when a directory is moved. |
| 1932 |
* Assumes a glock is held on dvp. |
| 1933 |
* |
| 1934 |
* Returns: errno |
| 1935 |
*/ |
| 1936 |
|
| 1937 |
int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename, |
| 1938 |
const struct gfs2_inode *nip, unsigned int new_type) |
| 1939 |
{ |
| 1940 |
struct buffer_head *bh; |
| 1941 |
struct gfs2_dirent *dent; |
| 1942 |
|
| 1943 |
dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh); |
| 1944 |
if (!dent) { |
| 1945 |
gfs2_consist_inode(dip); |
| 1946 |
return -EIO; |
| 1947 |
} |
| 1948 |
if (IS_ERR(dent)) |
| 1949 |
return PTR_ERR(dent); |
| 1950 |
|
| 1951 |
gfs2_trans_add_meta(dip->i_gl, bh); |
| 1952 |
gfs2_inum_out(nip, dent); |
| 1953 |
dent->de_type = cpu_to_be16(new_type); |
| 1954 |
brelse(bh); |
| 1955 |
|
| 1956 |
inode_set_mtime_to_ts(&dip->i_inode, inode_set_ctime_current(&dip->i_inode)); |
| 1957 |
mark_inode_dirty_sync(&dip->i_inode); |
| 1958 |
return 0; |
| 1959 |
} |
| 1960 |
|
| 1961 |
/** |
| 1962 |
* leaf_dealloc - Deallocate a directory leaf |
| 1963 |
* @dip: the directory |
| 1964 |
* @index: the hash table offset in the directory |
| 1965 |
* @len: the number of pointers to this leaf |
| 1966 |
* @leaf_no: the leaf number |
| 1967 |
* @leaf_bh: buffer_head for the starting leaf |
| 1968 |
* @last_dealloc: 1 if this is the final dealloc for the leaf, else 0 |
| 1969 |
* |
| 1970 |
* Returns: errno |
| 1971 |
*/ |
| 1972 |
|
| 1973 |
static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len, |
| 1974 |
u64 leaf_no, struct buffer_head *leaf_bh, |
| 1975 |
int last_dealloc) |
| 1976 |
{ |
| 1977 |
struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
| 1978 |
struct gfs2_leaf *tmp_leaf; |
| 1979 |
struct gfs2_rgrp_list rlist; |
| 1980 |
struct buffer_head *bh, *dibh; |
| 1981 |
u64 blk, nblk; |
| 1982 |
unsigned int rg_blocks = 0, l_blocks = 0; |
| 1983 |
char *ht; |
| 1984 |
unsigned int x, size = len * sizeof(u64); |
| 1985 |
int error; |
| 1986 |
|
| 1987 |
error = gfs2_rindex_update(sdp); |
| 1988 |
if (error) |
| 1989 |
return error; |
| 1990 |
|
| 1991 |
memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); |
| 1992 |
|
| 1993 |
ht = kzalloc(size, GFP_NOFS | __GFP_NOWARN); |
| 1994 |
if (ht == NULL) |
| 1995 |
ht = __vmalloc(size, GFP_NOFS | __GFP_NOWARN | __GFP_ZERO); |
| 1996 |
if (!ht) |
| 1997 |
return -ENOMEM; |
| 1998 |
|
| 1999 |
error = gfs2_quota_hold(dip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
| 2000 |
if (error) |
| 2001 |
goto out; |
| 2002 |
|
| 2003 |
/* Count the number of leaves */ |
| 2004 |
bh = leaf_bh; |
| 2005 |
|
| 2006 |
for (blk = leaf_no; blk; blk = nblk) { |
| 2007 |
if (blk != leaf_no) { |
| 2008 |
error = get_leaf(dip, blk, &bh); |
| 2009 |
if (error) |
| 2010 |
goto out_rlist; |
| 2011 |
} |
| 2012 |
tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
| 2013 |
nblk = be64_to_cpu(tmp_leaf->lf_next); |
| 2014 |
if (blk != leaf_no) |
| 2015 |
brelse(bh); |
| 2016 |
|
| 2017 |
gfs2_rlist_add(dip, &rlist, blk); |
| 2018 |
l_blocks++; |
| 2019 |
} |
| 2020 |
|
| 2021 |
gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE); |
| 2022 |
|
| 2023 |
for (x = 0; x < rlist.rl_rgrps; x++) { |
| 2024 |
struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl); |
| 2025 |
|
| 2026 |
rg_blocks += rgd->rd_length; |
| 2027 |
} |
| 2028 |
|
| 2029 |
error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs); |
| 2030 |
if (error) |
| 2031 |
goto out_rlist; |
| 2032 |
|
| 2033 |
error = gfs2_trans_begin(sdp, |
| 2034 |
rg_blocks + (DIV_ROUND_UP(size, sdp->sd_jbsize) + 1) + |
| 2035 |
RES_DINODE + RES_STATFS + RES_QUOTA, RES_DINODE + |
| 2036 |
l_blocks); |
| 2037 |
if (error) |
| 2038 |
goto out_rg_gunlock; |
| 2039 |
|
| 2040 |
bh = leaf_bh; |
| 2041 |
|
| 2042 |
for (blk = leaf_no; blk; blk = nblk) { |
| 2043 |
struct gfs2_rgrpd *rgd; |
| 2044 |
|
| 2045 |
if (blk != leaf_no) { |
| 2046 |
error = get_leaf(dip, blk, &bh); |
| 2047 |
if (error) |
| 2048 |
goto out_end_trans; |
| 2049 |
} |
| 2050 |
tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
| 2051 |
nblk = be64_to_cpu(tmp_leaf->lf_next); |
| 2052 |
if (blk != leaf_no) |
| 2053 |
brelse(bh); |
| 2054 |
|
| 2055 |
rgd = gfs2_blk2rgrpd(sdp, blk, true); |
| 2056 |
gfs2_free_meta(dip, rgd, blk, 1); |
| 2057 |
gfs2_add_inode_blocks(&dip->i_inode, -1); |
| 2058 |
} |
| 2059 |
|
| 2060 |
error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size); |
| 2061 |
if (error != size) { |
| 2062 |
if (error >= 0) |
| 2063 |
error = -EIO; |
| 2064 |
goto out_end_trans; |
| 2065 |
} |
| 2066 |
|
| 2067 |
error = gfs2_meta_inode_buffer(dip, &dibh); |
| 2068 |
if (error) |
| 2069 |
goto out_end_trans; |
| 2070 |
|
| 2071 |
gfs2_trans_add_meta(dip->i_gl, dibh); |
| 2072 |
/* On the last dealloc, make this a regular file in case we crash. |
| 2073 |
(We don't want to free these blocks a second time.) */ |
| 2074 |
if (last_dealloc) |
| 2075 |
dip->i_inode.i_mode = S_IFREG; |
| 2076 |
gfs2_dinode_out(dip, dibh->b_data); |
| 2077 |
brelse(dibh); |
| 2078 |
|
| 2079 |
out_end_trans: |
| 2080 |
gfs2_trans_end(sdp); |
| 2081 |
out_rg_gunlock: |
| 2082 |
gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
| 2083 |
out_rlist: |
| 2084 |
gfs2_rlist_free(&rlist); |
| 2085 |
gfs2_quota_unhold(dip); |
| 2086 |
out: |
| 2087 |
kvfree(ht); |
| 2088 |
return error; |
| 2089 |
} |
| 2090 |
|
| 2091 |
/** |
| 2092 |
* gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory |
| 2093 |
* @dip: the directory |
| 2094 |
* |
| 2095 |
* Dealloc all on-disk directory leaves to FREEMETA state |
| 2096 |
* Change on-disk inode type to "regular file" |
| 2097 |
* |
| 2098 |
* Returns: errno |
| 2099 |
*/ |
| 2100 |
|
| 2101 |
int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip) |
| 2102 |
{ |
| 2103 |
struct buffer_head *bh; |
| 2104 |
struct gfs2_leaf *leaf; |
| 2105 |
u32 hsize, len; |
| 2106 |
u32 index = 0, next_index; |
| 2107 |
__be64 *lp; |
| 2108 |
u64 leaf_no; |
| 2109 |
int error = 0, last; |
| 2110 |
|
| 2111 |
hsize = BIT(dip->i_depth); |
| 2112 |
|
| 2113 |
lp = gfs2_dir_get_hash_table(dip); |
| 2114 |
if (IS_ERR(lp)) |
| 2115 |
return PTR_ERR(lp); |
| 2116 |
|
| 2117 |
while (index < hsize) { |
| 2118 |
leaf_no = be64_to_cpu(lp[index]); |
| 2119 |
if (leaf_no) { |
| 2120 |
error = get_leaf(dip, leaf_no, &bh); |
| 2121 |
if (error) |
| 2122 |
goto out; |
| 2123 |
leaf = (struct gfs2_leaf *)bh->b_data; |
| 2124 |
len = BIT(dip->i_depth - be16_to_cpu(leaf->lf_depth)); |
| 2125 |
|
| 2126 |
next_index = (index & ~(len - 1)) + len; |
| 2127 |
last = ((next_index >= hsize) ? 1 : 0); |
| 2128 |
error = leaf_dealloc(dip, index, len, leaf_no, bh, |
| 2129 |
last); |
| 2130 |
brelse(bh); |
| 2131 |
if (error) |
| 2132 |
goto out; |
| 2133 |
index = next_index; |
| 2134 |
} else |
| 2135 |
index++; |
| 2136 |
} |
| 2137 |
|
| 2138 |
if (index != hsize) { |
| 2139 |
gfs2_consist_inode(dip); |
| 2140 |
error = -EIO; |
| 2141 |
} |
| 2142 |
|
| 2143 |
out: |
| 2144 |
|
| 2145 |
return error; |
| 2146 |
} |
| 2147 |
|
| 2148 |
/** |
| 2149 |
* gfs2_diradd_alloc_required - find if adding entry will require an allocation |
| 2150 |
* @inode: the directory inode being written to |
| 2151 |
* @name: the filename that's going to be added |
| 2152 |
* @da: The structure to return dir alloc info |
| 2153 |
* |
| 2154 |
* Returns: 0 if ok, -ve on error |
| 2155 |
*/ |
| 2156 |
|
| 2157 |
int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name, |
| 2158 |
struct gfs2_diradd *da) |
| 2159 |
{ |
| 2160 |
struct gfs2_inode *ip = GFS2_I(inode); |
| 2161 |
struct gfs2_sbd *sdp = GFS2_SB(inode); |
| 2162 |
const unsigned int extra = sizeof(struct gfs2_dinode) - sizeof(struct gfs2_leaf); |
| 2163 |
struct gfs2_dirent *dent; |
| 2164 |
struct buffer_head *bh; |
| 2165 |
|
| 2166 |
da->nr_blocks = 0; |
| 2167 |
da->bh = NULL; |
| 2168 |
da->dent = NULL; |
| 2169 |
|
| 2170 |
dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh); |
| 2171 |
if (!dent) { |
| 2172 |
da->nr_blocks = sdp->sd_max_dirres; |
| 2173 |
if (!(ip->i_diskflags & GFS2_DIF_EXHASH) && |
| 2174 |
(GFS2_DIRENT_SIZE(name->len) < extra)) |
| 2175 |
da->nr_blocks = 1; |
| 2176 |
return 0; |
| 2177 |
} |
| 2178 |
if (IS_ERR(dent)) |
| 2179 |
return PTR_ERR(dent); |
| 2180 |
|
| 2181 |
if (da->save_loc) { |
| 2182 |
da->bh = bh; |
| 2183 |
da->dent = dent; |
| 2184 |
} else { |
| 2185 |
brelse(bh); |
| 2186 |
} |
| 2187 |
return 0; |
| 2188 |
} |
| 2189 |
|