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