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