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