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