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