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