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 | * |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 39 | * dip->i_diskflags & GFS2_DIF_EXHASH is true |
Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 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 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | #include <linux/slab.h> |
| 57 | #include <linux/spinlock.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 58 | #include <linux/buffer_head.h> |
| 59 | #include <linux/sort.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 60 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 61 | #include <linux/crc32.h> |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame] | 62 | #include <linux/vmalloc.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 63 | |
| 64 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 65 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 66 | #include "dir.h" |
| 67 | #include "glock.h" |
| 68 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 69 | #include "meta_io.h" |
| 70 | #include "quota.h" |
| 71 | #include "rgrp.h" |
| 72 | #include "trans.h" |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 73 | #include "bmap.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 74 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 75 | |
| 76 | #define IS_LEAF 1 /* Hashed (leaf) directory */ |
| 77 | #define IS_DINODE 2 /* Linear (stuffed dinode block) directory */ |
| 78 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 79 | #define gfs2_disk_hash2offset(h) (((u64)(h)) >> 1) |
| 80 | #define gfs2_dir_offset2hash(p) ((u32)(((u64)(p)) << 1)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 81 | |
Steven Whitehouse | 8d12358 | 2010-09-17 12:30:23 +0100 | [diff] [blame] | 82 | struct qstr gfs2_qdot __read_mostly; |
| 83 | struct qstr gfs2_qdotdot __read_mostly; |
| 84 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 85 | typedef int (*gfs2_dscan_t)(const struct gfs2_dirent *dent, |
| 86 | const struct qstr *name, void *opaque); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 88 | int gfs2_dir_get_new_buffer(struct gfs2_inode *ip, u64 block, |
Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 89 | struct buffer_head **bhp) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 90 | { |
| 91 | struct buffer_head *bh; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 92 | |
Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 93 | bh = gfs2_meta_new(ip->i_gl, block); |
| 94 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 95 | gfs2_metatype_set(bh, GFS2_METATYPE_JD, GFS2_FORMAT_JD); |
| 96 | gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 97 | *bhp = bh; |
| 98 | return 0; |
| 99 | } |
| 100 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 101 | 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] | 102 | struct buffer_head **bhp) |
| 103 | { |
| 104 | struct buffer_head *bh; |
| 105 | int error; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 106 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 107 | error = gfs2_meta_read(ip->i_gl, block, DIO_WAIT, &bh); |
Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 108 | if (error) |
| 109 | return error; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 110 | 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] | 111 | brelse(bh); |
| 112 | return -EIO; |
| 113 | } |
| 114 | *bhp = bh; |
| 115 | return 0; |
| 116 | } |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 117 | |
| 118 | static int gfs2_dir_write_stuffed(struct gfs2_inode *ip, const char *buf, |
| 119 | unsigned int offset, unsigned int size) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 120 | { |
| 121 | struct buffer_head *dibh; |
| 122 | int error; |
| 123 | |
| 124 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 125 | if (error) |
| 126 | return error; |
| 127 | |
| 128 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 129 | memcpy(dibh->b_data + offset + sizeof(struct gfs2_dinode), buf, size); |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 130 | if (ip->i_inode.i_size < offset + size) |
| 131 | i_size_write(&ip->i_inode, offset + size); |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 132 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 133 | gfs2_dinode_out(ip, dibh->b_data); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 134 | |
| 135 | brelse(dibh); |
| 136 | |
| 137 | return size; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * gfs2_dir_write_data - Write directory information to the inode |
| 144 | * @ip: The GFS2 inode |
| 145 | * @buf: The buffer containing information to be written |
| 146 | * @offset: The file offset to start writing at |
| 147 | * @size: The amount of data to write |
| 148 | * |
| 149 | * Returns: The number of bytes correctly written or error code |
| 150 | */ |
| 151 | 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] | 152 | u64 offset, unsigned int size) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 153 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 154 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 155 | struct buffer_head *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 156 | u64 lblock, dblock; |
| 157 | u32 extlen = 0; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 158 | unsigned int o; |
| 159 | int copied = 0; |
| 160 | int error = 0; |
Steven Whitehouse | 9b8c81d | 2008-02-22 16:09:31 +0000 | [diff] [blame] | 161 | int new = 0; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 162 | |
| 163 | if (!size) |
| 164 | return 0; |
| 165 | |
| 166 | if (gfs2_is_stuffed(ip) && |
| 167 | offset + size <= sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame] | 168 | return gfs2_dir_write_stuffed(ip, buf, (unsigned int)offset, |
| 169 | size); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 170 | |
| 171 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) |
| 172 | return -EINVAL; |
| 173 | |
| 174 | if (gfs2_is_stuffed(ip)) { |
Steven Whitehouse | f25ef0c | 2006-07-26 10:51:20 -0400 | [diff] [blame] | 175 | error = gfs2_unstuff_dinode(ip, NULL); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 176 | if (error) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 177 | return error; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | lblock = offset; |
| 181 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); |
| 182 | |
| 183 | while (copied < size) { |
| 184 | unsigned int amount; |
| 185 | struct buffer_head *bh; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 186 | |
| 187 | amount = size - copied; |
| 188 | if (amount > sdp->sd_sb.sb_bsize - o) |
| 189 | amount = sdp->sd_sb.sb_bsize - o; |
| 190 | |
| 191 | if (!extlen) { |
| 192 | new = 1; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 193 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 194 | &dblock, &extlen); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 195 | if (error) |
| 196 | goto fail; |
| 197 | error = -EIO; |
| 198 | if (gfs2_assert_withdraw(sdp, dblock)) |
| 199 | goto fail; |
| 200 | } |
| 201 | |
Steven Whitehouse | 61e085a | 2006-04-24 10:07:13 -0400 | [diff] [blame] | 202 | if (amount == sdp->sd_jbsize || new) |
| 203 | error = gfs2_dir_get_new_buffer(ip, dblock, &bh); |
| 204 | else |
| 205 | error = gfs2_dir_get_existing_buffer(ip, dblock, &bh); |
| 206 | |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 207 | if (error) |
| 208 | goto fail; |
| 209 | |
| 210 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 211 | memcpy(bh->b_data + o, buf, amount); |
| 212 | brelse(bh); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 213 | |
Steven Whitehouse | 899bb26 | 2006-08-01 15:28:57 -0400 | [diff] [blame] | 214 | buf += amount; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 215 | copied += amount; |
| 216 | lblock++; |
| 217 | dblock++; |
| 218 | extlen--; |
| 219 | |
| 220 | o = sizeof(struct gfs2_meta_header); |
| 221 | } |
| 222 | |
| 223 | out: |
| 224 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 225 | if (error) |
| 226 | return error; |
| 227 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 228 | if (ip->i_inode.i_size < offset + copied) |
| 229 | i_size_write(&ip->i_inode, offset + copied); |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 230 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 231 | |
| 232 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 233 | gfs2_dinode_out(ip, dibh->b_data); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 234 | brelse(dibh); |
| 235 | |
| 236 | return copied; |
| 237 | fail: |
| 238 | if (copied) |
| 239 | goto out; |
| 240 | return error; |
| 241 | } |
| 242 | |
| 243 | static int gfs2_dir_read_stuffed(struct gfs2_inode *ip, char *buf, |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 244 | u64 offset, unsigned int size) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 245 | { |
| 246 | struct buffer_head *dibh; |
| 247 | int error; |
| 248 | |
| 249 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 250 | if (!error) { |
| 251 | offset += sizeof(struct gfs2_dinode); |
| 252 | memcpy(buf, dibh->b_data + offset, size); |
| 253 | brelse(dibh); |
| 254 | } |
| 255 | |
| 256 | return (error) ? error : size; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /** |
| 261 | * gfs2_dir_read_data - Read a data from a directory inode |
| 262 | * @ip: The GFS2 Inode |
| 263 | * @buf: The buffer to place result into |
| 264 | * @offset: File offset to begin jdata_readng from |
| 265 | * @size: Amount of data to transfer |
| 266 | * |
| 267 | * Returns: The amount of data actually copied or the error |
| 268 | */ |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 269 | static int gfs2_dir_read_data(struct gfs2_inode *ip, char *buf, u64 offset, |
| 270 | unsigned int size, unsigned ra) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 271 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 272 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 273 | u64 lblock, dblock; |
| 274 | u32 extlen = 0; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 275 | unsigned int o; |
| 276 | int copied = 0; |
| 277 | int error = 0; |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 278 | u64 disksize = i_size_read(&ip->i_inode); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 279 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 280 | if (offset >= disksize) |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 281 | return 0; |
| 282 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 283 | if (offset + size > disksize) |
| 284 | size = disksize - offset; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 285 | |
| 286 | if (!size) |
| 287 | return 0; |
| 288 | |
| 289 | if (gfs2_is_stuffed(ip)) |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 290 | return gfs2_dir_read_stuffed(ip, buf, offset, size); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 291 | |
| 292 | if (gfs2_assert_warn(sdp, gfs2_is_jdata(ip))) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | lblock = offset; |
| 296 | o = do_div(lblock, sdp->sd_jbsize) + sizeof(struct gfs2_meta_header); |
| 297 | |
| 298 | while (copied < size) { |
| 299 | unsigned int amount; |
| 300 | struct buffer_head *bh; |
| 301 | int new; |
| 302 | |
| 303 | amount = size - copied; |
| 304 | if (amount > sdp->sd_sb.sb_bsize - o) |
| 305 | amount = sdp->sd_sb.sb_bsize - o; |
| 306 | |
| 307 | if (!extlen) { |
| 308 | new = 0; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 309 | error = gfs2_extent_map(&ip->i_inode, lblock, &new, |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 310 | &dblock, &extlen); |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 311 | if (error || !dblock) |
| 312 | goto fail; |
| 313 | BUG_ON(extlen < 1); |
| 314 | if (!ra) |
| 315 | extlen = 1; |
| 316 | bh = gfs2_meta_ra(ip->i_gl, dblock, extlen); |
Adrian Bunk | b7d8ac3 | 2006-10-19 16:02:07 +0200 | [diff] [blame] | 317 | } else { |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 318 | error = gfs2_meta_read(ip->i_gl, dblock, DIO_WAIT, &bh); |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 319 | if (error) |
| 320 | goto fail; |
| 321 | } |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 322 | error = gfs2_metatype_check(sdp, bh, GFS2_METATYPE_JD); |
| 323 | if (error) { |
| 324 | brelse(bh); |
| 325 | goto fail; |
| 326 | } |
| 327 | dblock++; |
| 328 | extlen--; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 329 | memcpy(buf, bh->b_data + o, amount); |
| 330 | brelse(bh); |
Steven Whitehouse | 899bb26 | 2006-08-01 15:28:57 -0400 | [diff] [blame] | 331 | buf += amount; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 332 | copied += amount; |
| 333 | lblock++; |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 334 | o = sizeof(struct gfs2_meta_header); |
| 335 | } |
| 336 | |
| 337 | return copied; |
| 338 | fail: |
| 339 | return (copied) ? copied : error; |
| 340 | } |
| 341 | |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 342 | static inline int gfs2_dirent_sentinel(const struct gfs2_dirent *dent) |
| 343 | { |
| 344 | return dent->de_inum.no_addr == 0 || dent->de_inum.no_formal_ino == 0; |
| 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 | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 350 | if (!gfs2_dirent_sentinel(dent) && |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 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 | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 395 | if (gfs2_dirent_sentinel(dent)) |
Bob Peterson | 728a756 | 2010-07-14 18:12:26 -0400 | [diff] [blame] | 396 | actual = 0; |
Jan Engelhardt | c539212 | 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; |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 412 | if (!gfs2_dirent_sentinel(dent)) { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 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"; |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 440 | if (unlikely(!first && gfs2_dirent_sentinel(dent))) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 441 | goto error; |
| 442 | msg = "name length is greater than space in dirent"; |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 443 | if (!gfs2_dirent_sentinel(dent) && |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 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 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 529 | static int dirent_check_reclen(struct gfs2_inode *dip, |
| 530 | const struct gfs2_dirent *d, const void *end_p) |
| 531 | { |
| 532 | const void *ptr = d; |
| 533 | u16 rec_len = be16_to_cpu(d->de_rec_len); |
| 534 | |
| 535 | if (unlikely(rec_len < sizeof(struct gfs2_dirent))) |
| 536 | goto broken; |
| 537 | ptr += rec_len; |
| 538 | if (ptr < end_p) |
| 539 | return rec_len; |
| 540 | if (ptr == end_p) |
| 541 | return -ENOENT; |
| 542 | broken: |
| 543 | gfs2_consist_inode(dip); |
| 544 | return -EIO; |
| 545 | } |
| 546 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 547 | /** |
| 548 | * dirent_next - Next dirent |
| 549 | * @dip: the directory |
| 550 | * @bh: The buffer |
| 551 | * @dent: Pointer to list of dirents |
| 552 | * |
| 553 | * Returns: 0 on success, error code otherwise |
| 554 | */ |
| 555 | |
| 556 | static int dirent_next(struct gfs2_inode *dip, struct buffer_head *bh, |
| 557 | struct gfs2_dirent **dent) |
| 558 | { |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 559 | struct gfs2_dirent *cur = *dent, *tmp; |
| 560 | char *bh_end = bh->b_data + bh->b_size; |
| 561 | int ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 562 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 563 | ret = dirent_check_reclen(dip, cur, bh_end); |
| 564 | if (ret < 0) |
| 565 | return ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 566 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 567 | tmp = (void *)cur + ret; |
| 568 | ret = dirent_check_reclen(dip, tmp, bh_end); |
| 569 | if (ret == -EIO) |
| 570 | return ret; |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 571 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 572 | /* Only the first dent could ever have de_inum.no_addr == 0 */ |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 573 | if (gfs2_dirent_sentinel(tmp)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 574 | gfs2_consist_inode(dip); |
| 575 | return -EIO; |
| 576 | } |
| 577 | |
| 578 | *dent = tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * dirent_del - Delete a dirent |
| 584 | * @dip: The GFS2 inode |
| 585 | * @bh: The buffer |
| 586 | * @prev: The previous dirent |
| 587 | * @cur: The current dirent |
| 588 | * |
| 589 | */ |
| 590 | |
| 591 | static void dirent_del(struct gfs2_inode *dip, struct buffer_head *bh, |
| 592 | struct gfs2_dirent *prev, struct gfs2_dirent *cur) |
| 593 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 594 | u16 cur_rec_len, prev_rec_len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 595 | |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 596 | if (gfs2_dirent_sentinel(cur)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 597 | gfs2_consist_inode(dip); |
| 598 | return; |
| 599 | } |
| 600 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 601 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 602 | |
| 603 | /* If there is no prev entry, this is the first entry in the block. |
| 604 | The de_rec_len is already as big as it needs to be. Just zero |
| 605 | out the inode number and return. */ |
| 606 | |
| 607 | if (!prev) { |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 608 | cur->de_inum.no_addr = 0; |
| 609 | cur->de_inum.no_formal_ino = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 610 | return; |
| 611 | } |
| 612 | |
| 613 | /* Combine this dentry with the previous one. */ |
| 614 | |
Steven Whitehouse | fc69d0d | 2006-02-13 16:21:47 +0000 | [diff] [blame] | 615 | prev_rec_len = be16_to_cpu(prev->de_rec_len); |
| 616 | cur_rec_len = be16_to_cpu(cur->de_rec_len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 617 | |
| 618 | if ((char *)prev + prev_rec_len != (char *)cur) |
| 619 | gfs2_consist_inode(dip); |
| 620 | if ((char *)cur + cur_rec_len > bh->b_data + bh->b_size) |
| 621 | gfs2_consist_inode(dip); |
| 622 | |
| 623 | prev_rec_len += cur_rec_len; |
Steven Whitehouse | fc69d0d | 2006-02-13 16:21:47 +0000 | [diff] [blame] | 624 | prev->de_rec_len = cpu_to_be16(prev_rec_len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 627 | /* |
| 628 | * Takes a dent from which to grab space as an argument. Returns the |
| 629 | * newly created dent. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 630 | */ |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 631 | static struct gfs2_dirent *gfs2_init_dirent(struct inode *inode, |
| 632 | struct gfs2_dirent *dent, |
| 633 | const struct qstr *name, |
| 634 | struct buffer_head *bh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 635 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 636 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 637 | struct gfs2_dirent *ndent; |
| 638 | unsigned offset = 0, totlen; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 639 | |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 640 | if (!gfs2_dirent_sentinel(dent)) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 641 | offset = GFS2_DIRENT_SIZE(be16_to_cpu(dent->de_name_len)); |
| 642 | totlen = be16_to_cpu(dent->de_rec_len); |
| 643 | BUG_ON(offset + name->len > totlen); |
| 644 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 645 | ndent = (struct gfs2_dirent *)((char *)dent + offset); |
| 646 | dent->de_rec_len = cpu_to_be16(offset); |
| 647 | gfs2_qstr2dirent(name, totlen - offset, ndent); |
| 648 | return ndent; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 651 | static struct gfs2_dirent *gfs2_dirent_alloc(struct inode *inode, |
| 652 | struct buffer_head *bh, |
| 653 | const struct qstr *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 654 | { |
| 655 | struct gfs2_dirent *dent; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 656 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 657 | gfs2_dirent_find_space, name, NULL); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 658 | if (!dent || IS_ERR(dent)) |
| 659 | return dent; |
| 660 | return gfs2_init_dirent(inode, dent, name, bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 663 | static int get_leaf(struct gfs2_inode *dip, u64 leaf_no, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 664 | struct buffer_head **bhp) |
| 665 | { |
| 666 | int error; |
| 667 | |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 668 | error = gfs2_meta_read(dip->i_gl, leaf_no, DIO_WAIT, bhp); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 669 | if (!error && gfs2_metatype_check(GFS2_SB(&dip->i_inode), *bhp, GFS2_METATYPE_LF)) { |
| 670 | /* printk(KERN_INFO "block num=%llu\n", leaf_no); */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 671 | error = -EIO; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 672 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 673 | |
| 674 | return error; |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * get_leaf_nr - Get a leaf number associated with the index |
| 679 | * @dip: The GFS2 inode |
| 680 | * @index: |
| 681 | * @leaf_out: |
| 682 | * |
| 683 | * Returns: 0 on success, error code otherwise |
| 684 | */ |
| 685 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 686 | static int get_leaf_nr(struct gfs2_inode *dip, u32 index, |
| 687 | u64 *leaf_out) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 688 | { |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 689 | __be64 leaf_no; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 690 | int error; |
| 691 | |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 692 | error = gfs2_dir_read_data(dip, (char *)&leaf_no, |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 693 | index * sizeof(__be64), |
| 694 | sizeof(__be64), 0); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 695 | if (error != sizeof(u64)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 696 | return (error < 0) ? error : -EIO; |
| 697 | |
| 698 | *leaf_out = be64_to_cpu(leaf_no); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 703 | static int get_first_leaf(struct gfs2_inode *dip, u32 index, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 704 | struct buffer_head **bh_out) |
| 705 | { |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 706 | u64 leaf_no; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 707 | int error; |
| 708 | |
| 709 | error = get_leaf_nr(dip, index, &leaf_no); |
| 710 | if (!error) |
| 711 | error = get_leaf(dip, leaf_no, bh_out); |
| 712 | |
| 713 | return error; |
| 714 | } |
| 715 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 716 | static struct gfs2_dirent *gfs2_dirent_search(struct inode *inode, |
| 717 | const struct qstr *name, |
| 718 | gfs2_dscan_t scan, |
| 719 | struct buffer_head **pbh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 720 | { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 721 | struct buffer_head *bh; |
| 722 | struct gfs2_dirent *dent; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 723 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 724 | int error; |
| 725 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 726 | if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 727 | struct gfs2_leaf *leaf; |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 728 | unsigned hsize = 1 << ip->i_depth; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 729 | unsigned index; |
| 730 | u64 ln; |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 731 | if (hsize * sizeof(u64) != i_size_read(inode)) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 732 | gfs2_consist_inode(ip); |
| 733 | return ERR_PTR(-EIO); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 734 | } |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 735 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 736 | index = name->hash >> (32 - ip->i_depth); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 737 | error = get_first_leaf(ip, index, &bh); |
| 738 | if (error) |
| 739 | return ERR_PTR(error); |
| 740 | do { |
| 741 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 742 | scan, name, NULL); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 743 | if (dent) |
| 744 | goto got_dent; |
| 745 | leaf = (struct gfs2_leaf *)bh->b_data; |
| 746 | ln = be64_to_cpu(leaf->lf_next); |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 747 | brelse(bh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 748 | if (!ln) |
| 749 | break; |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 750 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 751 | error = get_leaf(ip, ln, &bh); |
| 752 | } while(!error); |
| 753 | |
| 754 | return error ? ERR_PTR(error) : NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 755 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 756 | |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 757 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 758 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 759 | if (error) |
| 760 | return ERR_PTR(error); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 761 | 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] | 762 | got_dent: |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 763 | if (unlikely(dent == NULL || IS_ERR(dent))) { |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 764 | brelse(bh); |
| 765 | bh = NULL; |
| 766 | } |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 767 | *pbh = bh; |
| 768 | return dent; |
| 769 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 770 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 771 | static struct gfs2_leaf *new_leaf(struct inode *inode, struct buffer_head **pbh, u16 depth) |
| 772 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 773 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | b45e41d | 2008-02-06 10:11:15 +0000 | [diff] [blame] | 774 | unsigned int n = 1; |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 775 | u64 bn; |
| 776 | int error; |
| 777 | struct buffer_head *bh; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 778 | struct gfs2_leaf *leaf; |
| 779 | struct gfs2_dirent *dent; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 780 | struct qstr name = { .name = "", .len = 0, .hash = 0 }; |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 781 | |
| 782 | error = gfs2_alloc_block(ip, &bn, &n); |
| 783 | if (error) |
| 784 | return NULL; |
| 785 | bh = gfs2_meta_new(ip->i_gl, bn); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 786 | if (!bh) |
| 787 | return NULL; |
Steven Whitehouse | 0901097 | 2009-05-20 10:48:47 +0100 | [diff] [blame] | 788 | |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 789 | gfs2_trans_add_unrevoke(GFS2_SB(inode), bn, 1); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 790 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 791 | gfs2_metatype_set(bh, GFS2_METATYPE_LF, GFS2_FORMAT_LF); |
| 792 | leaf = (struct gfs2_leaf *)bh->b_data; |
| 793 | leaf->lf_depth = cpu_to_be16(depth); |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 794 | leaf->lf_entries = 0; |
Al Viro | a2d7d02 | 2006-10-14 16:49:30 +0100 | [diff] [blame] | 795 | leaf->lf_dirent_format = cpu_to_be32(GFS2_FORMAT_DE); |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 796 | leaf->lf_next = 0; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 797 | memset(leaf->lf_reserved, 0, sizeof(leaf->lf_reserved)); |
| 798 | dent = (struct gfs2_dirent *)(leaf+1); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 799 | gfs2_qstr2dirent(&name, bh->b_size - sizeof(struct gfs2_leaf), dent); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 800 | *pbh = bh; |
| 801 | return leaf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | /** |
| 805 | * dir_make_exhash - Convert a stuffed directory into an ExHash directory |
| 806 | * @dip: The GFS2 inode |
| 807 | * |
| 808 | * Returns: 0 on success, error code otherwise |
| 809 | */ |
| 810 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 811 | static int dir_make_exhash(struct inode *inode) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 812 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 813 | struct gfs2_inode *dip = GFS2_I(inode); |
| 814 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 815 | struct gfs2_dirent *dent; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 816 | struct qstr args; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 817 | struct buffer_head *bh, *dibh; |
| 818 | struct gfs2_leaf *leaf; |
| 819 | int y; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 820 | u32 x; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 821 | __be64 *lp; |
| 822 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 823 | int error; |
| 824 | |
| 825 | error = gfs2_meta_inode_buffer(dip, &dibh); |
| 826 | if (error) |
| 827 | return error; |
| 828 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 829 | /* Turn over a new leaf */ |
| 830 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 831 | leaf = new_leaf(inode, &bh, 0); |
| 832 | if (!leaf) |
| 833 | return -ENOSPC; |
| 834 | bn = bh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 835 | |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 836 | gfs2_assert(sdp, dip->i_entries < (1 << 16)); |
| 837 | leaf->lf_entries = cpu_to_be16(dip->i_entries); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 838 | |
| 839 | /* Copy dirents */ |
| 840 | |
| 841 | gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_leaf), dibh, |
| 842 | sizeof(struct gfs2_dinode)); |
| 843 | |
| 844 | /* Find last entry */ |
| 845 | |
| 846 | x = 0; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 847 | args.len = bh->b_size - sizeof(struct gfs2_dinode) + |
| 848 | sizeof(struct gfs2_leaf); |
| 849 | args.name = bh->b_data; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 850 | 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] | 851 | gfs2_dirent_last, &args, NULL); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 852 | if (!dent) { |
| 853 | brelse(bh); |
| 854 | brelse(dibh); |
| 855 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 856 | } |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 857 | if (IS_ERR(dent)) { |
| 858 | brelse(bh); |
| 859 | brelse(dibh); |
| 860 | return PTR_ERR(dent); |
| 861 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 862 | |
| 863 | /* Adjust the last dirent's record length |
| 864 | (Remember that dent still points to the last entry.) */ |
| 865 | |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 866 | 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] | 867 | sizeof(struct gfs2_dinode) - |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 868 | sizeof(struct gfs2_leaf)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 869 | |
| 870 | brelse(bh); |
| 871 | |
| 872 | /* We're done with the new leaf block, now setup the new |
| 873 | hash table. */ |
| 874 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 875 | gfs2_trans_add_bh(dip->i_gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 876 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
| 877 | |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 878 | lp = (__be64 *)(dibh->b_data + sizeof(struct gfs2_dinode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 879 | |
| 880 | for (x = sdp->sd_hash_ptrs; x--; lp++) |
| 881 | *lp = cpu_to_be64(bn); |
| 882 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 883 | i_size_write(inode, sdp->sd_sb.sb_bsize / 2); |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 884 | gfs2_add_inode_blocks(&dip->i_inode, 1); |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 885 | dip->i_diskflags |= GFS2_DIF_EXHASH; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 886 | |
| 887 | for (x = sdp->sd_hash_ptrs, y = -1; x; x >>= 1, y++) ; |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 888 | dip->i_depth = y; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 889 | |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 890 | gfs2_dinode_out(dip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 891 | |
| 892 | brelse(dibh); |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * dir_split_leaf - Split a leaf block into two |
| 899 | * @dip: The GFS2 inode |
| 900 | * @index: |
| 901 | * @leaf_no: |
| 902 | * |
| 903 | * Returns: 0 on success, error code on failure |
| 904 | */ |
| 905 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 906 | static int dir_split_leaf(struct inode *inode, const struct qstr *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 907 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 908 | struct gfs2_inode *dip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 909 | struct buffer_head *nbh, *obh, *dibh; |
| 910 | struct gfs2_leaf *nleaf, *oleaf; |
Steven Whitehouse | 4da3c64 | 2006-07-11 13:19:13 -0400 | [diff] [blame] | 911 | struct gfs2_dirent *dent = NULL, *prev = NULL, *next = NULL, *new; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 912 | u32 start, len, half_len, divider; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 913 | u64 bn, leaf_no; |
| 914 | __be64 *lp; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 915 | u32 index; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 916 | int x, moved = 0; |
| 917 | int error; |
| 918 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 919 | index = name->hash >> (32 - dip->i_depth); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 920 | error = get_leaf_nr(dip, index, &leaf_no); |
| 921 | if (error) |
| 922 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 923 | |
| 924 | /* Get the old leaf block */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 925 | error = get_leaf(dip, leaf_no, &obh); |
| 926 | if (error) |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 927 | return error; |
| 928 | |
| 929 | oleaf = (struct gfs2_leaf *)obh->b_data; |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 930 | if (dip->i_depth == be16_to_cpu(oleaf->lf_depth)) { |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 931 | brelse(obh); |
| 932 | return 1; /* can't split */ |
| 933 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 934 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 935 | gfs2_trans_add_bh(dip->i_gl, obh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 936 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 937 | nleaf = new_leaf(inode, &nbh, be16_to_cpu(oleaf->lf_depth) + 1); |
| 938 | if (!nleaf) { |
| 939 | brelse(obh); |
| 940 | return -ENOSPC; |
| 941 | } |
| 942 | bn = nbh->b_blocknr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 943 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 944 | /* Compute the start and len of leaf pointers in the hash table. */ |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 945 | len = 1 << (dip->i_depth - be16_to_cpu(oleaf->lf_depth)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 946 | half_len = len >> 1; |
| 947 | if (!half_len) { |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 948 | printk(KERN_WARNING "i_depth %u lf_depth %u index %u\n", dip->i_depth, be16_to_cpu(oleaf->lf_depth), index); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 949 | gfs2_consist_inode(dip); |
| 950 | error = -EIO; |
| 951 | goto fail_brelse; |
| 952 | } |
| 953 | |
| 954 | start = (index & ~(len - 1)); |
| 955 | |
| 956 | /* Change the pointers. |
| 957 | Don't bother distinguishing stuffed from non-stuffed. |
| 958 | This code is complicated enough already. */ |
David Rientjes | 4244b52 | 2010-07-20 19:45:03 -0700 | [diff] [blame] | 959 | lp = kmalloc(half_len * sizeof(__be64), GFP_NOFS); |
| 960 | if (!lp) { |
| 961 | error = -ENOMEM; |
| 962 | goto fail_brelse; |
| 963 | } |
| 964 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 965 | /* Change the pointers */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 966 | for (x = 0; x < half_len; x++) |
| 967 | lp[x] = cpu_to_be64(bn); |
| 968 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 969 | error = gfs2_dir_write_data(dip, (char *)lp, start * sizeof(u64), |
| 970 | half_len * sizeof(u64)); |
| 971 | if (error != half_len * sizeof(u64)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 972 | if (error >= 0) |
| 973 | error = -EIO; |
| 974 | goto fail_lpfree; |
| 975 | } |
| 976 | |
| 977 | kfree(lp); |
| 978 | |
| 979 | /* Compute the divider */ |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 980 | divider = (start + half_len) << (32 - dip->i_depth); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 981 | |
| 982 | /* Copy the entries */ |
Steven Whitehouse | 1579343 | 2009-11-06 11:06:37 +0000 | [diff] [blame] | 983 | dent = (struct gfs2_dirent *)(obh->b_data + sizeof(struct gfs2_leaf)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 984 | |
| 985 | do { |
| 986 | next = dent; |
| 987 | if (dirent_next(dip, obh, &next)) |
| 988 | next = NULL; |
| 989 | |
Steven Whitehouse | 5e7d65c | 2006-11-17 12:27:44 -0500 | [diff] [blame] | 990 | if (!gfs2_dirent_sentinel(dent) && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 991 | be32_to_cpu(dent->de_hash) < divider) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 992 | struct qstr str; |
| 993 | str.name = (char*)(dent+1); |
| 994 | str.len = be16_to_cpu(dent->de_name_len); |
| 995 | str.hash = be32_to_cpu(dent->de_hash); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 996 | new = gfs2_dirent_alloc(inode, nbh, &str); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 997 | if (IS_ERR(new)) { |
| 998 | error = PTR_ERR(new); |
| 999 | break; |
| 1000 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1001 | |
| 1002 | new->de_inum = dent->de_inum; /* No endian worries */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1003 | new->de_type = dent->de_type; /* No endian worries */ |
Marcin Slusarz | bb16b34 | 2008-02-13 00:06:10 +0100 | [diff] [blame] | 1004 | be16_add_cpu(&nleaf->lf_entries, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1005 | |
| 1006 | dirent_del(dip, obh, prev, dent); |
| 1007 | |
| 1008 | if (!oleaf->lf_entries) |
| 1009 | gfs2_consist_inode(dip); |
Marcin Slusarz | bb16b34 | 2008-02-13 00:06:10 +0100 | [diff] [blame] | 1010 | be16_add_cpu(&oleaf->lf_entries, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (!prev) |
| 1013 | prev = dent; |
| 1014 | |
| 1015 | moved = 1; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1016 | } else { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1017 | prev = dent; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1018 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1019 | dent = next; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1020 | } while (dent); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1021 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1022 | oleaf->lf_depth = nleaf->lf_depth; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1023 | |
| 1024 | error = gfs2_meta_inode_buffer(dip, &dibh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1025 | if (!gfs2_assert_withdraw(GFS2_SB(&dip->i_inode), !error)) { |
Steven Whitehouse | 382e6e2 | 2007-08-16 17:08:20 +0100 | [diff] [blame] | 1026 | gfs2_trans_add_bh(dip->i_gl, dibh, 1); |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1027 | gfs2_add_inode_blocks(&dip->i_inode, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1028 | gfs2_dinode_out(dip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1029 | brelse(dibh); |
| 1030 | } |
| 1031 | |
| 1032 | brelse(obh); |
| 1033 | brelse(nbh); |
| 1034 | |
| 1035 | return error; |
| 1036 | |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 1037 | fail_lpfree: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1038 | kfree(lp); |
| 1039 | |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 1040 | fail_brelse: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1041 | brelse(obh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1042 | brelse(nbh); |
| 1043 | return error; |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * dir_double_exhash - Double size of ExHash table |
| 1048 | * @dip: The GFS2 dinode |
| 1049 | * |
| 1050 | * Returns: 0 on success, error code on failure |
| 1051 | */ |
| 1052 | |
| 1053 | static int dir_double_exhash(struct gfs2_inode *dip) |
| 1054 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1055 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1056 | struct buffer_head *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1057 | u32 hsize; |
| 1058 | u64 *buf; |
| 1059 | u64 *from, *to; |
| 1060 | u64 block; |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 1061 | u64 disksize = i_size_read(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1062 | int x; |
| 1063 | int error = 0; |
| 1064 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1065 | hsize = 1 << dip->i_depth; |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 1066 | if (hsize * sizeof(u64) != disksize) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1067 | gfs2_consist_inode(dip); |
| 1068 | return -EIO; |
| 1069 | } |
| 1070 | |
| 1071 | /* Allocate both the "from" and "to" buffers in one big chunk */ |
| 1072 | |
David Rientjes | 4244b52 | 2010-07-20 19:45:03 -0700 | [diff] [blame] | 1073 | buf = kcalloc(3, sdp->sd_hash_bsize, GFP_NOFS); |
| 1074 | if (!buf) |
| 1075 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1076 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 1077 | for (block = disksize >> sdp->sd_hash_bsize_shift; block--;) { |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 1078 | error = gfs2_dir_read_data(dip, (char *)buf, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1079 | block * sdp->sd_hash_bsize, |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1080 | sdp->sd_hash_bsize, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1081 | if (error != sdp->sd_hash_bsize) { |
| 1082 | if (error >= 0) |
| 1083 | error = -EIO; |
| 1084 | goto fail; |
| 1085 | } |
| 1086 | |
| 1087 | from = buf; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1088 | to = (u64 *)((char *)buf + sdp->sd_hash_bsize); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1089 | |
| 1090 | for (x = sdp->sd_hash_ptrs; x--; from++) { |
| 1091 | *to++ = *from; /* No endianess worries */ |
| 1092 | *to++ = *from; |
| 1093 | } |
| 1094 | |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 1095 | error = gfs2_dir_write_data(dip, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1096 | (char *)buf + sdp->sd_hash_bsize, |
| 1097 | block * sdp->sd_sb.sb_bsize, |
| 1098 | sdp->sd_sb.sb_bsize); |
| 1099 | if (error != sdp->sd_sb.sb_bsize) { |
| 1100 | if (error >= 0) |
| 1101 | error = -EIO; |
| 1102 | goto fail; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | kfree(buf); |
| 1107 | |
| 1108 | error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1109 | if (!gfs2_assert_withdraw(sdp, !error)) { |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1110 | dip->i_depth++; |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1111 | gfs2_dinode_out(dip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1112 | brelse(dibh); |
| 1113 | } |
| 1114 | |
| 1115 | return error; |
| 1116 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1117 | fail: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1118 | kfree(buf); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1119 | return error; |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * compare_dents - compare directory entries by hash value |
| 1124 | * @a: first dent |
| 1125 | * @b: second dent |
| 1126 | * |
| 1127 | * When comparing the hash entries of @a to @b: |
| 1128 | * gt: returns 1 |
| 1129 | * lt: returns -1 |
| 1130 | * eq: returns 0 |
| 1131 | */ |
| 1132 | |
| 1133 | static int compare_dents(const void *a, const void *b) |
| 1134 | { |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 1135 | const struct gfs2_dirent *dent_a, *dent_b; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1136 | u32 hash_a, hash_b; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1137 | int ret = 0; |
| 1138 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 1139 | dent_a = *(const struct gfs2_dirent **)a; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1140 | hash_a = be32_to_cpu(dent_a->de_hash); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1141 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 1142 | dent_b = *(const struct gfs2_dirent **)b; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1143 | hash_b = be32_to_cpu(dent_b->de_hash); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1144 | |
| 1145 | if (hash_a > hash_b) |
| 1146 | ret = 1; |
| 1147 | else if (hash_a < hash_b) |
| 1148 | ret = -1; |
| 1149 | else { |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 1150 | unsigned int len_a = be16_to_cpu(dent_a->de_name_len); |
| 1151 | unsigned int len_b = be16_to_cpu(dent_b->de_name_len); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1152 | |
| 1153 | if (len_a > len_b) |
| 1154 | ret = 1; |
| 1155 | else if (len_a < len_b) |
| 1156 | ret = -1; |
| 1157 | else |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 1158 | ret = memcmp(dent_a + 1, dent_b + 1, len_a); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | /** |
| 1165 | * do_filldir_main - read out directory entries |
| 1166 | * @dip: The GFS2 inode |
| 1167 | * @offset: The offset in the file to read from |
| 1168 | * @opaque: opaque data to pass to filldir |
| 1169 | * @filldir: The function to pass entries to |
| 1170 | * @darr: an array of struct gfs2_dirent pointers to read |
| 1171 | * @entries: the number of entries in darr |
| 1172 | * @copied: pointer to int that's non-zero if a entry has been copied out |
| 1173 | * |
| 1174 | * Jump through some hoops to make sure that if there are hash collsions, |
| 1175 | * they are read out at the beginning of a buffer. We want to minimize |
| 1176 | * the possibility that they will fall into different readdir buffers or |
| 1177 | * that someone will want to seek to that location. |
| 1178 | * |
| 1179 | * Returns: errno, >0 on exception from filldir |
| 1180 | */ |
| 1181 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1182 | static int do_filldir_main(struct gfs2_inode *dip, u64 *offset, |
Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 1183 | void *opaque, filldir_t filldir, |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1184 | const struct gfs2_dirent **darr, u32 entries, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1185 | int *copied) |
| 1186 | { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1187 | const struct gfs2_dirent *dent, *dent_next; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1188 | u64 off, off_next; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1189 | unsigned int x, y; |
| 1190 | int run = 0; |
| 1191 | int error = 0; |
| 1192 | |
| 1193 | sort(darr, entries, sizeof(struct gfs2_dirent *), compare_dents, NULL); |
| 1194 | |
| 1195 | dent_next = darr[0]; |
| 1196 | off_next = be32_to_cpu(dent_next->de_hash); |
| 1197 | off_next = gfs2_disk_hash2offset(off_next); |
| 1198 | |
| 1199 | for (x = 0, y = 1; x < entries; x++, y++) { |
| 1200 | dent = dent_next; |
| 1201 | off = off_next; |
| 1202 | |
| 1203 | if (y < entries) { |
| 1204 | dent_next = darr[y]; |
| 1205 | off_next = be32_to_cpu(dent_next->de_hash); |
| 1206 | off_next = gfs2_disk_hash2offset(off_next); |
| 1207 | |
| 1208 | if (off < *offset) |
| 1209 | continue; |
| 1210 | *offset = off; |
| 1211 | |
| 1212 | if (off_next == off) { |
| 1213 | if (*copied && !run) |
| 1214 | return 1; |
| 1215 | run = 1; |
| 1216 | } else |
| 1217 | run = 0; |
| 1218 | } else { |
| 1219 | if (off < *offset) |
| 1220 | continue; |
| 1221 | *offset = off; |
| 1222 | } |
| 1223 | |
Steven Whitehouse | 2bdbc5d | 2006-09-05 09:34:20 -0400 | [diff] [blame] | 1224 | error = filldir(opaque, (const char *)(dent + 1), |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 1225 | be16_to_cpu(dent->de_name_len), |
Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 1226 | off, be64_to_cpu(dent->de_inum.no_addr), |
Steven Whitehouse | 4dd651a | 2006-02-14 15:56:44 +0000 | [diff] [blame] | 1227 | be16_to_cpu(dent->de_type)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1228 | if (error) |
| 1229 | return 1; |
| 1230 | |
| 1231 | *copied = 1; |
| 1232 | } |
| 1233 | |
| 1234 | /* Increment the *offset by one, so the next time we come into the |
| 1235 | do_filldir fxn, we get the next entry instead of the last one in the |
| 1236 | current leaf */ |
| 1237 | |
| 1238 | (*offset)++; |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1243 | static void *gfs2_alloc_sort_buffer(unsigned size) |
| 1244 | { |
| 1245 | void *ptr = NULL; |
| 1246 | |
| 1247 | if (size < KMALLOC_MAX_SIZE) |
| 1248 | ptr = kmalloc(size, GFP_NOFS | __GFP_NOWARN); |
| 1249 | if (!ptr) |
| 1250 | ptr = __vmalloc(size, GFP_NOFS, PAGE_KERNEL); |
| 1251 | return ptr; |
| 1252 | } |
| 1253 | |
| 1254 | static void gfs2_free_sort_buffer(void *ptr) |
| 1255 | { |
| 1256 | if (is_vmalloc_addr(ptr)) |
| 1257 | vfree(ptr); |
| 1258 | else |
| 1259 | kfree(ptr); |
| 1260 | } |
| 1261 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1262 | static int gfs2_dir_read_leaf(struct inode *inode, u64 *offset, void *opaque, |
Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 1263 | filldir_t filldir, int *copied, unsigned *depth, |
| 1264 | u64 leaf_no) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1265 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1266 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1267 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1268 | struct buffer_head *bh; |
| 1269 | struct gfs2_leaf *lf; |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1270 | unsigned entries = 0, entries2 = 0; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1271 | unsigned leaves = 0; |
| 1272 | const struct gfs2_dirent **darr, *dent; |
| 1273 | struct dirent_gather g; |
| 1274 | struct buffer_head **larr; |
| 1275 | int leaf = 0; |
| 1276 | int error, i; |
| 1277 | u64 lfn = leaf_no; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1278 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1279 | do { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1280 | error = get_leaf(ip, lfn, &bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1281 | if (error) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1282 | goto out; |
| 1283 | lf = (struct gfs2_leaf *)bh->b_data; |
| 1284 | if (leaves == 0) |
| 1285 | *depth = be16_to_cpu(lf->lf_depth); |
| 1286 | entries += be16_to_cpu(lf->lf_entries); |
| 1287 | leaves++; |
| 1288 | lfn = be64_to_cpu(lf->lf_next); |
| 1289 | brelse(bh); |
| 1290 | } while(lfn); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1291 | |
| 1292 | if (!entries) |
| 1293 | return 0; |
| 1294 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1295 | error = -ENOMEM; |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1296 | /* |
| 1297 | * The extra 99 entries are not normally used, but are a buffer |
| 1298 | * zone in case the number of entries in the leaf is corrupt. |
| 1299 | * 99 is the maximum number of entries that can fit in a single |
| 1300 | * leaf block. |
| 1301 | */ |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1302 | larr = gfs2_alloc_sort_buffer((leaves + entries + 99) * sizeof(void *)); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1303 | if (!larr) |
| 1304 | goto out; |
| 1305 | darr = (const struct gfs2_dirent **)(larr + leaves); |
| 1306 | g.pdent = darr; |
| 1307 | g.offset = 0; |
| 1308 | lfn = leaf_no; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1309 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1310 | do { |
| 1311 | error = get_leaf(ip, lfn, &bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1312 | if (error) |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1313 | goto out_free; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1314 | lf = (struct gfs2_leaf *)bh->b_data; |
| 1315 | lfn = be64_to_cpu(lf->lf_next); |
| 1316 | if (lf->lf_entries) { |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1317 | entries2 += be16_to_cpu(lf->lf_entries); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1318 | dent = gfs2_dirent_scan(inode, bh->b_data, bh->b_size, |
| 1319 | gfs2_dirent_gather, NULL, &g); |
| 1320 | error = PTR_ERR(dent); |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1321 | if (IS_ERR(dent)) |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1322 | goto out_free; |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1323 | if (entries2 != g.offset) { |
akpm@linux-foundation.org | f391a4e | 2007-04-25 21:08:02 -0700 | [diff] [blame] | 1324 | fs_warn(sdp, "Number of entries corrupt in dir " |
| 1325 | "leaf %llu, entries2 (%u) != " |
| 1326 | "g.offset (%u)\n", |
| 1327 | (unsigned long long)bh->b_blocknr, |
| 1328 | entries2, g.offset); |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1329 | |
| 1330 | error = -EIO; |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1331 | goto out_free; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1332 | } |
| 1333 | error = 0; |
| 1334 | larr[leaf++] = bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1335 | } else { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1336 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1337 | } |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1338 | } while(lfn); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1339 | |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1340 | BUG_ON(entries2 != entries); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1341 | error = do_filldir_main(ip, offset, opaque, filldir, darr, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1342 | entries, copied); |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1343 | out_free: |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1344 | for(i = 0; i < leaf; i++) |
| 1345 | brelse(larr[i]); |
Steven Whitehouse | d2a97a4 | 2010-07-28 17:56:23 +0100 | [diff] [blame] | 1346 | gfs2_free_sort_buffer(larr); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1347 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1348 | return error; |
| 1349 | } |
| 1350 | |
| 1351 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1352 | * dir_e_read - Reads the entries from a directory into a filldir buffer |
| 1353 | * @dip: dinode pointer |
| 1354 | * @offset: the hash of the last entry read shifted to the right once |
| 1355 | * @opaque: buffer for the filldir function to fill |
| 1356 | * @filldir: points to the filldir function to use |
| 1357 | * |
| 1358 | * Returns: errno |
| 1359 | */ |
| 1360 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1361 | static int dir_e_read(struct inode *inode, u64 *offset, void *opaque, |
Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 1362 | filldir_t filldir) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1363 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1364 | struct gfs2_inode *dip = GFS2_I(inode); |
| 1365 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1366 | u32 hsize, len = 0; |
| 1367 | u32 ht_offset, lp_offset, ht_offset_cur = -1; |
| 1368 | u32 hash, index; |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1369 | __be64 *lp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1370 | int copied = 0; |
| 1371 | int error = 0; |
Steven Whitehouse | 4da3c64 | 2006-07-11 13:19:13 -0400 | [diff] [blame] | 1372 | unsigned depth = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1373 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1374 | hsize = 1 << dip->i_depth; |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 1375 | if (hsize * sizeof(u64) != i_size_read(inode)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1376 | gfs2_consist_inode(dip); |
| 1377 | return -EIO; |
| 1378 | } |
| 1379 | |
| 1380 | hash = gfs2_dir_offset2hash(*offset); |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1381 | index = hash >> (32 - dip->i_depth); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1382 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1383 | lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1384 | if (!lp) |
| 1385 | return -ENOMEM; |
| 1386 | |
| 1387 | while (index < hsize) { |
| 1388 | lp_offset = index & (sdp->sd_hash_ptrs - 1); |
| 1389 | ht_offset = index - lp_offset; |
| 1390 | |
| 1391 | if (ht_offset_cur != ht_offset) { |
Steven Whitehouse | e13940b | 2006-01-30 13:31:50 +0000 | [diff] [blame] | 1392 | error = gfs2_dir_read_data(dip, (char *)lp, |
Al Viro | b44b84d | 2006-10-14 10:46:30 -0400 | [diff] [blame] | 1393 | ht_offset * sizeof(__be64), |
Steven Whitehouse | 7276b3b | 2006-09-21 17:05:23 -0400 | [diff] [blame] | 1394 | sdp->sd_hash_bsize, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1395 | if (error != sdp->sd_hash_bsize) { |
| 1396 | if (error >= 0) |
| 1397 | error = -EIO; |
| 1398 | goto out; |
| 1399 | } |
| 1400 | ht_offset_cur = ht_offset; |
| 1401 | } |
| 1402 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1403 | error = gfs2_dir_read_leaf(inode, offset, opaque, filldir, |
| 1404 | &copied, &depth, |
| 1405 | be64_to_cpu(lp[lp_offset])); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1406 | if (error) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1407 | break; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1408 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1409 | len = 1 << (dip->i_depth - depth); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1410 | index = (index & ~(len - 1)) + len; |
| 1411 | } |
| 1412 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1413 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1414 | kfree(lp); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1415 | if (error > 0) |
| 1416 | error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1417 | return error; |
| 1418 | } |
| 1419 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1420 | int gfs2_dir_read(struct inode *inode, u64 *offset, void *opaque, |
Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 1421 | filldir_t filldir) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1422 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1423 | struct gfs2_inode *dip = GFS2_I(inode); |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1424 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1425 | struct dirent_gather g; |
| 1426 | const struct gfs2_dirent **darr, *dent; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1427 | struct buffer_head *dibh; |
| 1428 | int copied = 0; |
| 1429 | int error; |
| 1430 | |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1431 | if (!dip->i_entries) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1432 | return 0; |
| 1433 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1434 | if (dip->i_diskflags & GFS2_DIF_EXHASH) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1435 | return dir_e_read(inode, offset, opaque, filldir); |
| 1436 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1437 | if (!gfs2_is_stuffed(dip)) { |
| 1438 | gfs2_consist_inode(dip); |
| 1439 | return -EIO; |
| 1440 | } |
| 1441 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1442 | error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1443 | if (error) |
| 1444 | return error; |
| 1445 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1446 | error = -ENOMEM; |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1447 | /* 96 is max number of dirents which can be stuffed into an inode */ |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1448 | darr = kmalloc(96 * sizeof(struct gfs2_dirent *), GFP_NOFS); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1449 | if (darr) { |
| 1450 | g.pdent = darr; |
| 1451 | g.offset = 0; |
| 1452 | dent = gfs2_dirent_scan(inode, dibh->b_data, dibh->b_size, |
| 1453 | gfs2_dirent_gather, NULL, &g); |
| 1454 | if (IS_ERR(dent)) { |
| 1455 | error = PTR_ERR(dent); |
| 1456 | goto out; |
| 1457 | } |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1458 | if (dip->i_entries != g.offset) { |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1459 | fs_warn(sdp, "Number of entries corrupt in dir %llu, " |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1460 | "ip->i_entries (%u) != g.offset (%u)\n", |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1461 | (unsigned long long)dip->i_no_addr, |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1462 | dip->i_entries, |
Steven Whitehouse | bdd19a2 | 2007-04-18 09:38:42 +0100 | [diff] [blame] | 1463 | g.offset); |
| 1464 | error = -EIO; |
| 1465 | goto out; |
| 1466 | } |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1467 | error = do_filldir_main(dip, offset, opaque, filldir, darr, |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1468 | dip->i_entries, &copied); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1469 | out: |
| 1470 | kfree(darr); |
| 1471 | } |
| 1472 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1473 | if (error > 0) |
| 1474 | error = 0; |
| 1475 | |
| 1476 | brelse(dibh); |
| 1477 | |
| 1478 | return error; |
| 1479 | } |
| 1480 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1481 | /** |
| 1482 | * gfs2_dir_search - Search a directory |
| 1483 | * @dip: The GFS2 inode |
| 1484 | * @filename: |
| 1485 | * @inode: |
| 1486 | * |
| 1487 | * This routine searches a directory for a file or another directory. |
| 1488 | * Assumes a glock is held on dip. |
| 1489 | * |
| 1490 | * Returns: errno |
| 1491 | */ |
| 1492 | |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1493 | struct inode *gfs2_dir_search(struct inode *dir, const struct qstr *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1494 | { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1495 | struct buffer_head *bh; |
| 1496 | struct gfs2_dirent *dent; |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1497 | struct inode *inode; |
| 1498 | |
| 1499 | dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); |
| 1500 | if (dent) { |
| 1501 | if (IS_ERR(dent)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 1502 | return ERR_CAST(dent); |
Wendy Cheng | bb9bcf0 | 2007-06-27 17:07:08 -0400 | [diff] [blame] | 1503 | inode = gfs2_inode_lookup(dir->i_sb, |
| 1504 | be16_to_cpu(dent->de_type), |
| 1505 | be64_to_cpu(dent->de_inum.no_addr), |
Bob Peterson | 44ad37d | 2011-03-17 16:19:58 -0400 | [diff] [blame] | 1506 | be64_to_cpu(dent->de_inum.no_formal_ino), 0); |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1507 | brelse(bh); |
| 1508 | return inode; |
| 1509 | } |
| 1510 | return ERR_PTR(-ENOENT); |
| 1511 | } |
| 1512 | |
| 1513 | int gfs2_dir_check(struct inode *dir, const struct qstr *name, |
| 1514 | const struct gfs2_inode *ip) |
| 1515 | { |
| 1516 | struct buffer_head *bh; |
| 1517 | struct gfs2_dirent *dent; |
| 1518 | int ret = -ENOENT; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1519 | |
| 1520 | dent = gfs2_dirent_search(dir, name, gfs2_dirent_find, &bh); |
| 1521 | if (dent) { |
| 1522 | if (IS_ERR(dent)) |
| 1523 | return PTR_ERR(dent); |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1524 | if (ip) { |
| 1525 | if (be64_to_cpu(dent->de_inum.no_addr) != ip->i_no_addr) |
| 1526 | goto out; |
| 1527 | if (be64_to_cpu(dent->de_inum.no_formal_ino) != |
| 1528 | ip->i_no_formal_ino) |
| 1529 | goto out; |
| 1530 | if (unlikely(IF2DT(ip->i_inode.i_mode) != |
| 1531 | be16_to_cpu(dent->de_type))) { |
| 1532 | gfs2_consist_inode(GFS2_I(dir)); |
| 1533 | ret = -EIO; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | } |
| 1537 | ret = 0; |
| 1538 | out: |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1539 | brelse(bh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1540 | } |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1541 | return ret; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static int dir_new_leaf(struct inode *inode, const struct qstr *name) |
| 1545 | { |
| 1546 | struct buffer_head *bh, *obh; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1547 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1548 | struct gfs2_leaf *leaf, *oleaf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1549 | int error; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1550 | u32 index; |
| 1551 | u64 bn; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1552 | |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1553 | index = name->hash >> (32 - ip->i_depth); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1554 | error = get_first_leaf(ip, index, &obh); |
| 1555 | if (error) |
| 1556 | return error; |
| 1557 | do { |
| 1558 | oleaf = (struct gfs2_leaf *)obh->b_data; |
| 1559 | bn = be64_to_cpu(oleaf->lf_next); |
| 1560 | if (!bn) |
| 1561 | break; |
| 1562 | brelse(obh); |
| 1563 | error = get_leaf(ip, bn, &obh); |
| 1564 | if (error) |
| 1565 | return error; |
| 1566 | } while(1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1567 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1568 | gfs2_trans_add_bh(ip->i_gl, obh, 1); |
| 1569 | |
| 1570 | leaf = new_leaf(inode, &bh, be16_to_cpu(oleaf->lf_depth)); |
| 1571 | if (!leaf) { |
| 1572 | brelse(obh); |
| 1573 | return -ENOSPC; |
| 1574 | } |
Steven Whitehouse | 4d8012b | 2006-04-12 17:39:45 -0400 | [diff] [blame] | 1575 | oleaf->lf_next = cpu_to_be64(bh->b_blocknr); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1576 | brelse(bh); |
| 1577 | brelse(obh); |
| 1578 | |
| 1579 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 1580 | if (error) |
| 1581 | return error; |
| 1582 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1583 | gfs2_add_inode_blocks(&ip->i_inode, 1); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1584 | gfs2_dinode_out(ip, bh->b_data); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1585 | brelse(bh); |
| 1586 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | /** |
| 1590 | * gfs2_dir_add - Add new filename into directory |
| 1591 | * @dip: The GFS2 inode |
| 1592 | * @filename: The new name |
| 1593 | * @inode: The inode number of the entry |
| 1594 | * @type: The type of the entry |
| 1595 | * |
| 1596 | * Returns: 0 on success, error code on failure |
| 1597 | */ |
| 1598 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1599 | int gfs2_dir_add(struct inode *inode, const struct qstr *name, |
Steven Whitehouse | 3d6ecb7 | 2011-05-09 13:30:08 +0100 | [diff] [blame] | 1600 | const struct gfs2_inode *nip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1601 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1602 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1603 | struct buffer_head *bh; |
| 1604 | struct gfs2_dirent *dent; |
| 1605 | struct gfs2_leaf *leaf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1606 | int error; |
| 1607 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1608 | while(1) { |
| 1609 | dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, |
| 1610 | &bh); |
| 1611 | if (dent) { |
| 1612 | if (IS_ERR(dent)) |
| 1613 | return PTR_ERR(dent); |
| 1614 | dent = gfs2_init_dirent(inode, dent, name, bh); |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1615 | gfs2_inum_out(nip, dent); |
Steven Whitehouse | 3d6ecb7 | 2011-05-09 13:30:08 +0100 | [diff] [blame] | 1616 | dent->de_type = cpu_to_be16(IF2DT(nip->i_inode.i_mode)); |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1617 | if (ip->i_diskflags & GFS2_DIF_EXHASH) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1618 | leaf = (struct gfs2_leaf *)bh->b_data; |
Marcin Slusarz | bb16b34 | 2008-02-13 00:06:10 +0100 | [diff] [blame] | 1619 | be16_add_cpu(&leaf->lf_entries, 1); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1620 | } |
| 1621 | brelse(bh); |
| 1622 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 1623 | if (error) |
| 1624 | break; |
| 1625 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1626 | ip->i_entries++; |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1627 | ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | 3d6ecb7 | 2011-05-09 13:30:08 +0100 | [diff] [blame] | 1628 | if (S_ISDIR(nip->i_inode.i_mode)) |
| 1629 | inc_nlink(&ip->i_inode); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1630 | gfs2_dinode_out(ip, bh->b_data); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1631 | brelse(bh); |
| 1632 | error = 0; |
| 1633 | break; |
| 1634 | } |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1635 | if (!(ip->i_diskflags & GFS2_DIF_EXHASH)) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1636 | error = dir_make_exhash(inode); |
| 1637 | if (error) |
| 1638 | break; |
| 1639 | continue; |
| 1640 | } |
| 1641 | error = dir_split_leaf(inode, name); |
| 1642 | if (error == 0) |
| 1643 | continue; |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 1644 | if (error < 0) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1645 | break; |
Steven Whitehouse | 9a00450 | 2008-02-01 09:23:44 +0000 | [diff] [blame] | 1646 | if (ip->i_depth < GFS2_DIR_MAX_DEPTH) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1647 | error = dir_double_exhash(ip); |
| 1648 | if (error) |
| 1649 | break; |
| 1650 | error = dir_split_leaf(inode, name); |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 1651 | if (error < 0) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1652 | break; |
Steven Whitehouse | e90deff | 2006-03-29 19:02:15 -0500 | [diff] [blame] | 1653 | if (error == 0) |
| 1654 | continue; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1655 | } |
| 1656 | error = dir_new_leaf(inode, name); |
| 1657 | if (!error) |
| 1658 | continue; |
| 1659 | error = -ENOSPC; |
| 1660 | break; |
| 1661 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1662 | return error; |
| 1663 | } |
| 1664 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1665 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1666 | /** |
| 1667 | * gfs2_dir_del - Delete a directory entry |
| 1668 | * @dip: The GFS2 inode |
| 1669 | * @filename: The filename |
| 1670 | * |
| 1671 | * Returns: 0 on success, error code on failure |
| 1672 | */ |
| 1673 | |
Steven Whitehouse | 855d23c | 2011-05-09 16:42:37 +0100 | [diff] [blame] | 1674 | int gfs2_dir_del(struct gfs2_inode *dip, const struct dentry *dentry) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1675 | { |
Steven Whitehouse | 855d23c | 2011-05-09 16:42:37 +0100 | [diff] [blame] | 1676 | const struct qstr *name = &dentry->d_name; |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1677 | struct gfs2_dirent *dent, *prev = NULL; |
| 1678 | struct buffer_head *bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1679 | int error; |
| 1680 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1681 | /* Returns _either_ the entry (if its first in block) or the |
| 1682 | previous entry otherwise */ |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1683 | dent = gfs2_dirent_search(&dip->i_inode, name, gfs2_dirent_prev, &bh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1684 | if (!dent) { |
| 1685 | gfs2_consist_inode(dip); |
| 1686 | return -EIO; |
| 1687 | } |
| 1688 | if (IS_ERR(dent)) { |
| 1689 | gfs2_consist_inode(dip); |
| 1690 | return PTR_ERR(dent); |
| 1691 | } |
| 1692 | /* If not first in block, adjust pointers accordingly */ |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 1693 | if (gfs2_dirent_find(dent, name, NULL) == 0) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1694 | prev = dent; |
| 1695 | dent = (struct gfs2_dirent *)((char *)dent + be16_to_cpu(prev->de_rec_len)); |
| 1696 | } |
| 1697 | |
| 1698 | dirent_del(dip, bh, prev, dent); |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1699 | if (dip->i_diskflags & GFS2_DIF_EXHASH) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1700 | struct gfs2_leaf *leaf = (struct gfs2_leaf *)bh->b_data; |
| 1701 | u16 entries = be16_to_cpu(leaf->lf_entries); |
| 1702 | if (!entries) |
| 1703 | gfs2_consist_inode(dip); |
| 1704 | leaf->lf_entries = cpu_to_be16(--entries); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1705 | } |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 1706 | brelse(bh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1707 | |
| 1708 | error = gfs2_meta_inode_buffer(dip, &bh); |
| 1709 | if (error) |
| 1710 | return error; |
| 1711 | |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1712 | if (!dip->i_entries) |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1713 | gfs2_consist_inode(dip); |
| 1714 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
Steven Whitehouse | ad6203f | 2008-11-03 13:59:19 +0000 | [diff] [blame] | 1715 | dip->i_entries--; |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1716 | dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | 855d23c | 2011-05-09 16:42:37 +0100 | [diff] [blame] | 1717 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 1718 | drop_nlink(&dip->i_inode); |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1719 | gfs2_dinode_out(dip, bh->b_data); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1720 | brelse(bh); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1721 | mark_inode_dirty(&dip->i_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1722 | |
| 1723 | return error; |
| 1724 | } |
| 1725 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1726 | /** |
| 1727 | * gfs2_dir_mvino - Change inode number of directory entry |
| 1728 | * @dip: The GFS2 inode |
| 1729 | * @filename: |
| 1730 | * @new_inode: |
| 1731 | * |
| 1732 | * This routine changes the inode number of a directory entry. It's used |
| 1733 | * by rename to change ".." when a directory is moved. |
| 1734 | * Assumes a glock is held on dvp. |
| 1735 | * |
| 1736 | * Returns: errno |
| 1737 | */ |
| 1738 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1739 | int gfs2_dir_mvino(struct gfs2_inode *dip, const struct qstr *filename, |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1740 | const struct gfs2_inode *nip, unsigned int new_type) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1741 | { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1742 | struct buffer_head *bh; |
| 1743 | struct gfs2_dirent *dent; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1744 | int error; |
| 1745 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1746 | dent = gfs2_dirent_search(&dip->i_inode, filename, gfs2_dirent_find, &bh); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1747 | if (!dent) { |
| 1748 | gfs2_consist_inode(dip); |
| 1749 | return -EIO; |
| 1750 | } |
| 1751 | if (IS_ERR(dent)) |
| 1752 | return PTR_ERR(dent); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1753 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1754 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
Steven Whitehouse | dbb7cae | 2007-05-15 15:37:50 +0100 | [diff] [blame] | 1755 | gfs2_inum_out(nip, dent); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1756 | dent->de_type = cpu_to_be16(new_type); |
| 1757 | |
Steven Whitehouse | 383f01f | 2008-11-04 10:05:22 +0000 | [diff] [blame] | 1758 | if (dip->i_diskflags & GFS2_DIF_EXHASH) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1759 | brelse(bh); |
| 1760 | error = gfs2_meta_inode_buffer(dip, &bh); |
| 1761 | if (error) |
| 1762 | return error; |
| 1763 | gfs2_trans_add_bh(dip->i_gl, bh, 1); |
| 1764 | } |
| 1765 | |
Steven Whitehouse | 4bd91ba | 2007-06-05 09:39:18 +0100 | [diff] [blame] | 1766 | dip->i_inode.i_mtime = dip->i_inode.i_ctime = CURRENT_TIME; |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1767 | gfs2_dinode_out(dip, bh->b_data); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1768 | brelse(bh); |
| 1769 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1773 | * leaf_dealloc - Deallocate a directory leaf |
| 1774 | * @dip: the directory |
| 1775 | * @index: the hash table offset in the directory |
| 1776 | * @len: the number of pointers to this leaf |
| 1777 | * @leaf_no: the leaf number |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1778 | * @leaf_bh: buffer_head for the starting leaf |
Bob Peterson | d24a7a4 | 2011-03-22 13:54:03 -0400 | [diff] [blame] | 1779 | * last_dealloc: 1 if this is the final dealloc for the leaf, else 0 |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1780 | * |
| 1781 | * Returns: errno |
| 1782 | */ |
| 1783 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1784 | static int leaf_dealloc(struct gfs2_inode *dip, u32 index, u32 len, |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1785 | u64 leaf_no, struct buffer_head *leaf_bh, |
| 1786 | int last_dealloc) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1787 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 1788 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1789 | struct gfs2_leaf *tmp_leaf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1790 | struct gfs2_rgrp_list rlist; |
| 1791 | struct buffer_head *bh, *dibh; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1792 | u64 blk, nblk; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1793 | unsigned int rg_blocks = 0, l_blocks = 0; |
| 1794 | char *ht; |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1795 | unsigned int x, size = len * sizeof(u64); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1796 | int error; |
| 1797 | |
| 1798 | memset(&rlist, 0, sizeof(struct gfs2_rgrp_list)); |
| 1799 | |
Josef Bacik | 16c5f06 | 2008-04-09 09:33:41 -0400 | [diff] [blame] | 1800 | ht = kzalloc(size, GFP_NOFS); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1801 | if (!ht) |
| 1802 | return -ENOMEM; |
| 1803 | |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1804 | if (!gfs2_alloc_get(dip)) { |
| 1805 | error = -ENOMEM; |
| 1806 | goto out; |
| 1807 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1808 | |
| 1809 | error = gfs2_quota_hold(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 1810 | if (error) |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1811 | goto out_put; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1812 | |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1813 | error = gfs2_rindex_hold(sdp, &dip->i_alloc->al_ri_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1814 | if (error) |
| 1815 | goto out_qs; |
| 1816 | |
| 1817 | /* Count the number of leaves */ |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1818 | bh = leaf_bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1819 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1820 | for (blk = leaf_no; blk; blk = nblk) { |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1821 | if (blk != leaf_no) { |
| 1822 | error = get_leaf(dip, blk, &bh); |
| 1823 | if (error) |
| 1824 | goto out_rlist; |
| 1825 | } |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1826 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
| 1827 | nblk = be64_to_cpu(tmp_leaf->lf_next); |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1828 | if (blk != leaf_no) |
| 1829 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1830 | |
| 1831 | gfs2_rlist_add(sdp, &rlist, blk); |
| 1832 | l_blocks++; |
| 1833 | } |
| 1834 | |
Bob Peterson | fe6c991 | 2008-01-28 11:13:02 -0600 | [diff] [blame] | 1835 | gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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; |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 1840 | rg_blocks += rgd->rd_length; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1853 | bh = leaf_bh; |
| 1854 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1855 | for (blk = leaf_no; blk; blk = nblk) { |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1856 | if (blk != leaf_no) { |
| 1857 | error = get_leaf(dip, blk, &bh); |
| 1858 | if (error) |
| 1859 | goto out_end_trans; |
| 1860 | } |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1861 | tmp_leaf = (struct gfs2_leaf *)bh->b_data; |
| 1862 | nblk = be64_to_cpu(tmp_leaf->lf_next); |
Bob Peterson | ec038c8 | 2011-03-22 13:55:23 -0400 | [diff] [blame] | 1863 | if (blk != leaf_no) |
| 1864 | brelse(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1865 | |
| 1866 | gfs2_free_meta(dip, blk, 1); |
Steven Whitehouse | 77658aa | 2008-02-12 14:17:27 +0000 | [diff] [blame] | 1867 | gfs2_add_inode_blocks(&dip->i_inode, -1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1870 | error = gfs2_dir_write_data(dip, ht, index * sizeof(u64), size); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1871 | if (error != size) { |
| 1872 | if (error >= 0) |
| 1873 | error = -EIO; |
| 1874 | goto out_end_trans; |
| 1875 | } |
| 1876 | |
| 1877 | error = gfs2_meta_inode_buffer(dip, &dibh); |
| 1878 | if (error) |
| 1879 | goto out_end_trans; |
| 1880 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1881 | gfs2_trans_add_bh(dip->i_gl, dibh, 1); |
Bob Peterson | d24a7a4 | 2011-03-22 13:54:03 -0400 | [diff] [blame] | 1882 | /* On the last dealloc, make this a regular file in case we crash. |
| 1883 | (We don't want to free these blocks a second time.) */ |
| 1884 | if (last_dealloc) |
| 1885 | dip->i_inode.i_mode = S_IFREG; |
Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 1886 | gfs2_dinode_out(dip, dibh->b_data); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1887 | brelse(dibh); |
| 1888 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1889 | out_end_trans: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1890 | gfs2_trans_end(sdp); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1891 | out_rg_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1892 | gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1893 | out_rlist: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1894 | gfs2_rlist_free(&rlist); |
Steven Whitehouse | 6dbd822 | 2008-01-10 15:18:55 +0000 | [diff] [blame] | 1895 | gfs2_glock_dq_uninit(&dip->i_alloc->al_ri_gh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1896 | out_qs: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1897 | gfs2_quota_unhold(dip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1898 | out_put: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1899 | gfs2_alloc_put(dip); |
Cyrill Gorcunov | 182fe5a | 2008-03-03 21:54:21 +0300 | [diff] [blame] | 1900 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1901 | kfree(ht); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1902 | return error; |
| 1903 | } |
| 1904 | |
| 1905 | /** |
| 1906 | * gfs2_dir_exhash_dealloc - free all the leaf blocks in a directory |
| 1907 | * @dip: the directory |
| 1908 | * |
| 1909 | * Dealloc all on-disk directory leaves to FREEMETA state |
| 1910 | * Change on-disk inode type to "regular file" |
| 1911 | * |
| 1912 | * Returns: errno |
| 1913 | */ |
| 1914 | |
| 1915 | int gfs2_dir_exhash_dealloc(struct gfs2_inode *dip) |
| 1916 | { |
Bob Peterson | 556bb17 | 2011-03-22 13:56:37 -0400 | [diff] [blame] | 1917 | struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); |
| 1918 | struct buffer_head *bh; |
| 1919 | struct gfs2_leaf *leaf; |
| 1920 | u32 hsize, len; |
| 1921 | u32 ht_offset, lp_offset, ht_offset_cur = -1; |
| 1922 | u32 index = 0, next_index; |
| 1923 | __be64 *lp; |
| 1924 | u64 leaf_no; |
| 1925 | int error = 0, last; |
| 1926 | |
| 1927 | hsize = 1 << dip->i_depth; |
| 1928 | if (hsize * sizeof(u64) != i_size_read(&dip->i_inode)) { |
| 1929 | gfs2_consist_inode(dip); |
| 1930 | return -EIO; |
| 1931 | } |
| 1932 | |
| 1933 | lp = kmalloc(sdp->sd_hash_bsize, GFP_NOFS); |
| 1934 | if (!lp) |
| 1935 | return -ENOMEM; |
| 1936 | |
| 1937 | while (index < hsize) { |
| 1938 | lp_offset = index & (sdp->sd_hash_ptrs - 1); |
| 1939 | ht_offset = index - lp_offset; |
| 1940 | |
| 1941 | if (ht_offset_cur != ht_offset) { |
| 1942 | error = gfs2_dir_read_data(dip, (char *)lp, |
| 1943 | ht_offset * sizeof(__be64), |
| 1944 | sdp->sd_hash_bsize, 1); |
| 1945 | if (error != sdp->sd_hash_bsize) { |
| 1946 | if (error >= 0) |
| 1947 | error = -EIO; |
| 1948 | goto out; |
| 1949 | } |
| 1950 | ht_offset_cur = ht_offset; |
| 1951 | } |
| 1952 | |
| 1953 | leaf_no = be64_to_cpu(lp[lp_offset]); |
| 1954 | if (leaf_no) { |
| 1955 | error = get_leaf(dip, leaf_no, &bh); |
| 1956 | if (error) |
| 1957 | goto out; |
| 1958 | leaf = (struct gfs2_leaf *)bh->b_data; |
| 1959 | len = 1 << (dip->i_depth - be16_to_cpu(leaf->lf_depth)); |
| 1960 | |
| 1961 | next_index = (index & ~(len - 1)) + len; |
| 1962 | last = ((next_index >= hsize) ? 1 : 0); |
| 1963 | error = leaf_dealloc(dip, index, len, leaf_no, bh, |
| 1964 | last); |
| 1965 | brelse(bh); |
| 1966 | if (error) |
| 1967 | goto out; |
| 1968 | index = next_index; |
| 1969 | } else |
| 1970 | index++; |
| 1971 | } |
| 1972 | |
| 1973 | if (index != hsize) { |
| 1974 | gfs2_consist_inode(dip); |
| 1975 | error = -EIO; |
| 1976 | } |
| 1977 | |
| 1978 | out: |
| 1979 | kfree(lp); |
| 1980 | |
| 1981 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /** |
| 1985 | * gfs2_diradd_alloc_required - find if adding entry will require an allocation |
| 1986 | * @ip: the file being written to |
| 1987 | * @filname: the filename that's going to be added |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1988 | * |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1989 | * Returns: 1 if alloc required, 0 if not, -ve on error |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1990 | */ |
| 1991 | |
Steven Whitehouse | 4d8012b | 2006-04-12 17:39:45 -0400 | [diff] [blame] | 1992 | int gfs2_diradd_alloc_required(struct inode *inode, const struct qstr *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1993 | { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1994 | struct gfs2_dirent *dent; |
| 1995 | struct buffer_head *bh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1996 | |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1997 | dent = gfs2_dirent_search(inode, name, gfs2_dirent_find_space, &bh); |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 1998 | if (!dent) { |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 1999 | return 1; |
Steven Whitehouse | ed38650 | 2006-04-07 16:28:07 -0400 | [diff] [blame] | 2000 | } |
Steven Whitehouse | c752666 | 2006-03-20 12:30:04 -0500 | [diff] [blame] | 2001 | if (IS_ERR(dent)) |
| 2002 | return PTR_ERR(dent); |
| 2003 | brelse(bh); |
| 2004 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |