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