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