Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- mode: c; c-basic-offset: 8; -*- |
| 2 | * vim: noexpandtab sw=8 ts=8 sts=0: |
| 3 | * |
| 4 | * dir.c |
| 5 | * |
| 6 | * Creates, reads, walks and deletes directory-nodes |
| 7 | * |
| 8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. |
| 9 | * |
| 10 | * Portions of this code from linux/fs/ext3/dir.c |
| 11 | * |
| 12 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 13 | * Remy Card (card@masi.ibp.fr) |
| 14 | * Laboratoire MASI - Institut Blaise pascal |
| 15 | * Universite Pierre et Marie Curie (Paris VI) |
| 16 | * |
| 17 | * from |
| 18 | * |
| 19 | * linux/fs/minix/dir.c |
| 20 | * |
Jakub Wilk | 762515a | 2015-04-14 15:43:41 -0700 | [diff] [blame] | 21 | * Copyright (C) 1991, 1992 Linus Torvalds |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 22 | * |
| 23 | * This program is free software; you can redistribute it and/or |
| 24 | * modify it under the terms of the GNU General Public |
| 25 | * License as published by the Free Software Foundation; either |
| 26 | * version 2 of the License, or (at your option) any later version. |
| 27 | * |
| 28 | * This program is distributed in the hope that it will be useful, |
| 29 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 31 | * General Public License for more details. |
| 32 | * |
| 33 | * You should have received a copy of the GNU General Public |
| 34 | * License along with this program; if not, write to the |
| 35 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 36 | * Boston, MA 021110-1307, USA. |
| 37 | */ |
| 38 | |
| 39 | #include <linux/fs.h> |
| 40 | #include <linux/types.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/highmem.h> |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 43 | #include <linux/quotaops.h> |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 44 | #include <linux/sort.h> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 45 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 46 | #include <cluster/masklog.h> |
| 47 | |
| 48 | #include "ocfs2.h" |
| 49 | |
| 50 | #include "alloc.h" |
Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 51 | #include "blockcheck.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 52 | #include "dir.h" |
| 53 | #include "dlmglue.h" |
| 54 | #include "extent_map.h" |
| 55 | #include "file.h" |
| 56 | #include "inode.h" |
| 57 | #include "journal.h" |
| 58 | #include "namei.h" |
| 59 | #include "suballoc.h" |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 60 | #include "super.h" |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 61 | #include "sysfile.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 62 | #include "uptodate.h" |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 63 | #include "ocfs2_trace.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 64 | |
| 65 | #include "buffer_head_io.h" |
| 66 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 67 | #define NAMEI_RA_CHUNKS 2 |
| 68 | #define NAMEI_RA_BLOCKS 4 |
| 69 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 70 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 71 | static unsigned char ocfs2_filetype_table[] = { |
| 72 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 73 | }; |
| 74 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 75 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 76 | handle_t *handle, |
| 77 | struct inode *dir, |
| 78 | struct buffer_head *parent_fe_bh, |
| 79 | struct ocfs2_alloc_context *data_ac, |
| 80 | struct ocfs2_alloc_context *meta_ac, |
| 81 | struct buffer_head **new_bh); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 82 | static int ocfs2_dir_indexed(struct inode *inode); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 83 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 84 | /* |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 85 | * These are distinct checks because future versions of the file system will |
| 86 | * want to have a trailing dirent structure independent of indexing. |
| 87 | */ |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 88 | static int ocfs2_supports_dir_trailer(struct inode *dir) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 89 | { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 90 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 91 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 92 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 93 | return 0; |
| 94 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 95 | return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 98 | /* |
| 99 | * "new' here refers to the point at which we're creating a new |
| 100 | * directory via "mkdir()", but also when we're expanding an inline |
| 101 | * directory. In either case, we don't yet have the indexing bit set |
| 102 | * on the directory, so the standard checks will fail in when metaecc |
| 103 | * is turned off. Only directory-initialization type functions should |
| 104 | * use this then. Everything else wants ocfs2_supports_dir_trailer() |
| 105 | */ |
| 106 | static int ocfs2_new_dir_wants_trailer(struct inode *dir) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 107 | { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 108 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 109 | |
| 110 | return ocfs2_meta_ecc(osb) || |
| 111 | ocfs2_supports_indexed_dirs(osb); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb) |
| 115 | { |
| 116 | return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer); |
| 117 | } |
| 118 | |
| 119 | #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb)))) |
| 120 | |
Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 121 | /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make |
| 122 | * them more consistent? */ |
| 123 | struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize, |
| 124 | void *data) |
| 125 | { |
| 126 | char *p = data; |
| 127 | |
| 128 | p += blocksize - sizeof(struct ocfs2_dir_block_trailer); |
| 129 | return (struct ocfs2_dir_block_trailer *)p; |
| 130 | } |
| 131 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 132 | /* |
| 133 | * XXX: This is executed once on every dirent. We should consider optimizing |
| 134 | * it. |
| 135 | */ |
| 136 | static int ocfs2_skip_dir_trailer(struct inode *dir, |
| 137 | struct ocfs2_dir_entry *de, |
| 138 | unsigned long offset, |
| 139 | unsigned long blklen) |
| 140 | { |
| 141 | unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer); |
| 142 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 143 | if (!ocfs2_supports_dir_trailer(dir)) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 144 | return 0; |
| 145 | |
| 146 | if (offset != toff) |
| 147 | return 0; |
| 148 | |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | static void ocfs2_init_dir_trailer(struct inode *inode, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 153 | struct buffer_head *bh, u16 rec_len) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 154 | { |
| 155 | struct ocfs2_dir_block_trailer *trailer; |
| 156 | |
| 157 | trailer = ocfs2_trailer_from_bh(bh, inode->i_sb); |
| 158 | strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE); |
| 159 | trailer->db_compat_rec_len = |
| 160 | cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer)); |
| 161 | trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); |
| 162 | trailer->db_blkno = cpu_to_le64(bh->b_blocknr); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 163 | trailer->db_free_rec_len = cpu_to_le16(rec_len); |
| 164 | } |
| 165 | /* |
| 166 | * Link an unindexed block with a dir trailer structure into the index free |
| 167 | * list. This function will modify dirdata_bh, but assumes you've already |
| 168 | * passed it to the journal. |
| 169 | */ |
| 170 | static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle, |
| 171 | struct buffer_head *dx_root_bh, |
| 172 | struct buffer_head *dirdata_bh) |
| 173 | { |
| 174 | int ret; |
| 175 | struct ocfs2_dx_root_block *dx_root; |
| 176 | struct ocfs2_dir_block_trailer *trailer; |
| 177 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 178 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 179 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 180 | if (ret) { |
| 181 | mlog_errno(ret); |
| 182 | goto out; |
| 183 | } |
| 184 | trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); |
| 185 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 186 | |
| 187 | trailer->db_free_next = dx_root->dr_free_blk; |
| 188 | dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); |
| 189 | |
| 190 | ocfs2_journal_dirty(handle, dx_root_bh); |
| 191 | |
| 192 | out: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res) |
| 197 | { |
| 198 | return res->dl_prev_leaf_bh == NULL; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 201 | void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res) |
| 202 | { |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 203 | brelse(res->dl_dx_root_bh); |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 204 | brelse(res->dl_leaf_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 205 | brelse(res->dl_dx_leaf_bh); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 206 | brelse(res->dl_prev_leaf_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static int ocfs2_dir_indexed(struct inode *inode) |
| 210 | { |
| 211 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL) |
| 212 | return 1; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 216 | static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root) |
| 217 | { |
| 218 | return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE; |
| 219 | } |
| 220 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 221 | /* |
| 222 | * Hashing code adapted from ext3 |
| 223 | */ |
| 224 | #define DELTA 0x9E3779B9 |
| 225 | |
| 226 | static void TEA_transform(__u32 buf[4], __u32 const in[]) |
| 227 | { |
| 228 | __u32 sum = 0; |
| 229 | __u32 b0 = buf[0], b1 = buf[1]; |
| 230 | __u32 a = in[0], b = in[1], c = in[2], d = in[3]; |
| 231 | int n = 16; |
| 232 | |
| 233 | do { |
| 234 | sum += DELTA; |
| 235 | b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); |
| 236 | b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); |
| 237 | } while (--n); |
| 238 | |
| 239 | buf[0] += b0; |
| 240 | buf[1] += b1; |
| 241 | } |
| 242 | |
| 243 | static void str2hashbuf(const char *msg, int len, __u32 *buf, int num) |
| 244 | { |
| 245 | __u32 pad, val; |
| 246 | int i; |
| 247 | |
| 248 | pad = (__u32)len | ((__u32)len << 8); |
| 249 | pad |= pad << 16; |
| 250 | |
| 251 | val = pad; |
| 252 | if (len > num*4) |
| 253 | len = num * 4; |
| 254 | for (i = 0; i < len; i++) { |
| 255 | if ((i % 4) == 0) |
| 256 | val = pad; |
| 257 | val = msg[i] + (val << 8); |
| 258 | if ((i % 4) == 3) { |
| 259 | *buf++ = val; |
| 260 | val = pad; |
| 261 | num--; |
| 262 | } |
| 263 | } |
| 264 | if (--num >= 0) |
| 265 | *buf++ = val; |
| 266 | while (--num >= 0) |
| 267 | *buf++ = pad; |
| 268 | } |
| 269 | |
| 270 | static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len, |
| 271 | struct ocfs2_dx_hinfo *hinfo) |
| 272 | { |
| 273 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 274 | const char *p; |
| 275 | __u32 in[8], buf[4]; |
| 276 | |
| 277 | /* |
| 278 | * XXX: Is this really necessary, if the index is never looked |
| 279 | * at by readdir? Is a hash value of '0' a bad idea? |
| 280 | */ |
| 281 | if ((len == 1 && !strncmp(".", name, 1)) || |
| 282 | (len == 2 && !strncmp("..", name, 2))) { |
| 283 | buf[0] = buf[1] = 0; |
| 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | #ifdef OCFS2_DEBUG_DX_DIRS |
| 288 | /* |
| 289 | * This makes it very easy to debug indexing problems. We |
| 290 | * should never allow this to be selected without hand editing |
| 291 | * this file though. |
| 292 | */ |
| 293 | buf[0] = buf[1] = len; |
| 294 | goto out; |
| 295 | #endif |
| 296 | |
| 297 | memcpy(buf, osb->osb_dx_seed, sizeof(buf)); |
| 298 | |
| 299 | p = name; |
| 300 | while (len > 0) { |
| 301 | str2hashbuf(p, len, in, 4); |
| 302 | TEA_transform(buf, in); |
| 303 | len -= 16; |
| 304 | p += 16; |
| 305 | } |
| 306 | |
| 307 | out: |
| 308 | hinfo->major_hash = buf[0]; |
| 309 | hinfo->minor_hash = buf[1]; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 312 | /* |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 313 | * bh passed here can be an inode block or a dir data block, depending |
| 314 | * on the inode inline data flag. |
| 315 | */ |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 316 | static int ocfs2_check_dir_entry(struct inode * dir, |
| 317 | struct ocfs2_dir_entry * de, |
| 318 | struct buffer_head * bh, |
| 319 | unsigned long offset) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 320 | { |
| 321 | const char *error_msg = NULL; |
| 322 | const int rlen = le16_to_cpu(de->rec_len); |
| 323 | |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 324 | if (unlikely(rlen < OCFS2_DIR_REC_LEN(1))) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 325 | error_msg = "rec_len is smaller than minimal"; |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 326 | else if (unlikely(rlen % 4 != 0)) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 327 | error_msg = "rec_len % 4 != 0"; |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 328 | else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len))) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 329 | error_msg = "rec_len is too small for name_len"; |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 330 | else if (unlikely( |
| 331 | ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 332 | error_msg = "directory entry across blocks"; |
| 333 | |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 334 | if (unlikely(error_msg != NULL)) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 335 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " |
| 336 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", |
| 337 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, |
| 338 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, |
| 339 | de->name_len); |
Tao Ma | 1dd9ffc | 2011-01-24 23:23:30 +0800 | [diff] [blame] | 340 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 341 | return error_msg == NULL ? 1 : 0; |
| 342 | } |
| 343 | |
| 344 | static inline int ocfs2_match(int len, |
| 345 | const char * const name, |
| 346 | struct ocfs2_dir_entry *de) |
| 347 | { |
| 348 | if (len != de->name_len) |
| 349 | return 0; |
| 350 | if (!de->inode) |
| 351 | return 0; |
| 352 | return !memcmp(name, de->name, len); |
| 353 | } |
| 354 | |
| 355 | /* |
| 356 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 357 | */ |
Jesper Juhl | 42b16b3 | 2011-01-17 00:09:38 +0100 | [diff] [blame] | 358 | static inline int ocfs2_search_dirblock(struct buffer_head *bh, |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 359 | struct inode *dir, |
| 360 | const char *name, int namelen, |
| 361 | unsigned long offset, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 362 | char *first_de, |
| 363 | unsigned int bytes, |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 364 | struct ocfs2_dir_entry **res_dir) |
| 365 | { |
| 366 | struct ocfs2_dir_entry *de; |
| 367 | char *dlimit, *de_buf; |
| 368 | int de_len; |
| 369 | int ret = 0; |
| 370 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 371 | de_buf = first_de; |
| 372 | dlimit = de_buf + bytes; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 373 | |
| 374 | while (de_buf < dlimit) { |
| 375 | /* this code is executed quadratically often */ |
| 376 | /* do minimal checking `by hand' */ |
| 377 | |
| 378 | de = (struct ocfs2_dir_entry *) de_buf; |
| 379 | |
| 380 | if (de_buf + namelen <= dlimit && |
| 381 | ocfs2_match(namelen, name, de)) { |
| 382 | /* found a match - just to be sure, do a full check */ |
| 383 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 384 | ret = -1; |
| 385 | goto bail; |
| 386 | } |
| 387 | *res_dir = de; |
| 388 | ret = 1; |
| 389 | goto bail; |
| 390 | } |
| 391 | |
| 392 | /* prevent looping on a bad block */ |
| 393 | de_len = le16_to_cpu(de->rec_len); |
| 394 | if (de_len <= 0) { |
| 395 | ret = -1; |
| 396 | goto bail; |
| 397 | } |
| 398 | |
| 399 | de_buf += de_len; |
| 400 | offset += de_len; |
| 401 | } |
| 402 | |
| 403 | bail: |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 404 | trace_ocfs2_search_dirblock(ret); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 408 | static struct buffer_head *ocfs2_find_entry_id(const char *name, |
| 409 | int namelen, |
| 410 | struct inode *dir, |
| 411 | struct ocfs2_dir_entry **res_dir) |
| 412 | { |
| 413 | int ret, found; |
| 414 | struct buffer_head *di_bh = NULL; |
| 415 | struct ocfs2_dinode *di; |
| 416 | struct ocfs2_inline_data *data; |
| 417 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 418 | ret = ocfs2_read_inode_block(dir, &di_bh); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 419 | if (ret) { |
| 420 | mlog_errno(ret); |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 425 | data = &di->id2.i_data; |
| 426 | |
| 427 | found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, |
| 428 | data->id_data, i_size_read(dir), res_dir); |
| 429 | if (found == 1) |
| 430 | return di_bh; |
| 431 | |
| 432 | brelse(di_bh); |
| 433 | out: |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 437 | static int ocfs2_validate_dir_block(struct super_block *sb, |
| 438 | struct buffer_head *bh) |
| 439 | { |
Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 440 | int rc; |
| 441 | struct ocfs2_dir_block_trailer *trailer = |
| 442 | ocfs2_trailer_from_bh(bh, sb); |
| 443 | |
| 444 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 445 | /* |
Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 446 | * We don't validate dirents here, that's handled |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 447 | * in-place when the code walks them. |
| 448 | */ |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 449 | trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr); |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 450 | |
Joel Becker | c175a51 | 2008-12-10 17:58:22 -0800 | [diff] [blame] | 451 | BUG_ON(!buffer_uptodate(bh)); |
| 452 | |
| 453 | /* |
| 454 | * If the ecc fails, we return the error but otherwise |
| 455 | * leave the filesystem running. We know any error is |
| 456 | * local to this block. |
| 457 | * |
| 458 | * Note that we are safe to call this even if the directory |
| 459 | * doesn't have a trailer. Filesystems without metaecc will do |
| 460 | * nothing, and filesystems with it will have one. |
| 461 | */ |
| 462 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check); |
| 463 | if (rc) |
| 464 | mlog(ML_ERROR, "Checksum failed for dinode %llu\n", |
| 465 | (unsigned long long)bh->b_blocknr); |
| 466 | |
| 467 | return rc; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | /* |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 471 | * Validate a directory trailer. |
| 472 | * |
| 473 | * We check the trailer here rather than in ocfs2_validate_dir_block() |
| 474 | * because that function doesn't have the inode to test. |
| 475 | */ |
| 476 | static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh) |
| 477 | { |
| 478 | int rc = 0; |
| 479 | struct ocfs2_dir_block_trailer *trailer; |
| 480 | |
| 481 | trailer = ocfs2_trailer_from_bh(bh, dir->i_sb); |
| 482 | if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 483 | rc = ocfs2_error(dir->i_sb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 484 | "Invalid dirblock #%llu: " |
| 485 | "signature = %.*s\n", |
| 486 | (unsigned long long)bh->b_blocknr, 7, |
| 487 | trailer->db_signature); |
| 488 | goto out; |
| 489 | } |
| 490 | if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 491 | rc = ocfs2_error(dir->i_sb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 492 | "Directory block #%llu has an invalid " |
| 493 | "db_blkno of %llu", |
| 494 | (unsigned long long)bh->b_blocknr, |
| 495 | (unsigned long long)le64_to_cpu(trailer->db_blkno)); |
| 496 | goto out; |
| 497 | } |
| 498 | if (le64_to_cpu(trailer->db_parent_dinode) != |
| 499 | OCFS2_I(dir)->ip_blkno) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 500 | rc = ocfs2_error(dir->i_sb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 501 | "Directory block #%llu on dinode " |
| 502 | "#%llu has an invalid parent_dinode " |
| 503 | "of %llu", |
| 504 | (unsigned long long)bh->b_blocknr, |
| 505 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 506 | (unsigned long long)le64_to_cpu(trailer->db_blkno)); |
| 507 | goto out; |
| 508 | } |
| 509 | out: |
| 510 | return rc; |
| 511 | } |
| 512 | |
| 513 | /* |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 514 | * This function forces all errors to -EIO for consistency with its |
| 515 | * predecessor, ocfs2_bread(). We haven't audited what returning the |
| 516 | * real error codes would do to callers. We log the real codes with |
| 517 | * mlog_errno() before we squash them. |
| 518 | */ |
| 519 | static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, |
| 520 | struct buffer_head **bh, int flags) |
| 521 | { |
| 522 | int rc = 0; |
| 523 | struct buffer_head *tmp = *bh; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 524 | |
Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 525 | rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, |
| 526 | ocfs2_validate_dir_block); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 527 | if (rc) { |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 528 | mlog_errno(rc); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 529 | goto out; |
| 530 | } |
| 531 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 532 | if (!(flags & OCFS2_BH_READAHEAD) && |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 533 | ocfs2_supports_dir_trailer(inode)) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 534 | rc = ocfs2_check_dir_trailer(inode, tmp); |
| 535 | if (rc) { |
| 536 | if (!*bh) |
| 537 | brelse(tmp); |
| 538 | mlog_errno(rc); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 539 | goto out; |
| 540 | } |
| 541 | } |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 542 | |
Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 543 | /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 544 | if (!*bh) |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 545 | *bh = tmp; |
| 546 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 547 | out: |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 548 | return rc ? -EIO : 0; |
| 549 | } |
| 550 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 551 | /* |
| 552 | * Read the block at 'phys' which belongs to this directory |
| 553 | * inode. This function does no virtual->physical block translation - |
| 554 | * what's passed in is assumed to be a valid directory block. |
| 555 | */ |
| 556 | static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys, |
| 557 | struct buffer_head **bh) |
| 558 | { |
| 559 | int ret; |
| 560 | struct buffer_head *tmp = *bh; |
| 561 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 562 | ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp, |
| 563 | ocfs2_validate_dir_block); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 564 | if (ret) { |
| 565 | mlog_errno(ret); |
| 566 | goto out; |
| 567 | } |
| 568 | |
| 569 | if (ocfs2_supports_dir_trailer(dir)) { |
| 570 | ret = ocfs2_check_dir_trailer(dir, tmp); |
| 571 | if (ret) { |
| 572 | if (!*bh) |
| 573 | brelse(tmp); |
| 574 | mlog_errno(ret); |
| 575 | goto out; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (!ret && !*bh) |
| 580 | *bh = tmp; |
| 581 | out: |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static int ocfs2_validate_dx_root(struct super_block *sb, |
| 586 | struct buffer_head *bh) |
| 587 | { |
| 588 | int ret; |
| 589 | struct ocfs2_dx_root_block *dx_root; |
| 590 | |
| 591 | BUG_ON(!buffer_uptodate(bh)); |
| 592 | |
| 593 | dx_root = (struct ocfs2_dx_root_block *) bh->b_data; |
| 594 | |
| 595 | ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check); |
| 596 | if (ret) { |
| 597 | mlog(ML_ERROR, |
| 598 | "Checksum failed for dir index root block %llu\n", |
| 599 | (unsigned long long)bh->b_blocknr); |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 604 | ret = ocfs2_error(sb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 605 | "Dir Index Root # %llu has bad signature %.*s", |
| 606 | (unsigned long long)le64_to_cpu(dx_root->dr_blkno), |
| 607 | 7, dx_root->dr_signature); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 610 | return ret; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di, |
| 614 | struct buffer_head **dx_root_bh) |
| 615 | { |
| 616 | int ret; |
| 617 | u64 blkno = le64_to_cpu(di->i_dx_root); |
| 618 | struct buffer_head *tmp = *dx_root_bh; |
| 619 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 620 | ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, |
| 621 | ocfs2_validate_dx_root); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 622 | |
| 623 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 624 | if (!ret && !*dx_root_bh) |
| 625 | *dx_root_bh = tmp; |
| 626 | |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static int ocfs2_validate_dx_leaf(struct super_block *sb, |
| 631 | struct buffer_head *bh) |
| 632 | { |
| 633 | int ret; |
| 634 | struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data; |
| 635 | |
| 636 | BUG_ON(!buffer_uptodate(bh)); |
| 637 | |
| 638 | ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check); |
| 639 | if (ret) { |
| 640 | mlog(ML_ERROR, |
| 641 | "Checksum failed for dir index leaf block %llu\n", |
| 642 | (unsigned long long)bh->b_blocknr); |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 647 | ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s", |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 648 | 7, dx_leaf->dl_signature); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 651 | return ret; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno, |
| 655 | struct buffer_head **dx_leaf_bh) |
| 656 | { |
| 657 | int ret; |
| 658 | struct buffer_head *tmp = *dx_leaf_bh; |
| 659 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 660 | ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp, |
| 661 | ocfs2_validate_dx_leaf); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 662 | |
| 663 | /* If ocfs2_read_block() got us a new bh, pass it up. */ |
| 664 | if (!ret && !*dx_leaf_bh) |
| 665 | *dx_leaf_bh = tmp; |
| 666 | |
| 667 | return ret; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Read a series of dx_leaf blocks. This expects all buffer_head |
| 672 | * pointers to be NULL on function entry. |
| 673 | */ |
| 674 | static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num, |
| 675 | struct buffer_head **dx_leaf_bhs) |
| 676 | { |
| 677 | int ret; |
| 678 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 679 | ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 680 | ocfs2_validate_dx_leaf); |
| 681 | if (ret) |
| 682 | mlog_errno(ret); |
| 683 | |
| 684 | return ret; |
| 685 | } |
| 686 | |
Adrian Bunk | 0af4bd3 | 2007-10-24 18:23:27 +0200 | [diff] [blame] | 687 | static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, |
| 688 | struct inode *dir, |
| 689 | struct ocfs2_dir_entry **res_dir) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 690 | { |
| 691 | struct super_block *sb; |
| 692 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 693 | struct buffer_head *bh, *ret = NULL; |
| 694 | unsigned long start, block, b; |
| 695 | int ra_max = 0; /* Number of bh's in the readahead |
| 696 | buffer, bh_use[] */ |
| 697 | int ra_ptr = 0; /* Current index into readahead |
| 698 | buffer */ |
| 699 | int num = 0; |
| 700 | int nblocks, i, err; |
| 701 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 702 | sb = dir->i_sb; |
| 703 | |
| 704 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 705 | start = OCFS2_I(dir)->ip_dir_start_lookup; |
| 706 | if (start >= nblocks) |
| 707 | start = 0; |
| 708 | block = start; |
| 709 | |
| 710 | restart: |
| 711 | do { |
| 712 | /* |
| 713 | * We deal with the read-ahead logic here. |
| 714 | */ |
| 715 | if (ra_ptr >= ra_max) { |
| 716 | /* Refill the readahead buffer */ |
| 717 | ra_ptr = 0; |
| 718 | b = block; |
| 719 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 720 | /* |
| 721 | * Terminate if we reach the end of the |
| 722 | * directory and must wrap, or if our |
| 723 | * search has finished at this block. |
| 724 | */ |
| 725 | if (b >= nblocks || (num && block == start)) { |
| 726 | bh_use[ra_max] = NULL; |
| 727 | break; |
| 728 | } |
| 729 | num++; |
| 730 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 731 | bh = NULL; |
| 732 | err = ocfs2_read_dir_block(dir, b++, &bh, |
| 733 | OCFS2_BH_READAHEAD); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 734 | bh_use[ra_max] = bh; |
| 735 | } |
| 736 | } |
| 737 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 738 | goto next; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 739 | if (ocfs2_read_dir_block(dir, block, &bh, 0)) { |
Joel Becker | 5e0b3de | 2008-10-09 17:20:33 -0700 | [diff] [blame] | 740 | /* read error, skip block & hope for the best. |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 741 | * ocfs2_read_dir_block() has released the bh. */ |
jiangyiwen | 61fb9ea | 2014-12-10 15:42:02 -0800 | [diff] [blame] | 742 | mlog(ML_ERROR, "reading directory %llu, " |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 743 | "offset %lu\n", |
| 744 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 745 | block); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 746 | goto next; |
| 747 | } |
| 748 | i = ocfs2_search_dirblock(bh, dir, name, namelen, |
| 749 | block << sb->s_blocksize_bits, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 750 | bh->b_data, sb->s_blocksize, |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 751 | res_dir); |
| 752 | if (i == 1) { |
| 753 | OCFS2_I(dir)->ip_dir_start_lookup = block; |
| 754 | ret = bh; |
| 755 | goto cleanup_and_exit; |
| 756 | } else { |
| 757 | brelse(bh); |
| 758 | if (i < 0) |
| 759 | goto cleanup_and_exit; |
| 760 | } |
| 761 | next: |
| 762 | if (++block >= nblocks) |
| 763 | block = 0; |
| 764 | } while (block != start); |
| 765 | |
| 766 | /* |
| 767 | * If the directory has grown while we were searching, then |
| 768 | * search the last part of the directory before giving up. |
| 769 | */ |
| 770 | block = nblocks; |
| 771 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 772 | if (block < nblocks) { |
| 773 | start = 0; |
| 774 | goto restart; |
| 775 | } |
| 776 | |
| 777 | cleanup_and_exit: |
| 778 | /* Clean up the read-ahead blocks */ |
| 779 | for (; ra_ptr < ra_max; ra_ptr++) |
| 780 | brelse(bh_use[ra_ptr]); |
| 781 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 782 | trace_ocfs2_find_entry_el(ret); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 783 | return ret; |
| 784 | } |
| 785 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 786 | static int ocfs2_dx_dir_lookup_rec(struct inode *inode, |
| 787 | struct ocfs2_extent_list *el, |
| 788 | u32 major_hash, |
| 789 | u32 *ret_cpos, |
| 790 | u64 *ret_phys_blkno, |
| 791 | unsigned int *ret_clen) |
| 792 | { |
| 793 | int ret = 0, i, found; |
| 794 | struct buffer_head *eb_bh = NULL; |
| 795 | struct ocfs2_extent_block *eb; |
| 796 | struct ocfs2_extent_rec *rec = NULL; |
| 797 | |
| 798 | if (el->l_tree_depth) { |
Joel Becker | facdb77 | 2009-02-12 18:08:48 -0800 | [diff] [blame] | 799 | ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash, |
| 800 | &eb_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 801 | if (ret) { |
| 802 | mlog_errno(ret); |
| 803 | goto out; |
| 804 | } |
| 805 | |
| 806 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; |
| 807 | el = &eb->h_list; |
| 808 | |
| 809 | if (el->l_tree_depth) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 810 | ret = ocfs2_error(inode->i_sb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 811 | "Inode %lu has non zero tree depth in " |
| 812 | "btree tree block %llu\n", inode->i_ino, |
| 813 | (unsigned long long)eb_bh->b_blocknr); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 814 | goto out; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | found = 0; |
| 819 | for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) { |
| 820 | rec = &el->l_recs[i]; |
| 821 | |
| 822 | if (le32_to_cpu(rec->e_cpos) <= major_hash) { |
| 823 | found = 1; |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | if (!found) { |
Goldwyn Rodrigues | 17a5b9a | 2015-09-04 15:44:17 -0700 | [diff] [blame^] | 829 | ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent " |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 830 | "record (%u, %u, 0) in btree", inode->i_ino, |
| 831 | le32_to_cpu(rec->e_cpos), |
| 832 | ocfs2_rec_clusters(el, rec)); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 833 | goto out; |
| 834 | } |
| 835 | |
| 836 | if (ret_phys_blkno) |
| 837 | *ret_phys_blkno = le64_to_cpu(rec->e_blkno); |
| 838 | if (ret_cpos) |
| 839 | *ret_cpos = le32_to_cpu(rec->e_cpos); |
| 840 | if (ret_clen) |
| 841 | *ret_clen = le16_to_cpu(rec->e_leaf_clusters); |
| 842 | |
| 843 | out: |
| 844 | brelse(eb_bh); |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Returns the block index, from the start of the cluster which this |
| 850 | * hash belongs too. |
| 851 | */ |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 852 | static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, |
| 853 | u32 minor_hash) |
| 854 | { |
| 855 | return minor_hash & osb->osb_dx_mask; |
| 856 | } |
| 857 | |
| 858 | static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 859 | struct ocfs2_dx_hinfo *hinfo) |
| 860 | { |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 861 | return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | static int ocfs2_dx_dir_lookup(struct inode *inode, |
| 865 | struct ocfs2_extent_list *el, |
| 866 | struct ocfs2_dx_hinfo *hinfo, |
| 867 | u32 *ret_cpos, |
| 868 | u64 *ret_phys_blkno) |
| 869 | { |
| 870 | int ret = 0; |
| 871 | unsigned int cend, uninitialized_var(clen); |
| 872 | u32 uninitialized_var(cpos); |
| 873 | u64 uninitialized_var(blkno); |
| 874 | u32 name_hash = hinfo->major_hash; |
| 875 | |
| 876 | ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno, |
| 877 | &clen); |
| 878 | if (ret) { |
| 879 | mlog_errno(ret); |
| 880 | goto out; |
| 881 | } |
| 882 | |
| 883 | cend = cpos + clen; |
| 884 | if (name_hash >= cend) { |
| 885 | /* We want the last cluster */ |
| 886 | blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1); |
| 887 | cpos += clen - 1; |
| 888 | } else { |
| 889 | blkno += ocfs2_clusters_to_blocks(inode->i_sb, |
| 890 | name_hash - cpos); |
| 891 | cpos = name_hash; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * We now have the cluster which should hold our entry. To |
| 896 | * find the exact block from the start of the cluster to |
| 897 | * search, we take the lower bits of the hash. |
| 898 | */ |
| 899 | blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo); |
| 900 | |
| 901 | if (ret_phys_blkno) |
| 902 | *ret_phys_blkno = blkno; |
| 903 | if (ret_cpos) |
| 904 | *ret_cpos = cpos; |
| 905 | |
| 906 | out: |
| 907 | |
| 908 | return ret; |
| 909 | } |
| 910 | |
| 911 | static int ocfs2_dx_dir_search(const char *name, int namelen, |
| 912 | struct inode *dir, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 913 | struct ocfs2_dx_root_block *dx_root, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 914 | struct ocfs2_dir_lookup_result *res) |
| 915 | { |
| 916 | int ret, i, found; |
| 917 | u64 uninitialized_var(phys); |
| 918 | struct buffer_head *dx_leaf_bh = NULL; |
| 919 | struct ocfs2_dx_leaf *dx_leaf; |
| 920 | struct ocfs2_dx_entry *dx_entry = NULL; |
| 921 | struct buffer_head *dir_ent_bh = NULL; |
| 922 | struct ocfs2_dir_entry *dir_ent = NULL; |
| 923 | struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 924 | struct ocfs2_extent_list *dr_el; |
| 925 | struct ocfs2_dx_entry_list *entry_list; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 926 | |
| 927 | ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo); |
| 928 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 929 | if (ocfs2_dx_root_inline(dx_root)) { |
| 930 | entry_list = &dx_root->dr_entries; |
| 931 | goto search; |
| 932 | } |
| 933 | |
| 934 | dr_el = &dx_root->dr_list; |
| 935 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 936 | ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys); |
| 937 | if (ret) { |
| 938 | mlog_errno(ret); |
| 939 | goto out; |
| 940 | } |
| 941 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 942 | trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 943 | namelen, name, hinfo->major_hash, |
| 944 | hinfo->minor_hash, (unsigned long long)phys); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 945 | |
| 946 | ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh); |
| 947 | if (ret) { |
| 948 | mlog_errno(ret); |
| 949 | goto out; |
| 950 | } |
| 951 | |
| 952 | dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data; |
| 953 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 954 | trace_ocfs2_dx_dir_search_leaf_info( |
| 955 | le16_to_cpu(dx_leaf->dl_list.de_num_used), |
| 956 | le16_to_cpu(dx_leaf->dl_list.de_count)); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 957 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 958 | entry_list = &dx_leaf->dl_list; |
| 959 | |
| 960 | search: |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 961 | /* |
| 962 | * Empty leaf is legal, so no need to check for that. |
| 963 | */ |
| 964 | found = 0; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 965 | for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { |
| 966 | dx_entry = &entry_list->de_entries[i]; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 967 | |
| 968 | if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash) |
| 969 | || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash)) |
| 970 | continue; |
| 971 | |
| 972 | /* |
| 973 | * Search unindexed leaf block now. We're not |
| 974 | * guaranteed to find anything. |
| 975 | */ |
| 976 | ret = ocfs2_read_dir_block_direct(dir, |
| 977 | le64_to_cpu(dx_entry->dx_dirent_blk), |
| 978 | &dir_ent_bh); |
| 979 | if (ret) { |
| 980 | mlog_errno(ret); |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * XXX: We should check the unindexed block here, |
| 986 | * before using it. |
| 987 | */ |
| 988 | |
| 989 | found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen, |
| 990 | 0, dir_ent_bh->b_data, |
| 991 | dir->i_sb->s_blocksize, &dir_ent); |
| 992 | if (found == 1) |
| 993 | break; |
| 994 | |
| 995 | if (found == -1) { |
| 996 | /* This means we found a bad directory entry. */ |
| 997 | ret = -EIO; |
| 998 | mlog_errno(ret); |
| 999 | goto out; |
| 1000 | } |
| 1001 | |
| 1002 | brelse(dir_ent_bh); |
| 1003 | dir_ent_bh = NULL; |
| 1004 | } |
| 1005 | |
| 1006 | if (found <= 0) { |
| 1007 | ret = -ENOENT; |
| 1008 | goto out; |
| 1009 | } |
| 1010 | |
| 1011 | res->dl_leaf_bh = dir_ent_bh; |
| 1012 | res->dl_entry = dir_ent; |
| 1013 | res->dl_dx_leaf_bh = dx_leaf_bh; |
| 1014 | res->dl_dx_entry = dx_entry; |
| 1015 | |
| 1016 | ret = 0; |
| 1017 | out: |
| 1018 | if (ret) { |
| 1019 | brelse(dx_leaf_bh); |
| 1020 | brelse(dir_ent_bh); |
| 1021 | } |
| 1022 | return ret; |
| 1023 | } |
| 1024 | |
| 1025 | static int ocfs2_find_entry_dx(const char *name, int namelen, |
| 1026 | struct inode *dir, |
| 1027 | struct ocfs2_dir_lookup_result *lookup) |
| 1028 | { |
| 1029 | int ret; |
| 1030 | struct buffer_head *di_bh = NULL; |
| 1031 | struct ocfs2_dinode *di; |
| 1032 | struct buffer_head *dx_root_bh = NULL; |
| 1033 | struct ocfs2_dx_root_block *dx_root; |
| 1034 | |
| 1035 | ret = ocfs2_read_inode_block(dir, &di_bh); |
| 1036 | if (ret) { |
| 1037 | mlog_errno(ret); |
| 1038 | goto out; |
| 1039 | } |
| 1040 | |
| 1041 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1042 | |
| 1043 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); |
| 1044 | if (ret) { |
| 1045 | mlog_errno(ret); |
| 1046 | goto out; |
| 1047 | } |
| 1048 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; |
| 1049 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1050 | ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1051 | if (ret) { |
| 1052 | if (ret != -ENOENT) |
| 1053 | mlog_errno(ret); |
| 1054 | goto out; |
| 1055 | } |
| 1056 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1057 | lookup->dl_dx_root_bh = dx_root_bh; |
| 1058 | dx_root_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1059 | out: |
| 1060 | brelse(di_bh); |
| 1061 | brelse(dx_root_bh); |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1065 | /* |
| 1066 | * Try to find an entry of the provided name within 'dir'. |
| 1067 | * |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1068 | * If nothing was found, -ENOENT is returned. Otherwise, zero is |
| 1069 | * returned and the struct 'res' will contain information useful to |
| 1070 | * other directory manipulation functions. |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1071 | * |
| 1072 | * Caller can NOT assume anything about the contents of the |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1073 | * buffer_heads - they are passed back only so that it can be passed |
| 1074 | * into any one of the manipulation functions (add entry, delete |
| 1075 | * entry, etc). As an example, bh in the extent directory case is a |
| 1076 | * data block, in the inline-data case it actually points to an inode, |
| 1077 | * in the indexed directory case, multiple buffers are involved. |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1078 | */ |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1079 | int ocfs2_find_entry(const char *name, int namelen, |
| 1080 | struct inode *dir, struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1081 | { |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1082 | struct buffer_head *bh; |
| 1083 | struct ocfs2_dir_entry *res_dir = NULL; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1084 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1085 | if (ocfs2_dir_indexed(dir)) |
| 1086 | return ocfs2_find_entry_dx(name, namelen, dir, lookup); |
| 1087 | |
| 1088 | /* |
| 1089 | * The unindexed dir code only uses part of the lookup |
| 1090 | * structure, so there's no reason to push it down further |
| 1091 | * than this. |
| 1092 | */ |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1093 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1094 | bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir); |
| 1095 | else |
| 1096 | bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1097 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1098 | if (bh == NULL) |
| 1099 | return -ENOENT; |
| 1100 | |
| 1101 | lookup->dl_leaf_bh = bh; |
| 1102 | lookup->dl_entry = res_dir; |
| 1103 | return 0; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1106 | /* |
| 1107 | * Update inode number and type of a previously found directory entry. |
| 1108 | */ |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1109 | int ocfs2_update_entry(struct inode *dir, handle_t *handle, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1110 | struct ocfs2_dir_lookup_result *res, |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1111 | struct inode *new_entry_inode) |
| 1112 | { |
| 1113 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1114 | ocfs2_journal_access_func access = ocfs2_journal_access_db; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1115 | struct ocfs2_dir_entry *de = res->dl_entry; |
| 1116 | struct buffer_head *de_bh = res->dl_leaf_bh; |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1117 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1118 | /* |
| 1119 | * The same code works fine for both inline-data and extent |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1120 | * based directories, so no need to split this up. The only |
| 1121 | * difference is the journal_access function. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1122 | */ |
| 1123 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1124 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 1125 | access = ocfs2_journal_access_di; |
| 1126 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1127 | ret = access(handle, INODE_CACHE(dir), de_bh, |
| 1128 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 1129 | if (ret) { |
| 1130 | mlog_errno(ret); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
| 1134 | de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); |
| 1135 | ocfs2_set_de_type(de, new_entry_inode->i_mode); |
| 1136 | |
| 1137 | ocfs2_journal_dirty(handle, de_bh); |
| 1138 | |
| 1139 | out: |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1143 | /* |
| 1144 | * __ocfs2_delete_entry deletes a directory entry by merging it with the |
| 1145 | * previous entry |
| 1146 | */ |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1147 | static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, |
| 1148 | struct ocfs2_dir_entry *de_del, |
| 1149 | struct buffer_head *bh, char *first_de, |
| 1150 | unsigned int bytes) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1151 | { |
| 1152 | struct ocfs2_dir_entry *de, *pde; |
| 1153 | int i, status = -ENOENT; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1154 | ocfs2_journal_access_func access = ocfs2_journal_access_db; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1155 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1156 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 1157 | access = ocfs2_journal_access_di; |
| 1158 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1159 | i = 0; |
| 1160 | pde = NULL; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1161 | de = (struct ocfs2_dir_entry *) first_de; |
| 1162 | while (i < bytes) { |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1163 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { |
| 1164 | status = -EIO; |
| 1165 | mlog_errno(status); |
| 1166 | goto bail; |
| 1167 | } |
| 1168 | if (de == de_del) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1169 | status = access(handle, INODE_CACHE(dir), bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1170 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1171 | if (status < 0) { |
| 1172 | status = -EIO; |
| 1173 | mlog_errno(status); |
| 1174 | goto bail; |
| 1175 | } |
| 1176 | if (pde) |
Marcin Slusarz | 0dd3256 | 2008-02-13 00:06:18 +0100 | [diff] [blame] | 1177 | le16_add_cpu(&pde->rec_len, |
| 1178 | le16_to_cpu(de->rec_len)); |
Wengang Wang | 8298524 | 2011-07-12 16:43:14 +0800 | [diff] [blame] | 1179 | de->inode = 0; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1180 | dir->i_version++; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1181 | ocfs2_journal_dirty(handle, bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1182 | goto bail; |
| 1183 | } |
| 1184 | i += le16_to_cpu(de->rec_len); |
| 1185 | pde = de; |
| 1186 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); |
| 1187 | } |
| 1188 | bail: |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1189 | return status; |
| 1190 | } |
| 1191 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1192 | static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de) |
| 1193 | { |
| 1194 | unsigned int hole; |
| 1195 | |
| 1196 | if (le64_to_cpu(de->inode) == 0) |
| 1197 | hole = le16_to_cpu(de->rec_len); |
| 1198 | else |
| 1199 | hole = le16_to_cpu(de->rec_len) - |
| 1200 | OCFS2_DIR_REC_LEN(de->name_len); |
| 1201 | |
| 1202 | return hole; |
| 1203 | } |
| 1204 | |
| 1205 | static int ocfs2_find_max_rec_len(struct super_block *sb, |
| 1206 | struct buffer_head *dirblock_bh) |
| 1207 | { |
| 1208 | int size, this_hole, largest_hole = 0; |
| 1209 | char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data; |
| 1210 | struct ocfs2_dir_entry *de; |
| 1211 | |
| 1212 | trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb); |
| 1213 | size = ocfs2_dir_trailer_blk_off(sb); |
| 1214 | limit = start + size; |
| 1215 | de_buf = start; |
| 1216 | de = (struct ocfs2_dir_entry *)de_buf; |
| 1217 | do { |
| 1218 | if (de_buf != trailer) { |
| 1219 | this_hole = ocfs2_figure_dirent_hole(de); |
| 1220 | if (this_hole > largest_hole) |
| 1221 | largest_hole = this_hole; |
| 1222 | } |
| 1223 | |
| 1224 | de_buf += le16_to_cpu(de->rec_len); |
| 1225 | de = (struct ocfs2_dir_entry *)de_buf; |
| 1226 | } while (de_buf < limit); |
| 1227 | |
| 1228 | if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) |
| 1229 | return largest_hole; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1233 | static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list, |
| 1234 | int index) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1235 | { |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1236 | int num_used = le16_to_cpu(entry_list->de_num_used); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1237 | |
| 1238 | if (num_used == 1 || index == (num_used - 1)) |
| 1239 | goto clear; |
| 1240 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1241 | memmove(&entry_list->de_entries[index], |
| 1242 | &entry_list->de_entries[index + 1], |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1243 | (num_used - index - 1)*sizeof(struct ocfs2_dx_entry)); |
| 1244 | clear: |
| 1245 | num_used--; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1246 | memset(&entry_list->de_entries[num_used], 0, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1247 | sizeof(struct ocfs2_dx_entry)); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1248 | entry_list->de_num_used = cpu_to_le16(num_used); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir, |
| 1252 | struct ocfs2_dir_lookup_result *lookup) |
| 1253 | { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1254 | int ret, index, max_rec_len, add_to_free_list = 0; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1255 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1256 | struct buffer_head *leaf_bh = lookup->dl_leaf_bh; |
| 1257 | struct ocfs2_dx_leaf *dx_leaf; |
| 1258 | struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1259 | struct ocfs2_dir_block_trailer *trailer; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1260 | struct ocfs2_dx_root_block *dx_root; |
| 1261 | struct ocfs2_dx_entry_list *entry_list; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1262 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1263 | /* |
| 1264 | * This function gets a bit messy because we might have to |
| 1265 | * modify the root block, regardless of whether the indexed |
| 1266 | * entries are stored inline. |
| 1267 | */ |
| 1268 | |
| 1269 | /* |
| 1270 | * *Only* set 'entry_list' here, based on where we're looking |
| 1271 | * for the indexed entries. Later, we might still want to |
| 1272 | * journal both blocks, based on free list state. |
| 1273 | */ |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1274 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 1275 | if (ocfs2_dx_root_inline(dx_root)) { |
| 1276 | entry_list = &dx_root->dr_entries; |
| 1277 | } else { |
| 1278 | dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data; |
| 1279 | entry_list = &dx_leaf->dl_list; |
| 1280 | } |
| 1281 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1282 | /* Neither of these are a disk corruption - that should have |
| 1283 | * been caught by lookup, before we got here. */ |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1284 | BUG_ON(le16_to_cpu(entry_list->de_count) <= 0); |
| 1285 | BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1286 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1287 | index = (char *)dx_entry - (char *)entry_list->de_entries; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1288 | index /= sizeof(*dx_entry); |
| 1289 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1290 | if (index >= le16_to_cpu(entry_list->de_num_used)) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1291 | mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n", |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1292 | (unsigned long long)OCFS2_I(dir)->ip_blkno, index, |
| 1293 | entry_list, dx_entry); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1294 | return -EIO; |
| 1295 | } |
| 1296 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1297 | /* |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1298 | * We know that removal of this dirent will leave enough room |
| 1299 | * for a new one, so add this block to the free list if it |
| 1300 | * isn't already there. |
| 1301 | */ |
| 1302 | trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); |
| 1303 | if (trailer->db_free_rec_len == 0) |
| 1304 | add_to_free_list = 1; |
| 1305 | |
| 1306 | /* |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1307 | * Add the block holding our index into the journal before |
| 1308 | * removing the unindexed entry. If we get an error return |
| 1309 | * from __ocfs2_delete_entry(), then it hasn't removed the |
| 1310 | * entry yet. Likewise, successful return means we *must* |
| 1311 | * remove the indexed entry. |
| 1312 | * |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1313 | * We're also careful to journal the root tree block here as |
| 1314 | * the entry count needs to be updated. Also, we might be |
| 1315 | * adding to the start of the free list. |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1316 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1317 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1318 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1319 | if (ret) { |
| 1320 | mlog_errno(ret); |
| 1321 | goto out; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | if (!ocfs2_dx_root_inline(dx_root)) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1325 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1326 | lookup->dl_dx_leaf_bh, |
| 1327 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1328 | if (ret) { |
| 1329 | mlog_errno(ret); |
| 1330 | goto out; |
| 1331 | } |
| 1332 | } |
| 1333 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 1334 | trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 1335 | index); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1336 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1337 | ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry, |
| 1338 | leaf_bh, leaf_bh->b_data, leaf_bh->b_size); |
| 1339 | if (ret) { |
| 1340 | mlog_errno(ret); |
| 1341 | goto out; |
| 1342 | } |
| 1343 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1344 | max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh); |
| 1345 | trailer->db_free_rec_len = cpu_to_le16(max_rec_len); |
| 1346 | if (add_to_free_list) { |
| 1347 | trailer->db_free_next = dx_root->dr_free_blk; |
| 1348 | dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr); |
| 1349 | ocfs2_journal_dirty(handle, dx_root_bh); |
| 1350 | } |
| 1351 | |
| 1352 | /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */ |
| 1353 | ocfs2_journal_dirty(handle, leaf_bh); |
| 1354 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1355 | le32_add_cpu(&dx_root->dr_num_entries, -1); |
| 1356 | ocfs2_journal_dirty(handle, dx_root_bh); |
| 1357 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1358 | ocfs2_dx_list_remove_entry(entry_list, index); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1359 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1360 | if (!ocfs2_dx_root_inline(dx_root)) |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1361 | ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1362 | |
| 1363 | out: |
| 1364 | return ret; |
| 1365 | } |
| 1366 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1367 | static inline int ocfs2_delete_entry_id(handle_t *handle, |
| 1368 | struct inode *dir, |
| 1369 | struct ocfs2_dir_entry *de_del, |
| 1370 | struct buffer_head *bh) |
| 1371 | { |
| 1372 | int ret; |
| 1373 | struct buffer_head *di_bh = NULL; |
| 1374 | struct ocfs2_dinode *di; |
| 1375 | struct ocfs2_inline_data *data; |
| 1376 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1377 | ret = ocfs2_read_inode_block(dir, &di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1378 | if (ret) { |
| 1379 | mlog_errno(ret); |
| 1380 | goto out; |
| 1381 | } |
| 1382 | |
| 1383 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1384 | data = &di->id2.i_data; |
| 1385 | |
| 1386 | ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, |
| 1387 | i_size_read(dir)); |
| 1388 | |
| 1389 | brelse(di_bh); |
| 1390 | out: |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
| 1394 | static inline int ocfs2_delete_entry_el(handle_t *handle, |
| 1395 | struct inode *dir, |
| 1396 | struct ocfs2_dir_entry *de_del, |
| 1397 | struct buffer_head *bh) |
| 1398 | { |
| 1399 | return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, |
| 1400 | bh->b_size); |
| 1401 | } |
| 1402 | |
| 1403 | /* |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1404 | * Delete a directory entry. Hide the details of directory |
| 1405 | * implementation from the caller. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1406 | */ |
| 1407 | int ocfs2_delete_entry(handle_t *handle, |
| 1408 | struct inode *dir, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1409 | struct ocfs2_dir_lookup_result *res) |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1410 | { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1411 | if (ocfs2_dir_indexed(dir)) |
| 1412 | return ocfs2_delete_entry_dx(handle, dir, res); |
| 1413 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1414 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1415 | return ocfs2_delete_entry_id(handle, dir, res->dl_entry, |
| 1416 | res->dl_leaf_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1417 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1418 | return ocfs2_delete_entry_el(handle, dir, res->dl_entry, |
| 1419 | res->dl_leaf_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 1422 | /* |
| 1423 | * Check whether 'de' has enough room to hold an entry of |
| 1424 | * 'new_rec_len' bytes. |
| 1425 | */ |
| 1426 | static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, |
| 1427 | unsigned int new_rec_len) |
| 1428 | { |
| 1429 | unsigned int de_really_used; |
| 1430 | |
| 1431 | /* Check whether this is an empty record with enough space */ |
| 1432 | if (le64_to_cpu(de->inode) == 0 && |
| 1433 | le16_to_cpu(de->rec_len) >= new_rec_len) |
| 1434 | return 1; |
| 1435 | |
| 1436 | /* |
| 1437 | * Record might have free space at the end which we can |
| 1438 | * use. |
| 1439 | */ |
| 1440 | de_really_used = OCFS2_DIR_REC_LEN(de->name_len); |
| 1441 | if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) |
| 1442 | return 1; |
| 1443 | |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1447 | static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf, |
| 1448 | struct ocfs2_dx_entry *dx_new_entry) |
| 1449 | { |
| 1450 | int i; |
| 1451 | |
| 1452 | i = le16_to_cpu(dx_leaf->dl_list.de_num_used); |
| 1453 | dx_leaf->dl_list.de_entries[i] = *dx_new_entry; |
| 1454 | |
| 1455 | le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1); |
| 1456 | } |
| 1457 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1458 | static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list, |
| 1459 | struct ocfs2_dx_hinfo *hinfo, |
| 1460 | u64 dirent_blk) |
| 1461 | { |
| 1462 | int i; |
| 1463 | struct ocfs2_dx_entry *dx_entry; |
| 1464 | |
| 1465 | i = le16_to_cpu(entry_list->de_num_used); |
| 1466 | dx_entry = &entry_list->de_entries[i]; |
| 1467 | |
| 1468 | memset(dx_entry, 0, sizeof(*dx_entry)); |
| 1469 | dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash); |
| 1470 | dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash); |
| 1471 | dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk); |
| 1472 | |
| 1473 | le16_add_cpu(&entry_list->de_num_used, 1); |
| 1474 | } |
| 1475 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1476 | static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle, |
| 1477 | struct ocfs2_dx_hinfo *hinfo, |
| 1478 | u64 dirent_blk, |
| 1479 | struct buffer_head *dx_leaf_bh) |
| 1480 | { |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1481 | int ret; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1482 | struct ocfs2_dx_leaf *dx_leaf; |
| 1483 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1484 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1485 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1486 | if (ret) { |
| 1487 | mlog_errno(ret); |
| 1488 | goto out; |
| 1489 | } |
| 1490 | |
| 1491 | dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1492 | ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1493 | ocfs2_journal_dirty(handle, dx_leaf_bh); |
| 1494 | |
| 1495 | out: |
| 1496 | return ret; |
| 1497 | } |
| 1498 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1499 | static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle, |
| 1500 | struct ocfs2_dx_hinfo *hinfo, |
| 1501 | u64 dirent_blk, |
| 1502 | struct ocfs2_dx_root_block *dx_root) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1503 | { |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1504 | ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk); |
| 1505 | } |
| 1506 | |
| 1507 | static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle, |
| 1508 | struct ocfs2_dir_lookup_result *lookup) |
| 1509 | { |
| 1510 | int ret = 0; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1511 | struct ocfs2_dx_root_block *dx_root; |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1512 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1513 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1514 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1515 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1516 | if (ret) { |
| 1517 | mlog_errno(ret); |
| 1518 | goto out; |
| 1519 | } |
| 1520 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 1521 | dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data; |
| 1522 | if (ocfs2_dx_root_inline(dx_root)) { |
| 1523 | ocfs2_dx_inline_root_insert(dir, handle, |
| 1524 | &lookup->dl_hinfo, |
| 1525 | lookup->dl_leaf_bh->b_blocknr, |
| 1526 | dx_root); |
| 1527 | } else { |
| 1528 | ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo, |
| 1529 | lookup->dl_leaf_bh->b_blocknr, |
| 1530 | lookup->dl_dx_leaf_bh); |
| 1531 | if (ret) |
| 1532 | goto out; |
| 1533 | } |
| 1534 | |
| 1535 | le32_add_cpu(&dx_root->dr_num_entries, 1); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1536 | ocfs2_journal_dirty(handle, dx_root_bh); |
| 1537 | |
| 1538 | out: |
| 1539 | return ret; |
| 1540 | } |
| 1541 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1542 | static void ocfs2_remove_block_from_free_list(struct inode *dir, |
| 1543 | handle_t *handle, |
| 1544 | struct ocfs2_dir_lookup_result *lookup) |
| 1545 | { |
| 1546 | struct ocfs2_dir_block_trailer *trailer, *prev; |
| 1547 | struct ocfs2_dx_root_block *dx_root; |
| 1548 | struct buffer_head *bh; |
| 1549 | |
| 1550 | trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); |
| 1551 | |
| 1552 | if (ocfs2_free_list_at_root(lookup)) { |
| 1553 | bh = lookup->dl_dx_root_bh; |
| 1554 | dx_root = (struct ocfs2_dx_root_block *)bh->b_data; |
| 1555 | dx_root->dr_free_blk = trailer->db_free_next; |
| 1556 | } else { |
| 1557 | bh = lookup->dl_prev_leaf_bh; |
| 1558 | prev = ocfs2_trailer_from_bh(bh, dir->i_sb); |
| 1559 | prev->db_free_next = trailer->db_free_next; |
| 1560 | } |
| 1561 | |
| 1562 | trailer->db_free_rec_len = cpu_to_le16(0); |
| 1563 | trailer->db_free_next = cpu_to_le64(0); |
| 1564 | |
| 1565 | ocfs2_journal_dirty(handle, bh); |
| 1566 | ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * This expects that a journal write has been reserved on |
| 1571 | * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh |
| 1572 | */ |
| 1573 | static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle, |
| 1574 | struct ocfs2_dir_lookup_result *lookup) |
| 1575 | { |
| 1576 | int max_rec_len; |
| 1577 | struct ocfs2_dir_block_trailer *trailer; |
| 1578 | |
| 1579 | /* Walk dl_leaf_bh to figure out what the new free rec_len is. */ |
| 1580 | max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh); |
| 1581 | if (max_rec_len) { |
| 1582 | /* |
| 1583 | * There's still room in this block, so no need to remove it |
| 1584 | * from the free list. In this case, we just want to update |
| 1585 | * the rec len accounting. |
| 1586 | */ |
| 1587 | trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb); |
| 1588 | trailer->db_free_rec_len = cpu_to_le16(max_rec_len); |
| 1589 | ocfs2_journal_dirty(handle, lookup->dl_leaf_bh); |
| 1590 | } else { |
| 1591 | ocfs2_remove_block_from_free_list(dir, handle, lookup); |
| 1592 | } |
| 1593 | } |
| 1594 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1595 | /* we don't always have a dentry for what we want to add, so people |
| 1596 | * like orphan dir can call this instead. |
| 1597 | * |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1598 | * The lookup context must have been filled from |
| 1599 | * ocfs2_prepare_dir_for_insert. |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1600 | */ |
| 1601 | int __ocfs2_add_entry(handle_t *handle, |
| 1602 | struct inode *dir, |
| 1603 | const char *name, int namelen, |
| 1604 | struct inode *inode, u64 blkno, |
| 1605 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1606 | struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1607 | { |
| 1608 | unsigned long offset; |
| 1609 | unsigned short rec_len; |
| 1610 | struct ocfs2_dir_entry *de, *de1; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1611 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; |
| 1612 | struct super_block *sb = dir->i_sb; |
Daeseok Youn | 2e17315 | 2015-06-24 16:55:01 -0700 | [diff] [blame] | 1613 | int retval; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1614 | unsigned int size = sb->s_blocksize; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1615 | struct buffer_head *insert_bh = lookup->dl_leaf_bh; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1616 | char *data_start = insert_bh->b_data; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1617 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1618 | if (!namelen) |
| 1619 | return -EINVAL; |
| 1620 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1621 | if (ocfs2_dir_indexed(dir)) { |
| 1622 | struct buffer_head *bh; |
| 1623 | |
| 1624 | /* |
| 1625 | * An indexed dir may require that we update the free space |
| 1626 | * list. Reserve a write to the previous node in the list so |
| 1627 | * that we don't fail later. |
| 1628 | * |
| 1629 | * XXX: This can be either a dx_root_block, or an unindexed |
| 1630 | * directory tree leaf block. |
| 1631 | */ |
| 1632 | if (ocfs2_free_list_at_root(lookup)) { |
| 1633 | bh = lookup->dl_dx_root_bh; |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1634 | retval = ocfs2_journal_access_dr(handle, |
| 1635 | INODE_CACHE(dir), bh, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1636 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1637 | } else { |
| 1638 | bh = lookup->dl_prev_leaf_bh; |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1639 | retval = ocfs2_journal_access_db(handle, |
| 1640 | INODE_CACHE(dir), bh, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1641 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1642 | } |
| 1643 | if (retval) { |
| 1644 | mlog_errno(retval); |
| 1645 | return retval; |
| 1646 | } |
| 1647 | } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1648 | data_start = di->id2.i_data.id_data; |
| 1649 | size = i_size_read(dir); |
| 1650 | |
| 1651 | BUG_ON(insert_bh != parent_fe_bh); |
| 1652 | } |
| 1653 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1654 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 1655 | offset = 0; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1656 | de = (struct ocfs2_dir_entry *) data_start; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1657 | while (1) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1658 | BUG_ON((char *)de >= (size + data_start)); |
| 1659 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1660 | /* These checks should've already been passed by the |
| 1661 | * prepare function, but I guess we can leave them |
| 1662 | * here anyway. */ |
| 1663 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { |
| 1664 | retval = -ENOENT; |
| 1665 | goto bail; |
| 1666 | } |
| 1667 | if (ocfs2_match(namelen, name, de)) { |
| 1668 | retval = -EEXIST; |
| 1669 | goto bail; |
| 1670 | } |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 1671 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1672 | /* We're guaranteed that we should have space, so we |
| 1673 | * can't possibly have hit the trailer...right? */ |
| 1674 | mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size), |
| 1675 | "Hit dir trailer trying to insert %.*s " |
| 1676 | "(namelen %d) into directory %llu. " |
| 1677 | "offset is %lu, trailer offset is %d\n", |
| 1678 | namelen, name, namelen, |
| 1679 | (unsigned long long)parent_fe_bh->b_blocknr, |
| 1680 | offset, ocfs2_dir_trailer_blk_off(dir->i_sb)); |
| 1681 | |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 1682 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1683 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 1684 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 1685 | if (retval < 0) { |
| 1686 | mlog_errno(retval); |
| 1687 | goto bail; |
| 1688 | } |
| 1689 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1690 | if (insert_bh == parent_fe_bh) |
Daeseok Youn | 2e17315 | 2015-06-24 16:55:01 -0700 | [diff] [blame] | 1691 | retval = ocfs2_journal_access_di(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1692 | INODE_CACHE(dir), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1693 | insert_bh, |
| 1694 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1695 | else { |
Daeseok Youn | 2e17315 | 2015-06-24 16:55:01 -0700 | [diff] [blame] | 1696 | retval = ocfs2_journal_access_db(handle, |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 1697 | INODE_CACHE(dir), |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 1698 | insert_bh, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1699 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1700 | |
Daeseok Youn | 2e17315 | 2015-06-24 16:55:01 -0700 | [diff] [blame] | 1701 | if (!retval && ocfs2_dir_indexed(dir)) |
| 1702 | retval = ocfs2_dx_dir_insert(dir, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 1703 | handle, |
| 1704 | lookup); |
Daeseok Youn | 2e17315 | 2015-06-24 16:55:01 -0700 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | if (retval) { |
| 1708 | mlog_errno(retval); |
| 1709 | goto bail; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1710 | } |
| 1711 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1712 | /* By now the buffer is marked for journaling */ |
| 1713 | offset += le16_to_cpu(de->rec_len); |
| 1714 | if (le64_to_cpu(de->inode)) { |
| 1715 | de1 = (struct ocfs2_dir_entry *)((char *) de + |
| 1716 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 1717 | de1->rec_len = |
| 1718 | cpu_to_le16(le16_to_cpu(de->rec_len) - |
| 1719 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 1720 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 1721 | de = de1; |
| 1722 | } |
| 1723 | de->file_type = OCFS2_FT_UNKNOWN; |
| 1724 | if (blkno) { |
| 1725 | de->inode = cpu_to_le64(blkno); |
| 1726 | ocfs2_set_de_type(de, inode->i_mode); |
| 1727 | } else |
| 1728 | de->inode = 0; |
| 1729 | de->name_len = namelen; |
| 1730 | memcpy(de->name, name, namelen); |
| 1731 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 1732 | if (ocfs2_dir_indexed(dir)) |
| 1733 | ocfs2_recalc_free_list(dir, handle, lookup); |
| 1734 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1735 | dir->i_version++; |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 1736 | ocfs2_journal_dirty(handle, insert_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1737 | retval = 0; |
| 1738 | goto bail; |
| 1739 | } |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 1740 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1741 | offset += le16_to_cpu(de->rec_len); |
| 1742 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); |
| 1743 | } |
| 1744 | |
| 1745 | /* when you think about it, the assert above should prevent us |
| 1746 | * from ever getting here. */ |
| 1747 | retval = -ENOSPC; |
| 1748 | bail: |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1749 | if (retval) |
| 1750 | mlog_errno(retval); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1751 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1752 | return retval; |
| 1753 | } |
| 1754 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1755 | static int ocfs2_dir_foreach_blk_id(struct inode *inode, |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 1756 | u64 *f_version, |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1757 | struct dir_context *ctx) |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1758 | { |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1759 | int ret, i; |
| 1760 | unsigned long offset = ctx->pos; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1761 | struct buffer_head *di_bh = NULL; |
| 1762 | struct ocfs2_dinode *di; |
| 1763 | struct ocfs2_inline_data *data; |
| 1764 | struct ocfs2_dir_entry *de; |
| 1765 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 1766 | ret = ocfs2_read_inode_block(inode, &di_bh); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1767 | if (ret) { |
| 1768 | mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", |
| 1769 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 1770 | goto out; |
| 1771 | } |
| 1772 | |
| 1773 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1774 | data = &di->id2.i_data; |
| 1775 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1776 | while (ctx->pos < i_size_read(inode)) { |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1777 | /* If the dir block has changed since the last call to |
| 1778 | * readdir(2), then we might be pointing to an invalid |
| 1779 | * dirent right now. Scan from the start of the block |
| 1780 | * to make sure. */ |
| 1781 | if (*f_version != inode->i_version) { |
| 1782 | for (i = 0; i < i_size_read(inode) && i < offset; ) { |
| 1783 | de = (struct ocfs2_dir_entry *) |
| 1784 | (data->id_data + i); |
| 1785 | /* It's too expensive to do a full |
| 1786 | * dirent test each time round this |
| 1787 | * loop, but we do have to test at |
| 1788 | * least that it is non-zero. A |
| 1789 | * failure will be detected in the |
| 1790 | * dirent test below. */ |
| 1791 | if (le16_to_cpu(de->rec_len) < |
| 1792 | OCFS2_DIR_REC_LEN(1)) |
| 1793 | break; |
| 1794 | i += le16_to_cpu(de->rec_len); |
| 1795 | } |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1796 | ctx->pos = offset = i; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1797 | *f_version = inode->i_version; |
| 1798 | } |
| 1799 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1800 | de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos); |
| 1801 | if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) { |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1802 | /* On error, skip the f_pos to the end. */ |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1803 | ctx->pos = i_size_read(inode); |
| 1804 | break; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1805 | } |
| 1806 | offset += le16_to_cpu(de->rec_len); |
| 1807 | if (le64_to_cpu(de->inode)) { |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1808 | unsigned char d_type = DT_UNKNOWN; |
| 1809 | |
| 1810 | if (de->file_type < OCFS2_FT_MAX) |
| 1811 | d_type = ocfs2_filetype_table[de->file_type]; |
| 1812 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1813 | if (!dir_emit(ctx, de->name, de->name_len, |
| 1814 | le64_to_cpu(de->inode), d_type)) |
| 1815 | goto out; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1816 | } |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1817 | ctx->pos += le16_to_cpu(de->rec_len); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1818 | } |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1819 | out: |
| 1820 | brelse(di_bh); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1821 | return 0; |
| 1822 | } |
| 1823 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 1824 | /* |
| 1825 | * NOTE: This function can be called against unindexed directories, |
| 1826 | * and indexed ones. |
| 1827 | */ |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1828 | static int ocfs2_dir_foreach_blk_el(struct inode *inode, |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 1829 | u64 *f_version, |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1830 | struct dir_context *ctx, |
| 1831 | bool persist) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1832 | { |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1833 | unsigned long offset, blk, last_ra_blk = 0; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1834 | int i; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1835 | struct buffer_head * bh, * tmp; |
| 1836 | struct ocfs2_dir_entry * de; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1837 | struct super_block * sb = inode->i_sb; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1838 | unsigned int ra_sectors = 16; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1839 | int stored = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1840 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1841 | bh = NULL; |
| 1842 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1843 | offset = ctx->pos & (sb->s_blocksize - 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1844 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1845 | while (ctx->pos < i_size_read(inode)) { |
| 1846 | blk = ctx->pos >> sb->s_blocksize_bits; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1847 | if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { |
| 1848 | /* Skip the corrupt dirblock and keep trying */ |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1849 | ctx->pos += sb->s_blocksize - offset; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1850 | continue; |
| 1851 | } |
| 1852 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1853 | /* The idea here is to begin with 8k read-ahead and to stay |
| 1854 | * 4k ahead of our current position. |
| 1855 | * |
| 1856 | * TODO: Use the pagecache for this. We just need to |
| 1857 | * make sure it's cluster-safe... */ |
| 1858 | if (!last_ra_blk |
| 1859 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { |
| 1860 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1861 | i > 0; i--) { |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1862 | tmp = NULL; |
| 1863 | if (!ocfs2_read_dir_block(inode, ++blk, &tmp, |
| 1864 | OCFS2_BH_READAHEAD)) |
| 1865 | brelse(tmp); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1866 | } |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1867 | last_ra_blk = blk; |
| 1868 | ra_sectors = 8; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1869 | } |
| 1870 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1871 | /* If the dir block has changed since the last call to |
| 1872 | * readdir(2), then we might be pointing to an invalid |
| 1873 | * dirent right now. Scan from the start of the block |
| 1874 | * to make sure. */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1875 | if (*f_version != inode->i_version) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1876 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
| 1877 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); |
| 1878 | /* It's too expensive to do a full |
| 1879 | * dirent test each time round this |
| 1880 | * loop, but we do have to test at |
| 1881 | * least that it is non-zero. A |
| 1882 | * failure will be detected in the |
| 1883 | * dirent test below. */ |
| 1884 | if (le16_to_cpu(de->rec_len) < |
| 1885 | OCFS2_DIR_REC_LEN(1)) |
| 1886 | break; |
| 1887 | i += le16_to_cpu(de->rec_len); |
| 1888 | } |
| 1889 | offset = i; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1890 | ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1891 | | offset; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1892 | *f_version = inode->i_version; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1895 | while (ctx->pos < i_size_read(inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1896 | && offset < sb->s_blocksize) { |
| 1897 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); |
| 1898 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 1899 | /* On error, skip the f_pos to the |
| 1900 | next block. */ |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1901 | ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1902 | brelse(bh); |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1903 | continue; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1904 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1905 | if (le64_to_cpu(de->inode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1906 | unsigned char d_type = DT_UNKNOWN; |
| 1907 | |
| 1908 | if (de->file_type < OCFS2_FT_MAX) |
| 1909 | d_type = ocfs2_filetype_table[de->file_type]; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1910 | if (!dir_emit(ctx, de->name, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1911 | de->name_len, |
Mark Fasheh | 7e85367 | 2007-09-10 17:30:26 -0700 | [diff] [blame] | 1912 | le64_to_cpu(de->inode), |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1913 | d_type)) { |
| 1914 | brelse(bh); |
| 1915 | return 0; |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 1916 | } |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1917 | stored++; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1918 | } |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1919 | offset += le16_to_cpu(de->rec_len); |
| 1920 | ctx->pos += le16_to_cpu(de->rec_len); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1921 | } |
| 1922 | offset = 0; |
| 1923 | brelse(bh); |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1924 | bh = NULL; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1925 | if (!persist && stored) |
| 1926 | break; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1927 | } |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1928 | return 0; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 1931 | static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1932 | struct dir_context *ctx, |
| 1933 | bool persist) |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1934 | { |
| 1935 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1936 | return ocfs2_dir_foreach_blk_id(inode, f_version, ctx); |
| 1937 | return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 1938 | } |
| 1939 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1940 | /* |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1941 | * This is intended to be called from inside other kernel functions, |
| 1942 | * so we fake some arguments. |
| 1943 | */ |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1944 | int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx) |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1945 | { |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 1946 | u64 version = inode->i_version; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1947 | ocfs2_dir_foreach_blk(inode, &version, ctx, true); |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 1948 | return 0; |
| 1949 | } |
| 1950 | |
| 1951 | /* |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1952 | * ocfs2_readdir() |
| 1953 | * |
| 1954 | */ |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1955 | int ocfs2_readdir(struct file *file, struct dir_context *ctx) |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1956 | { |
| 1957 | int error = 0; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1958 | struct inode *inode = file_inode(file); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1959 | int lock_level = 0; |
| 1960 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 1961 | trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1962 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1963 | error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1964 | if (lock_level && error >= 0) { |
| 1965 | /* We release EX lock which used to update atime |
| 1966 | * and get PR lock again to reduce contention |
| 1967 | * on commonly accessed directories. */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1968 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1969 | lock_level = 0; |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1970 | error = ocfs2_inode_lock(inode, NULL, 0); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1971 | } |
| 1972 | if (error < 0) { |
| 1973 | if (error != -ENOENT) |
| 1974 | mlog_errno(error); |
| 1975 | /* we haven't got any yet, so propagate the error. */ |
| 1976 | goto bail_nolock; |
| 1977 | } |
| 1978 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 1979 | error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1980 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 1981 | ocfs2_inode_unlock(inode, lock_level); |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 1982 | if (error) |
| 1983 | mlog_errno(error); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1984 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 1985 | bail_nolock: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1986 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 1987 | return error; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1991 | * NOTE: this should always be called with parent dir i_mutex taken. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1992 | */ |
| 1993 | int ocfs2_find_files_on_disk(const char *name, |
| 1994 | int namelen, |
| 1995 | u64 *blkno, |
| 1996 | struct inode *inode, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 1997 | struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1998 | { |
| 1999 | int status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2000 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 2001 | trace_ocfs2_find_files_on_disk(namelen, name, blkno, |
| 2002 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2003 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2004 | status = ocfs2_find_entry(name, namelen, inode, lookup); |
| 2005 | if (status) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2006 | goto leave; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2007 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2008 | *blkno = le64_to_cpu(lookup->dl_entry->inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2009 | |
| 2010 | status = 0; |
| 2011 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2012 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2013 | return status; |
| 2014 | } |
| 2015 | |
Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 2016 | /* |
| 2017 | * Convenience function for callers which just want the block number |
| 2018 | * mapped to a name and don't require the full dirent info, etc. |
| 2019 | */ |
| 2020 | int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, |
| 2021 | int namelen, u64 *blkno) |
| 2022 | { |
| 2023 | int ret; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2024 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 2025 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2026 | ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup); |
| 2027 | ocfs2_free_dir_lookup_result(&lookup); |
Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 2028 | |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2032 | /* Check for a name within a directory. |
| 2033 | * |
| 2034 | * Return 0 if the name does not exist |
| 2035 | * Return -EEXIST if the directory contains the name |
| 2036 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2037 | * Callers should have i_mutex + a cluster lock on dir |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2038 | */ |
| 2039 | int ocfs2_check_dir_for_entry(struct inode *dir, |
| 2040 | const char *name, |
| 2041 | int namelen) |
| 2042 | { |
Daeseok Youn | 7c01ad8 | 2015-04-14 15:43:30 -0700 | [diff] [blame] | 2043 | int ret = 0; |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2044 | struct ocfs2_dir_lookup_result lookup = { NULL, }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2045 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 2046 | trace_ocfs2_check_dir_for_entry( |
| 2047 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2048 | |
Daeseok Youn | 7c01ad8 | 2015-04-14 15:43:30 -0700 | [diff] [blame] | 2049 | if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) { |
| 2050 | ret = -EEXIST; |
| 2051 | mlog_errno(ret); |
| 2052 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2053 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 2054 | ocfs2_free_dir_lookup_result(&lookup); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2055 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2056 | return ret; |
| 2057 | } |
| 2058 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2059 | struct ocfs2_empty_dir_priv { |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 2060 | struct dir_context ctx; |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2061 | unsigned seen_dot; |
| 2062 | unsigned seen_dot_dot; |
| 2063 | unsigned seen_other; |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2064 | unsigned dx_dir; |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2065 | }; |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 2066 | static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name, |
| 2067 | int name_len, loff_t pos, u64 ino, |
| 2068 | unsigned type) |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2069 | { |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 2070 | struct ocfs2_empty_dir_priv *p = |
| 2071 | container_of(ctx, struct ocfs2_empty_dir_priv, ctx); |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2072 | |
| 2073 | /* |
| 2074 | * Check the positions of "." and ".." records to be sure |
| 2075 | * they're in the correct place. |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2076 | * |
| 2077 | * Indexed directories don't need to proceed past the first |
| 2078 | * two entries, so we end the scan after seeing '..'. Despite |
| 2079 | * that, we allow the scan to proceed In the event that we |
| 2080 | * have a corrupted indexed directory (no dot or dot dot |
| 2081 | * entries). This allows us to double check for existing |
| 2082 | * entries which might not have been found in the index. |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2083 | */ |
| 2084 | if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { |
| 2085 | p->seen_dot = 1; |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | if (name_len == 2 && !strncmp("..", name, 2) && |
| 2090 | pos == OCFS2_DIR_REC_LEN(1)) { |
| 2091 | p->seen_dot_dot = 1; |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2092 | |
| 2093 | if (p->dx_dir && p->seen_dot) |
| 2094 | return 1; |
| 2095 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2096 | return 0; |
| 2097 | } |
| 2098 | |
| 2099 | p->seen_other = 1; |
| 2100 | return 1; |
| 2101 | } |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2102 | |
| 2103 | static int ocfs2_empty_dir_dx(struct inode *inode, |
| 2104 | struct ocfs2_empty_dir_priv *priv) |
| 2105 | { |
| 2106 | int ret; |
| 2107 | struct buffer_head *di_bh = NULL; |
| 2108 | struct buffer_head *dx_root_bh = NULL; |
| 2109 | struct ocfs2_dinode *di; |
| 2110 | struct ocfs2_dx_root_block *dx_root; |
| 2111 | |
| 2112 | priv->dx_dir = 1; |
| 2113 | |
| 2114 | ret = ocfs2_read_inode_block(inode, &di_bh); |
| 2115 | if (ret) { |
| 2116 | mlog_errno(ret); |
| 2117 | goto out; |
| 2118 | } |
| 2119 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2120 | |
| 2121 | ret = ocfs2_read_dx_root(inode, di, &dx_root_bh); |
| 2122 | if (ret) { |
| 2123 | mlog_errno(ret); |
| 2124 | goto out; |
| 2125 | } |
| 2126 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 2127 | |
| 2128 | if (le32_to_cpu(dx_root->dr_num_entries) != 2) |
| 2129 | priv->seen_other = 1; |
| 2130 | |
| 2131 | out: |
| 2132 | brelse(di_bh); |
| 2133 | brelse(dx_root_bh); |
| 2134 | return ret; |
| 2135 | } |
| 2136 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2137 | /* |
| 2138 | * routine to check that the specified directory is empty (for rmdir) |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2139 | * |
| 2140 | * Returns 1 if dir is empty, zero otherwise. |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2141 | * |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2142 | * XXX: This is a performance problem for unindexed directories. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2143 | */ |
| 2144 | int ocfs2_empty_dir(struct inode *inode) |
| 2145 | { |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2146 | int ret; |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 2147 | struct ocfs2_empty_dir_priv priv = { |
Jeff Liu | d6394b5 | 2013-08-13 16:01:01 -0700 | [diff] [blame] | 2148 | .ctx.actor = ocfs2_empty_dir_filldir, |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 2149 | }; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2150 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2151 | if (ocfs2_dir_indexed(inode)) { |
| 2152 | ret = ocfs2_empty_dir_dx(inode, &priv); |
| 2153 | if (ret) |
| 2154 | mlog_errno(ret); |
| 2155 | /* |
| 2156 | * We still run ocfs2_dir_foreach to get the checks |
| 2157 | * for "." and "..". |
| 2158 | */ |
| 2159 | } |
| 2160 | |
Al Viro | 3704412b | 2013-05-22 21:06:00 -0400 | [diff] [blame] | 2161 | ret = ocfs2_dir_foreach(inode, &priv.ctx); |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2162 | if (ret) |
| 2163 | mlog_errno(ret); |
| 2164 | |
| 2165 | if (!priv.seen_dot || !priv.seen_dot_dot) { |
| 2166 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2167 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2168 | /* |
| 2169 | * XXX: Is it really safe to allow an unlink to continue? |
| 2170 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2171 | return 1; |
| 2172 | } |
| 2173 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 2174 | return !priv.seen_other; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2175 | } |
| 2176 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2177 | /* |
| 2178 | * Fills "." and ".." dirents in a new directory block. Returns dirent for |
| 2179 | * "..", which might be used during creation of a directory with a trailing |
| 2180 | * header. It is otherwise safe to ignore the return code. |
| 2181 | */ |
| 2182 | static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode, |
| 2183 | struct inode *parent, |
| 2184 | char *start, |
| 2185 | unsigned int size) |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2186 | { |
| 2187 | struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; |
| 2188 | |
| 2189 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); |
| 2190 | de->name_len = 1; |
| 2191 | de->rec_len = |
| 2192 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 2193 | strcpy(de->name, "."); |
| 2194 | ocfs2_set_de_type(de, S_IFDIR); |
| 2195 | |
| 2196 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); |
| 2197 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); |
| 2198 | de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); |
| 2199 | de->name_len = 2; |
| 2200 | strcpy(de->name, ".."); |
| 2201 | ocfs2_set_de_type(de, S_IFDIR); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2202 | |
| 2203 | return de; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* |
| 2207 | * This works together with code in ocfs2_mknod_locked() which sets |
| 2208 | * the inline-data flag and initializes the inline-data section. |
| 2209 | */ |
| 2210 | static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, |
| 2211 | handle_t *handle, |
| 2212 | struct inode *parent, |
| 2213 | struct inode *inode, |
| 2214 | struct buffer_head *di_bh) |
| 2215 | { |
| 2216 | int ret; |
| 2217 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2218 | struct ocfs2_inline_data *data = &di->id2.i_data; |
| 2219 | unsigned int size = le16_to_cpu(data->id_count); |
| 2220 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2221 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2222 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2223 | if (ret) { |
| 2224 | mlog_errno(ret); |
| 2225 | goto out; |
| 2226 | } |
| 2227 | |
| 2228 | ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2229 | ocfs2_journal_dirty(handle, di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2230 | |
| 2231 | i_size_write(inode, size); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2232 | set_nlink(inode, 2); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2233 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 2234 | |
| 2235 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); |
| 2236 | if (ret < 0) |
| 2237 | mlog_errno(ret); |
| 2238 | |
| 2239 | out: |
| 2240 | return ret; |
| 2241 | } |
| 2242 | |
| 2243 | static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, |
| 2244 | handle_t *handle, |
| 2245 | struct inode *parent, |
| 2246 | struct inode *inode, |
| 2247 | struct buffer_head *fe_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2248 | struct ocfs2_alloc_context *data_ac, |
| 2249 | struct buffer_head **ret_new_bh) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2250 | { |
| 2251 | int status; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2252 | unsigned int size = osb->sb->s_blocksize; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2253 | struct buffer_head *new_bh = NULL; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2254 | struct ocfs2_dir_entry *de; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2255 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2256 | if (ocfs2_new_dir_wants_trailer(inode)) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2257 | size = ocfs2_dir_trailer_blk_off(parent->i_sb); |
| 2258 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2259 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, |
| 2260 | data_ac, NULL, &new_bh); |
| 2261 | if (status < 0) { |
| 2262 | mlog_errno(status); |
| 2263 | goto bail; |
| 2264 | } |
| 2265 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 2266 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2267 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2268 | status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2269 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2270 | if (status < 0) { |
| 2271 | mlog_errno(status); |
| 2272 | goto bail; |
| 2273 | } |
| 2274 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); |
| 2275 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2276 | de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2277 | if (ocfs2_new_dir_wants_trailer(inode)) { |
| 2278 | int size = le16_to_cpu(de->rec_len); |
| 2279 | |
| 2280 | /* |
| 2281 | * Figure out the size of the hole left over after |
| 2282 | * insertion of '.' and '..'. The trailer wants this |
| 2283 | * information. |
| 2284 | */ |
| 2285 | size -= OCFS2_DIR_REC_LEN(2); |
| 2286 | size -= sizeof(struct ocfs2_dir_block_trailer); |
| 2287 | |
| 2288 | ocfs2_init_dir_trailer(inode, new_bh, size); |
| 2289 | } |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2290 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2291 | ocfs2_journal_dirty(handle, new_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2292 | |
| 2293 | i_size_write(inode, inode->i_sb->s_blocksize); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2294 | set_nlink(inode, 2); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2295 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 2296 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 2297 | if (status < 0) { |
| 2298 | mlog_errno(status); |
| 2299 | goto bail; |
| 2300 | } |
| 2301 | |
| 2302 | status = 0; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2303 | if (ret_new_bh) { |
| 2304 | *ret_new_bh = new_bh; |
| 2305 | new_bh = NULL; |
| 2306 | } |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2307 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 2308 | brelse(new_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2309 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 2310 | return status; |
| 2311 | } |
| 2312 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2313 | static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb, |
| 2314 | handle_t *handle, struct inode *dir, |
| 2315 | struct buffer_head *di_bh, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2316 | struct buffer_head *dirdata_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2317 | struct ocfs2_alloc_context *meta_ac, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2318 | int dx_inline, u32 num_entries, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2319 | struct buffer_head **ret_dx_root_bh) |
| 2320 | { |
| 2321 | int ret; |
| 2322 | struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; |
| 2323 | u16 dr_suballoc_bit; |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2324 | u64 suballoc_loc, dr_blkno; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2325 | unsigned int num_bits; |
| 2326 | struct buffer_head *dx_root_bh = NULL; |
| 2327 | struct ocfs2_dx_root_block *dx_root; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2328 | struct ocfs2_dir_block_trailer *trailer = |
| 2329 | ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2330 | |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2331 | ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc, |
| 2332 | &dr_suballoc_bit, &num_bits, &dr_blkno); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2333 | if (ret) { |
| 2334 | mlog_errno(ret); |
| 2335 | goto out; |
| 2336 | } |
| 2337 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 2338 | trace_ocfs2_dx_dir_attach_index( |
| 2339 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 2340 | (unsigned long long)dr_blkno); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2341 | |
| 2342 | dx_root_bh = sb_getblk(osb->sb, dr_blkno); |
| 2343 | if (dx_root_bh == NULL) { |
Rui Xiang | 7391a29 | 2013-11-12 15:06:54 -0800 | [diff] [blame] | 2344 | ret = -ENOMEM; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2345 | goto out; |
| 2346 | } |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 2347 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2348 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2349 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2350 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2351 | if (ret < 0) { |
| 2352 | mlog_errno(ret); |
| 2353 | goto out; |
| 2354 | } |
| 2355 | |
| 2356 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 2357 | memset(dx_root, 0, osb->sb->s_blocksize); |
| 2358 | strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE); |
Tiger Yang | b89c542 | 2010-01-25 14:11:06 +0800 | [diff] [blame] | 2359 | dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot); |
Joel Becker | 2b6cb57 | 2010-03-26 10:09:15 +0800 | [diff] [blame] | 2360 | dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2361 | dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit); |
| 2362 | dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation); |
| 2363 | dx_root->dr_blkno = cpu_to_le64(dr_blkno); |
| 2364 | dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2365 | dx_root->dr_num_entries = cpu_to_le32(num_entries); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2366 | if (le16_to_cpu(trailer->db_free_rec_len)) |
| 2367 | dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr); |
| 2368 | else |
| 2369 | dx_root->dr_free_blk = cpu_to_le64(0); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2370 | |
| 2371 | if (dx_inline) { |
| 2372 | dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE; |
| 2373 | dx_root->dr_entries.de_count = |
| 2374 | cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb)); |
| 2375 | } else { |
| 2376 | dx_root->dr_list.l_count = |
| 2377 | cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); |
| 2378 | } |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2379 | ocfs2_journal_dirty(handle, dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2380 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2381 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2382 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2383 | if (ret) { |
| 2384 | mlog_errno(ret); |
| 2385 | goto out; |
| 2386 | } |
| 2387 | |
| 2388 | di->i_dx_root = cpu_to_le64(dr_blkno); |
| 2389 | |
Tao Ma | 8ac33dc | 2010-12-15 16:30:00 +0800 | [diff] [blame] | 2390 | spin_lock(&OCFS2_I(dir)->ip_lock); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2391 | OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL; |
| 2392 | di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); |
Tao Ma | 8ac33dc | 2010-12-15 16:30:00 +0800 | [diff] [blame] | 2393 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2394 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2395 | ocfs2_journal_dirty(handle, di_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2396 | |
| 2397 | *ret_dx_root_bh = dx_root_bh; |
| 2398 | dx_root_bh = NULL; |
| 2399 | |
| 2400 | out: |
| 2401 | brelse(dx_root_bh); |
| 2402 | return ret; |
| 2403 | } |
| 2404 | |
| 2405 | static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb, |
| 2406 | handle_t *handle, struct inode *dir, |
| 2407 | struct buffer_head **dx_leaves, |
| 2408 | int num_dx_leaves, u64 start_blk) |
| 2409 | { |
| 2410 | int ret, i; |
| 2411 | struct ocfs2_dx_leaf *dx_leaf; |
| 2412 | struct buffer_head *bh; |
| 2413 | |
| 2414 | for (i = 0; i < num_dx_leaves; i++) { |
| 2415 | bh = sb_getblk(osb->sb, start_blk + i); |
| 2416 | if (bh == NULL) { |
Rui Xiang | 7391a29 | 2013-11-12 15:06:54 -0800 | [diff] [blame] | 2417 | ret = -ENOMEM; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2418 | goto out; |
| 2419 | } |
| 2420 | dx_leaves[i] = bh; |
| 2421 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 2422 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2423 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2424 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2425 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 2426 | if (ret < 0) { |
| 2427 | mlog_errno(ret); |
| 2428 | goto out; |
| 2429 | } |
| 2430 | |
| 2431 | dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data; |
| 2432 | |
| 2433 | memset(dx_leaf, 0, osb->sb->s_blocksize); |
| 2434 | strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE); |
| 2435 | dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation); |
| 2436 | dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr); |
| 2437 | dx_leaf->dl_list.de_count = |
| 2438 | cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb)); |
| 2439 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 2440 | trace_ocfs2_dx_dir_format_cluster( |
| 2441 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 2442 | (unsigned long long)bh->b_blocknr, |
| 2443 | le16_to_cpu(dx_leaf->dl_list.de_count)); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2444 | |
| 2445 | ocfs2_journal_dirty(handle, bh); |
| 2446 | } |
| 2447 | |
| 2448 | ret = 0; |
| 2449 | out: |
| 2450 | return ret; |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | * Allocates and formats a new cluster for use in an indexed dir |
| 2455 | * leaf. This version will not do the extent insert, so that it can be |
| 2456 | * used by operations which need careful ordering. |
| 2457 | */ |
| 2458 | static int __ocfs2_dx_dir_new_cluster(struct inode *dir, |
| 2459 | u32 cpos, handle_t *handle, |
| 2460 | struct ocfs2_alloc_context *data_ac, |
| 2461 | struct buffer_head **dx_leaves, |
| 2462 | int num_dx_leaves, u64 *ret_phys_blkno) |
| 2463 | { |
| 2464 | int ret; |
| 2465 | u32 phys, num; |
| 2466 | u64 phys_blkno; |
| 2467 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 2468 | |
| 2469 | /* |
| 2470 | * XXX: For create, this should claim cluster for the index |
| 2471 | * *before* the unindexed insert so that we have a better |
| 2472 | * chance of contiguousness as the directory grows in number |
| 2473 | * of entries. |
| 2474 | */ |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2475 | ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2476 | if (ret) { |
| 2477 | mlog_errno(ret); |
| 2478 | goto out; |
| 2479 | } |
| 2480 | |
| 2481 | /* |
| 2482 | * Format the new cluster first. That way, we're inserting |
| 2483 | * valid data. |
| 2484 | */ |
| 2485 | phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys); |
| 2486 | ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves, |
| 2487 | num_dx_leaves, phys_blkno); |
| 2488 | if (ret) { |
| 2489 | mlog_errno(ret); |
| 2490 | goto out; |
| 2491 | } |
| 2492 | |
| 2493 | *ret_phys_blkno = phys_blkno; |
| 2494 | out: |
| 2495 | return ret; |
| 2496 | } |
| 2497 | |
| 2498 | static int ocfs2_dx_dir_new_cluster(struct inode *dir, |
| 2499 | struct ocfs2_extent_tree *et, |
| 2500 | u32 cpos, handle_t *handle, |
| 2501 | struct ocfs2_alloc_context *data_ac, |
| 2502 | struct ocfs2_alloc_context *meta_ac, |
| 2503 | struct buffer_head **dx_leaves, |
| 2504 | int num_dx_leaves) |
| 2505 | { |
| 2506 | int ret; |
| 2507 | u64 phys_blkno; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2508 | |
| 2509 | ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves, |
| 2510 | num_dx_leaves, &phys_blkno); |
| 2511 | if (ret) { |
| 2512 | mlog_errno(ret); |
| 2513 | goto out; |
| 2514 | } |
| 2515 | |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 2516 | ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2517 | meta_ac); |
| 2518 | if (ret) |
| 2519 | mlog_errno(ret); |
| 2520 | out: |
| 2521 | return ret; |
| 2522 | } |
| 2523 | |
| 2524 | static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb, |
| 2525 | int *ret_num_leaves) |
| 2526 | { |
| 2527 | int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1); |
| 2528 | struct buffer_head **dx_leaves; |
| 2529 | |
| 2530 | dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *), |
| 2531 | GFP_NOFS); |
| 2532 | if (dx_leaves && ret_num_leaves) |
| 2533 | *ret_num_leaves = num_dx_leaves; |
| 2534 | |
| 2535 | return dx_leaves; |
| 2536 | } |
| 2537 | |
| 2538 | static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb, |
| 2539 | handle_t *handle, |
| 2540 | struct inode *parent, |
| 2541 | struct inode *inode, |
| 2542 | struct buffer_head *di_bh, |
| 2543 | struct ocfs2_alloc_context *data_ac, |
| 2544 | struct ocfs2_alloc_context *meta_ac) |
| 2545 | { |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2546 | int ret; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2547 | struct buffer_head *leaf_bh = NULL; |
| 2548 | struct buffer_head *dx_root_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2549 | struct ocfs2_dx_hinfo hinfo; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2550 | struct ocfs2_dx_root_block *dx_root; |
| 2551 | struct ocfs2_dx_entry_list *entry_list; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2552 | |
| 2553 | /* |
| 2554 | * Our strategy is to create the directory as though it were |
| 2555 | * unindexed, then add the index block. This works with very |
| 2556 | * little complication since the state of a new directory is a |
| 2557 | * very well known quantity. |
| 2558 | * |
| 2559 | * Essentially, we have two dirents ("." and ".."), in the 1st |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2560 | * block which need indexing. These are easily inserted into |
| 2561 | * the index block. |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2562 | */ |
| 2563 | |
| 2564 | ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh, |
| 2565 | data_ac, &leaf_bh); |
| 2566 | if (ret) { |
| 2567 | mlog_errno(ret); |
| 2568 | goto out; |
| 2569 | } |
| 2570 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2571 | ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2572 | meta_ac, 1, 2, &dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2573 | if (ret) { |
| 2574 | mlog_errno(ret); |
| 2575 | goto out; |
| 2576 | } |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2577 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 2578 | entry_list = &dx_root->dr_entries; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2579 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2580 | /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */ |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2581 | ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2582 | ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2583 | |
| 2584 | ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2585 | ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2586 | |
| 2587 | out: |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2588 | brelse(dx_root_bh); |
| 2589 | brelse(leaf_bh); |
| 2590 | return ret; |
| 2591 | } |
| 2592 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2593 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
| 2594 | handle_t *handle, |
| 2595 | struct inode *parent, |
| 2596 | struct inode *inode, |
| 2597 | struct buffer_head *fe_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2598 | struct ocfs2_alloc_context *data_ac, |
| 2599 | struct ocfs2_alloc_context *meta_ac) |
| 2600 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2601 | { |
| 2602 | BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); |
| 2603 | |
| 2604 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 2605 | return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); |
| 2606 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2607 | if (ocfs2_supports_indexed_dirs(osb)) |
| 2608 | return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh, |
| 2609 | data_ac, meta_ac); |
| 2610 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2611 | return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2612 | data_ac, NULL); |
| 2613 | } |
| 2614 | |
| 2615 | static int ocfs2_dx_dir_index_block(struct inode *dir, |
| 2616 | handle_t *handle, |
| 2617 | struct buffer_head **dx_leaves, |
| 2618 | int num_dx_leaves, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2619 | u32 *num_dx_entries, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2620 | struct buffer_head *dirent_bh) |
| 2621 | { |
Tao Ma | 0fba813 | 2009-03-19 05:08:43 +0800 | [diff] [blame] | 2622 | int ret = 0, namelen, i; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2623 | char *de_buf, *limit; |
| 2624 | struct ocfs2_dir_entry *de; |
| 2625 | struct buffer_head *dx_leaf_bh; |
| 2626 | struct ocfs2_dx_hinfo hinfo; |
| 2627 | u64 dirent_blk = dirent_bh->b_blocknr; |
| 2628 | |
| 2629 | de_buf = dirent_bh->b_data; |
| 2630 | limit = de_buf + dir->i_sb->s_blocksize; |
| 2631 | |
| 2632 | while (de_buf < limit) { |
| 2633 | de = (struct ocfs2_dir_entry *)de_buf; |
| 2634 | |
| 2635 | namelen = de->name_len; |
| 2636 | if (!namelen || !de->inode) |
| 2637 | goto inc; |
| 2638 | |
| 2639 | ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo); |
| 2640 | |
| 2641 | i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo); |
| 2642 | dx_leaf_bh = dx_leaves[i]; |
| 2643 | |
| 2644 | ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo, |
| 2645 | dirent_blk, dx_leaf_bh); |
| 2646 | if (ret) { |
| 2647 | mlog_errno(ret); |
| 2648 | goto out; |
| 2649 | } |
| 2650 | |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2651 | *num_dx_entries = *num_dx_entries + 1; |
| 2652 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2653 | inc: |
| 2654 | de_buf += le16_to_cpu(de->rec_len); |
| 2655 | } |
| 2656 | |
| 2657 | out: |
| 2658 | return ret; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2659 | } |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2660 | |
| 2661 | /* |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2662 | * XXX: This expects dx_root_bh to already be part of the transaction. |
| 2663 | */ |
| 2664 | static void ocfs2_dx_dir_index_root_block(struct inode *dir, |
| 2665 | struct buffer_head *dx_root_bh, |
| 2666 | struct buffer_head *dirent_bh) |
| 2667 | { |
| 2668 | char *de_buf, *limit; |
| 2669 | struct ocfs2_dx_root_block *dx_root; |
| 2670 | struct ocfs2_dir_entry *de; |
| 2671 | struct ocfs2_dx_hinfo hinfo; |
| 2672 | u64 dirent_blk = dirent_bh->b_blocknr; |
| 2673 | |
| 2674 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 2675 | |
| 2676 | de_buf = dirent_bh->b_data; |
| 2677 | limit = de_buf + dir->i_sb->s_blocksize; |
| 2678 | |
| 2679 | while (de_buf < limit) { |
| 2680 | de = (struct ocfs2_dir_entry *)de_buf; |
| 2681 | |
| 2682 | if (!de->name_len || !de->inode) |
| 2683 | goto inc; |
| 2684 | |
| 2685 | ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo); |
| 2686 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 2687 | trace_ocfs2_dx_dir_index_root_block( |
| 2688 | (unsigned long long)dir->i_ino, |
| 2689 | hinfo.major_hash, hinfo.minor_hash, |
| 2690 | de->name_len, de->name, |
| 2691 | le16_to_cpu(dx_root->dr_entries.de_num_used)); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2692 | |
| 2693 | ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo, |
| 2694 | dirent_blk); |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2695 | |
| 2696 | le32_add_cpu(&dx_root->dr_num_entries, 1); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2697 | inc: |
| 2698 | de_buf += le16_to_cpu(de->rec_len); |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | /* |
| 2703 | * Count the number of inline directory entries in di_bh and compare |
| 2704 | * them against the number of entries we can hold in an inline dx root |
| 2705 | * block. |
| 2706 | */ |
| 2707 | static int ocfs2_new_dx_should_be_inline(struct inode *dir, |
| 2708 | struct buffer_head *di_bh) |
| 2709 | { |
| 2710 | int dirent_count = 0; |
| 2711 | char *de_buf, *limit; |
| 2712 | struct ocfs2_dir_entry *de; |
| 2713 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2714 | |
| 2715 | de_buf = di->id2.i_data.id_data; |
| 2716 | limit = de_buf + i_size_read(dir); |
| 2717 | |
| 2718 | while (de_buf < limit) { |
| 2719 | de = (struct ocfs2_dir_entry *)de_buf; |
| 2720 | |
| 2721 | if (de->name_len && de->inode) |
| 2722 | dirent_count++; |
| 2723 | |
| 2724 | de_buf += le16_to_cpu(de->rec_len); |
| 2725 | } |
| 2726 | |
| 2727 | /* We are careful to leave room for one extra record. */ |
| 2728 | return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb); |
| 2729 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2730 | |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2731 | /* |
| 2732 | * Expand rec_len of the rightmost dirent in a directory block so that it |
| 2733 | * contains the end of our valid space for dirents. We do this during |
| 2734 | * expansion from an inline directory to one with extents. The first dir block |
| 2735 | * in that case is taken from the inline data portion of the inode block. |
| 2736 | * |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2737 | * This will also return the largest amount of contiguous space for a dirent |
| 2738 | * in the block. That value is *not* necessarily the last dirent, even after |
| 2739 | * expansion. The directory indexing code wants this value for free space |
| 2740 | * accounting. We do this here since we're already walking the entire dir |
| 2741 | * block. |
| 2742 | * |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2743 | * We add the dir trailer if this filesystem wants it. |
| 2744 | */ |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2745 | static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size, |
| 2746 | struct inode *dir) |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2747 | { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2748 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2749 | struct ocfs2_dir_entry *de; |
| 2750 | struct ocfs2_dir_entry *prev_de; |
| 2751 | char *de_buf, *limit; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2752 | unsigned int new_size = sb->s_blocksize; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2753 | unsigned int bytes, this_hole; |
| 2754 | unsigned int largest_hole = 0; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2755 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2756 | if (ocfs2_new_dir_wants_trailer(dir)) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 2757 | new_size = ocfs2_dir_trailer_blk_off(sb); |
| 2758 | |
| 2759 | bytes = new_size - old_size; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2760 | |
| 2761 | limit = start + old_size; |
| 2762 | de_buf = start; |
| 2763 | de = (struct ocfs2_dir_entry *)de_buf; |
| 2764 | do { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2765 | this_hole = ocfs2_figure_dirent_hole(de); |
| 2766 | if (this_hole > largest_hole) |
| 2767 | largest_hole = this_hole; |
| 2768 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2769 | prev_de = de; |
| 2770 | de_buf += le16_to_cpu(de->rec_len); |
| 2771 | de = (struct ocfs2_dir_entry *)de_buf; |
| 2772 | } while (de_buf < limit); |
| 2773 | |
| 2774 | le16_add_cpu(&prev_de->rec_len, bytes); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2775 | |
| 2776 | /* We need to double check this after modification of the final |
| 2777 | * dirent. */ |
| 2778 | this_hole = ocfs2_figure_dirent_hole(prev_de); |
| 2779 | if (this_hole > largest_hole) |
| 2780 | largest_hole = this_hole; |
| 2781 | |
| 2782 | if (largest_hole >= OCFS2_DIR_MIN_REC_LEN) |
| 2783 | return largest_hole; |
| 2784 | return 0; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | /* |
| 2788 | * We allocate enough clusters to fulfill "blocks_wanted", but set |
| 2789 | * i_size to exactly one block. Ocfs2_extend_dir() will handle the |
| 2790 | * rest automatically for us. |
| 2791 | * |
| 2792 | * *first_block_bh is a pointer to the 1st data block allocated to the |
| 2793 | * directory. |
| 2794 | */ |
| 2795 | static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, |
| 2796 | unsigned int blocks_wanted, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2797 | struct ocfs2_dir_lookup_result *lookup, |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2798 | struct buffer_head **first_block_bh) |
| 2799 | { |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2800 | u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2801 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2802 | int ret, i, num_dx_leaves = 0, dx_inline = 0, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2803 | credits = ocfs2_inline_to_extents_credits(sb); |
| 2804 | u64 dx_insert_blkno, blkno, |
| 2805 | bytes = blocks_wanted << sb->s_blocksize_bits; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2806 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 2807 | struct ocfs2_inode_info *oi = OCFS2_I(dir); |
Marcus Meissner | 5d44670 | 2011-05-05 10:44:11 -0700 | [diff] [blame] | 2808 | struct ocfs2_alloc_context *data_ac = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2809 | struct ocfs2_alloc_context *meta_ac = NULL; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2810 | struct buffer_head *dirdata_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2811 | struct buffer_head *dx_root_bh = NULL; |
| 2812 | struct buffer_head **dx_leaves = NULL; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2813 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 2814 | handle_t *handle; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 2815 | struct ocfs2_extent_tree et; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2816 | struct ocfs2_extent_tree dx_et; |
| 2817 | int did_quota = 0, bytes_allocated = 0; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 2818 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 2819 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2820 | |
| 2821 | alloc = ocfs2_clusters_for_bytes(sb, bytes); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2822 | dx_alloc = 0; |
| 2823 | |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 2824 | down_write(&oi->ip_alloc_sem); |
| 2825 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2826 | if (ocfs2_supports_indexed_dirs(osb)) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2827 | credits += ocfs2_add_dir_index_credits(sb); |
| 2828 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2829 | dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh); |
| 2830 | if (!dx_inline) { |
| 2831 | /* Add one more cluster for an index leaf */ |
| 2832 | dx_alloc++; |
| 2833 | dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb, |
| 2834 | &num_dx_leaves); |
| 2835 | if (!dx_leaves) { |
| 2836 | ret = -ENOMEM; |
| 2837 | mlog_errno(ret); |
| 2838 | goto out; |
| 2839 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | /* This gets us the dx_root */ |
| 2843 | ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); |
| 2844 | if (ret) { |
| 2845 | mlog_errno(ret); |
| 2846 | goto out; |
| 2847 | } |
| 2848 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2849 | |
| 2850 | /* |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2851 | * We should never need more than 2 clusters for the unindexed |
| 2852 | * tree - maximum dirent size is far less than one block. In |
| 2853 | * fact, the only time we'd need more than one cluster is if |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2854 | * blocksize == clustersize and the dirent won't fit in the |
| 2855 | * extra space that the expansion to a single block gives. As |
| 2856 | * of today, that only happens on 4k/4k file systems. |
| 2857 | */ |
| 2858 | BUG_ON(alloc > 2); |
| 2859 | |
Tao Ma | 035a571 | 2009-04-07 07:40:57 +0800 | [diff] [blame] | 2860 | ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2861 | if (ret) { |
| 2862 | mlog_errno(ret); |
| 2863 | goto out; |
| 2864 | } |
| 2865 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2866 | /* |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 2867 | * Prepare for worst case allocation scenario of two separate |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2868 | * extents in the unindexed tree. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2869 | */ |
| 2870 | if (alloc == 2) |
| 2871 | credits += OCFS2_SUBALLOC_ALLOC; |
| 2872 | |
| 2873 | handle = ocfs2_start_trans(osb, credits); |
| 2874 | if (IS_ERR(handle)) { |
| 2875 | ret = PTR_ERR(handle); |
| 2876 | mlog_errno(ret); |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 2877 | goto out; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2878 | } |
| 2879 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 2880 | ret = dquot_alloc_space_nodirty(dir, |
| 2881 | ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc)); |
| 2882 | if (ret) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2883 | goto out_commit; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 2884 | did_quota = 1; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2885 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2886 | if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2887 | /* |
| 2888 | * Allocate our index cluster first, to maximize the |
| 2889 | * possibility that unindexed leaves grow |
| 2890 | * contiguously. |
| 2891 | */ |
| 2892 | ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, |
| 2893 | dx_leaves, num_dx_leaves, |
| 2894 | &dx_insert_blkno); |
| 2895 | if (ret) { |
| 2896 | mlog_errno(ret); |
| 2897 | goto out_commit; |
| 2898 | } |
| 2899 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); |
| 2900 | } |
| 2901 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2902 | /* |
| 2903 | * Try to claim as many clusters as the bitmap can give though |
| 2904 | * if we only get one now, that's enough to continue. The rest |
| 2905 | * will be claimed after the conversion to extents. |
| 2906 | */ |
Mark Fasheh | 83f9231 | 2010-04-05 18:17:16 -0700 | [diff] [blame] | 2907 | if (ocfs2_dir_resv_allowed(osb)) |
| 2908 | data_ac->ac_resv = &oi->ip_la_data_resv; |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 2909 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2910 | if (ret) { |
| 2911 | mlog_errno(ret); |
| 2912 | goto out_commit; |
| 2913 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2914 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2915 | |
| 2916 | /* |
| 2917 | * Operations are carefully ordered so that we set up the new |
| 2918 | * data block first. The conversion from inline data to |
| 2919 | * extents follows. |
| 2920 | */ |
| 2921 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); |
| 2922 | dirdata_bh = sb_getblk(sb, blkno); |
| 2923 | if (!dirdata_bh) { |
Rui Xiang | 7391a29 | 2013-11-12 15:06:54 -0800 | [diff] [blame] | 2924 | ret = -ENOMEM; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2925 | mlog_errno(ret); |
| 2926 | goto out_commit; |
| 2927 | } |
| 2928 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 2929 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2930 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2931 | ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2932 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2933 | if (ret) { |
| 2934 | mlog_errno(ret); |
| 2935 | goto out_commit; |
| 2936 | } |
| 2937 | |
| 2938 | memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); |
| 2939 | memset(dirdata_bh->b_data + i_size_read(dir), 0, |
| 2940 | sb->s_blocksize - i_size_read(dir)); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 2941 | i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir); |
| 2942 | if (ocfs2_new_dir_wants_trailer(dir)) { |
| 2943 | /* |
| 2944 | * Prepare the dir trailer up front. It will otherwise look |
| 2945 | * like a valid dirent. Even if inserting the index fails |
| 2946 | * (unlikely), then all we'll have done is given first dir |
| 2947 | * block a small amount of fragmentation. |
| 2948 | */ |
| 2949 | ocfs2_init_dir_trailer(dir, dirdata_bh, i); |
| 2950 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2951 | |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 2952 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 2953 | ocfs2_journal_dirty(handle, dirdata_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2954 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2955 | if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) { |
| 2956 | /* |
| 2957 | * Dx dirs with an external cluster need to do this up |
| 2958 | * front. Inline dx root's get handled later, after |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2959 | * we've allocated our root block. We get passed back |
| 2960 | * a total number of items so that dr_num_entries can |
| 2961 | * be correctly set once the dx_root has been |
| 2962 | * allocated. |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 2963 | */ |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2964 | ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 2965 | num_dx_leaves, &num_dx_entries, |
| 2966 | dirdata_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 2967 | if (ret) { |
| 2968 | mlog_errno(ret); |
| 2969 | goto out_commit; |
| 2970 | } |
| 2971 | } |
| 2972 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2973 | /* |
| 2974 | * Set extent, i_size, etc on the directory. After this, the |
| 2975 | * inode should contain the same exact dirents as before and |
| 2976 | * be fully accessible from system calls. |
| 2977 | * |
| 2978 | * We let the later dirent insert modify c/mtime - to the user |
| 2979 | * the data hasn't changed. |
| 2980 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 2981 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 2982 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 2983 | if (ret) { |
| 2984 | mlog_errno(ret); |
| 2985 | goto out_commit; |
| 2986 | } |
| 2987 | |
| 2988 | spin_lock(&oi->ip_lock); |
| 2989 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 2990 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 2991 | spin_unlock(&oi->ip_lock); |
| 2992 | |
| 2993 | ocfs2_dinode_new_extent_list(dir, di); |
| 2994 | |
| 2995 | i_size_write(dir, sb->s_blocksize); |
| 2996 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 2997 | |
| 2998 | di->i_size = cpu_to_le64(sb->s_blocksize); |
| 2999 | di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec); |
| 3000 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec); |
Darrick J. Wong | 6fdb702 | 2014-04-03 14:47:08 -0700 | [diff] [blame] | 3001 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * This should never fail as our extent list is empty and all |
| 3005 | * related blocks have been journaled already. |
| 3006 | */ |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 3007 | ret = ocfs2_insert_extent(handle, &et, 0, blkno, len, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 3008 | 0, NULL); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3009 | if (ret) { |
| 3010 | mlog_errno(ret); |
Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 3011 | goto out_commit; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3012 | } |
| 3013 | |
Mark Fasheh | 9780eb6 | 2008-08-05 11:32:46 -0700 | [diff] [blame] | 3014 | /* |
| 3015 | * Set i_blocks after the extent insert for the most up to |
| 3016 | * date ip_clusters value. |
| 3017 | */ |
| 3018 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
| 3019 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3020 | ocfs2_journal_dirty(handle, di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3021 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3022 | if (ocfs2_supports_indexed_dirs(osb)) { |
| 3023 | ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh, |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3024 | dirdata_bh, meta_ac, dx_inline, |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 3025 | num_dx_entries, &dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3026 | if (ret) { |
| 3027 | mlog_errno(ret); |
| 3028 | goto out_commit; |
| 3029 | } |
| 3030 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 3031 | if (dx_inline) { |
| 3032 | ocfs2_dx_dir_index_root_block(dir, dx_root_bh, |
| 3033 | dirdata_bh); |
| 3034 | } else { |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 3035 | ocfs2_init_dx_root_extent_tree(&dx_et, |
| 3036 | INODE_CACHE(dir), |
| 3037 | dx_root_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 3038 | ret = ocfs2_insert_extent(handle, &dx_et, 0, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 3039 | dx_insert_blkno, 1, 0, NULL); |
| 3040 | if (ret) |
| 3041 | mlog_errno(ret); |
| 3042 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3043 | } |
| 3044 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3045 | /* |
| 3046 | * We asked for two clusters, but only got one in the 1st |
| 3047 | * pass. Claim the 2nd cluster as a separate extent. |
| 3048 | */ |
| 3049 | if (alloc > len) { |
Joel Becker | 1ed9b77 | 2010-05-06 13:59:06 +0800 | [diff] [blame] | 3050 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3051 | &len); |
| 3052 | if (ret) { |
| 3053 | mlog_errno(ret); |
| 3054 | goto out_commit; |
| 3055 | } |
| 3056 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); |
| 3057 | |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 3058 | ret = ocfs2_insert_extent(handle, &et, 1, |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 3059 | blkno, len, 0, NULL); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3060 | if (ret) { |
| 3061 | mlog_errno(ret); |
Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 3062 | goto out_commit; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3063 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3064 | bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | *first_block_bh = dirdata_bh; |
| 3068 | dirdata_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3069 | if (ocfs2_supports_indexed_dirs(osb)) { |
| 3070 | unsigned int off; |
| 3071 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 3072 | if (!dx_inline) { |
| 3073 | /* |
| 3074 | * We need to return the correct block within the |
| 3075 | * cluster which should hold our entry. |
| 3076 | */ |
| 3077 | off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), |
| 3078 | &lookup->dl_hinfo); |
| 3079 | get_bh(dx_leaves[off]); |
| 3080 | lookup->dl_dx_leaf_bh = dx_leaves[off]; |
| 3081 | } |
| 3082 | lookup->dl_dx_root_bh = dx_root_bh; |
| 3083 | dx_root_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3084 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3085 | |
| 3086 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 3087 | if (ret < 0 && did_quota) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 3088 | dquot_free_space_nodirty(dir, bytes_allocated); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3089 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3090 | ocfs2_commit_trans(osb, handle); |
| 3091 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3092 | out: |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 3093 | up_write(&oi->ip_alloc_sem); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3094 | if (data_ac) |
| 3095 | ocfs2_free_alloc_context(data_ac); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3096 | if (meta_ac) |
| 3097 | ocfs2_free_alloc_context(meta_ac); |
| 3098 | |
| 3099 | if (dx_leaves) { |
| 3100 | for (i = 0; i < num_dx_leaves; i++) |
| 3101 | brelse(dx_leaves[i]); |
| 3102 | kfree(dx_leaves); |
| 3103 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3104 | |
| 3105 | brelse(dirdata_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3106 | brelse(dx_root_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3107 | |
| 3108 | return ret; |
| 3109 | } |
| 3110 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3111 | /* returns a bh of the 1st new block in the allocation. */ |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 3112 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 3113 | handle_t *handle, |
| 3114 | struct inode *dir, |
| 3115 | struct buffer_head *parent_fe_bh, |
| 3116 | struct ocfs2_alloc_context *data_ac, |
| 3117 | struct ocfs2_alloc_context *meta_ac, |
| 3118 | struct buffer_head **new_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3119 | { |
| 3120 | int status; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 3121 | int extend, did_quota = 0; |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 3122 | u64 p_blkno, v_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3123 | |
| 3124 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 3125 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); |
| 3126 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 3127 | |
| 3128 | if (extend) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 3129 | u32 offset = OCFS2_I(dir)->ip_clusters; |
| 3130 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 3131 | status = dquot_alloc_space_nodirty(dir, |
| 3132 | ocfs2_clusters_to_bytes(sb, 1)); |
| 3133 | if (status) |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 3134 | goto bail; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 3135 | did_quota = 1; |
| 3136 | |
Tao Ma | 0eb8d47e | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 3137 | status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, |
| 3138 | 1, 0, parent_fe_bh, handle, |
| 3139 | data_ac, meta_ac, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3140 | BUG_ON(status == -EAGAIN); |
| 3141 | if (status < 0) { |
| 3142 | mlog_errno(status); |
| 3143 | goto bail; |
| 3144 | } |
| 3145 | } |
| 3146 | |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 3147 | v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); |
| 3148 | status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3149 | if (status < 0) { |
| 3150 | mlog_errno(status); |
| 3151 | goto bail; |
| 3152 | } |
| 3153 | |
| 3154 | *new_bh = sb_getblk(sb, p_blkno); |
| 3155 | if (!*new_bh) { |
Rui Xiang | 7391a29 | 2013-11-12 15:06:54 -0800 | [diff] [blame] | 3156 | status = -ENOMEM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3157 | mlog_errno(status); |
| 3158 | goto bail; |
| 3159 | } |
| 3160 | status = 0; |
| 3161 | bail: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 3162 | if (did_quota && status < 0) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 3163 | dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3164 | return status; |
| 3165 | } |
| 3166 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3167 | /* |
| 3168 | * Assumes you already have a cluster lock on the directory. |
| 3169 | * |
| 3170 | * 'blocks_wanted' is only used if we have an inline directory which |
| 3171 | * is to be turned into an extent based one. The size of the dirent to |
| 3172 | * insert might be larger than the space gained by growing to just one |
| 3173 | * block, so we may have to grow the inode by two blocks in that case. |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3174 | * |
| 3175 | * If the directory is already indexed, dx_root_bh must be provided. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3176 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3177 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 3178 | struct inode *dir, |
| 3179 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3180 | unsigned int blocks_wanted, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3181 | struct ocfs2_dir_lookup_result *lookup, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3182 | struct buffer_head **new_de_bh) |
| 3183 | { |
| 3184 | int status = 0; |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 3185 | int credits, num_free_extents, drop_alloc_sem = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3186 | loff_t dir_i_size; |
| 3187 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 3188 | struct ocfs2_extent_list *el = &fe->id2.i_list; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3189 | struct ocfs2_alloc_context *data_ac = NULL; |
| 3190 | struct ocfs2_alloc_context *meta_ac = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 3191 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3192 | struct buffer_head *new_bh = NULL; |
| 3193 | struct ocfs2_dir_entry * de; |
| 3194 | struct super_block *sb = osb->sb; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 3195 | struct ocfs2_extent_tree et; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3196 | struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3197 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3198 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3199 | /* |
| 3200 | * This would be a code error as an inline directory should |
| 3201 | * never have an index root. |
| 3202 | */ |
| 3203 | BUG_ON(dx_root_bh); |
| 3204 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3205 | status = ocfs2_expand_inline_dir(dir, parent_fe_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3206 | blocks_wanted, lookup, |
| 3207 | &new_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3208 | if (status) { |
| 3209 | mlog_errno(status); |
| 3210 | goto bail; |
| 3211 | } |
| 3212 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3213 | /* Expansion from inline to an indexed directory will |
| 3214 | * have given us this. */ |
| 3215 | dx_root_bh = lookup->dl_dx_root_bh; |
| 3216 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3217 | if (blocks_wanted == 1) { |
| 3218 | /* |
| 3219 | * If the new dirent will fit inside the space |
| 3220 | * created by pushing out to one block, then |
| 3221 | * we can complete the operation |
| 3222 | * here. Otherwise we have to expand i_size |
| 3223 | * and format the 2nd block below. |
| 3224 | */ |
| 3225 | BUG_ON(new_bh == NULL); |
| 3226 | goto bail_bh; |
| 3227 | } |
| 3228 | |
| 3229 | /* |
| 3230 | * Get rid of 'new_bh' - we want to format the 2nd |
| 3231 | * data block and return that instead. |
| 3232 | */ |
| 3233 | brelse(new_bh); |
| 3234 | new_bh = NULL; |
| 3235 | |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 3236 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
| 3237 | drop_alloc_sem = 1; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3238 | dir_i_size = i_size_read(dir); |
| 3239 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 3240 | goto do_extend; |
| 3241 | } |
| 3242 | |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 3243 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
| 3244 | drop_alloc_sem = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3245 | dir_i_size = i_size_read(dir); |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 3246 | trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 3247 | dir_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3248 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3249 | /* dir->i_size is always block aligned. */ |
| 3250 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 3251 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { |
| 3252 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 3253 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), |
| 3254 | parent_fe_bh); |
Joel Becker | 3d03a30 | 2009-02-12 17:49:26 -0800 | [diff] [blame] | 3255 | num_free_extents = ocfs2_num_free_extents(osb, &et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3256 | if (num_free_extents < 0) { |
| 3257 | status = num_free_extents; |
| 3258 | mlog_errno(status); |
| 3259 | goto bail; |
| 3260 | } |
| 3261 | |
| 3262 | if (!num_free_extents) { |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 3263 | status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3264 | if (status < 0) { |
| 3265 | if (status != -ENOSPC) |
| 3266 | mlog_errno(status); |
| 3267 | goto bail; |
| 3268 | } |
| 3269 | } |
| 3270 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 3271 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3272 | if (status < 0) { |
| 3273 | if (status != -ENOSPC) |
| 3274 | mlog_errno(status); |
| 3275 | goto bail; |
| 3276 | } |
| 3277 | |
Mark Fasheh | 83f9231 | 2010-04-05 18:17:16 -0700 | [diff] [blame] | 3278 | if (ocfs2_dir_resv_allowed(osb)) |
| 3279 | data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv; |
Mark Fasheh | e3b4a97 | 2009-12-07 13:16:07 -0800 | [diff] [blame] | 3280 | |
Goldwyn Rodrigues | 06f9da6 | 2013-11-12 15:06:52 -0800 | [diff] [blame] | 3281 | credits = ocfs2_calc_extend_credits(sb, el); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3282 | } else { |
| 3283 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 3284 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 3285 | } |
| 3286 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3287 | do_extend: |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3288 | if (ocfs2_dir_indexed(dir)) |
| 3289 | credits++; /* For attaching the new dirent block to the |
| 3290 | * dx_root */ |
| 3291 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 3292 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3293 | if (IS_ERR(handle)) { |
| 3294 | status = PTR_ERR(handle); |
| 3295 | handle = NULL; |
| 3296 | mlog_errno(status); |
| 3297 | goto bail; |
| 3298 | } |
| 3299 | |
| 3300 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, |
| 3301 | data_ac, meta_ac, &new_bh); |
| 3302 | if (status < 0) { |
| 3303 | mlog_errno(status); |
| 3304 | goto bail; |
| 3305 | } |
| 3306 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 3307 | ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3308 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3309 | status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh, |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame] | 3310 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3311 | if (status < 0) { |
| 3312 | mlog_errno(status); |
| 3313 | goto bail; |
| 3314 | } |
| 3315 | memset(new_bh->b_data, 0, sb->s_blocksize); |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3316 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3317 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 3318 | de->inode = 0; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3319 | if (ocfs2_supports_dir_trailer(dir)) { |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3320 | de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb)); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3321 | |
| 3322 | ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len)); |
| 3323 | |
| 3324 | if (ocfs2_dir_indexed(dir)) { |
| 3325 | status = ocfs2_dx_dir_link_trailer(dir, handle, |
| 3326 | dx_root_bh, new_bh); |
| 3327 | if (status) { |
| 3328 | mlog_errno(status); |
| 3329 | goto bail; |
| 3330 | } |
| 3331 | } |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3332 | } else { |
| 3333 | de->rec_len = cpu_to_le16(sb->s_blocksize); |
| 3334 | } |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 3335 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3336 | ocfs2_journal_dirty(handle, new_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3337 | |
| 3338 | dir_i_size += dir->i_sb->s_blocksize; |
| 3339 | i_size_write(dir, dir_i_size); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 3340 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3341 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 3342 | if (status < 0) { |
| 3343 | mlog_errno(status); |
| 3344 | goto bail; |
| 3345 | } |
| 3346 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3347 | bail_bh: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3348 | *new_de_bh = new_bh; |
| 3349 | get_bh(*new_de_bh); |
| 3350 | bail: |
| 3351 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 3352 | ocfs2_commit_trans(osb, handle); |
Jan Kara | edd45c0 | 2009-06-02 14:24:03 +0200 | [diff] [blame] | 3353 | if (drop_alloc_sem) |
| 3354 | up_write(&OCFS2_I(dir)->ip_alloc_sem); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3355 | |
| 3356 | if (data_ac) |
| 3357 | ocfs2_free_alloc_context(data_ac); |
| 3358 | if (meta_ac) |
| 3359 | ocfs2_free_alloc_context(meta_ac); |
| 3360 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 3361 | brelse(new_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3362 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3363 | return status; |
| 3364 | } |
| 3365 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3366 | static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, |
| 3367 | const char *name, int namelen, |
| 3368 | struct buffer_head **ret_de_bh, |
| 3369 | unsigned int *blocks_wanted) |
| 3370 | { |
| 3371 | int ret; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3372 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3373 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 3374 | struct ocfs2_dir_entry *de, *last_de = NULL; |
| 3375 | char *de_buf, *limit; |
| 3376 | unsigned long offset = 0; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3377 | unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize; |
| 3378 | |
| 3379 | /* |
| 3380 | * This calculates how many free bytes we'd have in block zero, should |
| 3381 | * this function force expansion to an extent tree. |
| 3382 | */ |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3383 | if (ocfs2_new_dir_wants_trailer(dir)) |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3384 | free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir); |
| 3385 | else |
| 3386 | free_space = dir->i_sb->s_blocksize - i_size_read(dir); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3387 | |
| 3388 | de_buf = di->id2.i_data.id_data; |
| 3389 | limit = de_buf + i_size_read(dir); |
| 3390 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 3391 | |
| 3392 | while (de_buf < limit) { |
| 3393 | de = (struct ocfs2_dir_entry *)de_buf; |
| 3394 | |
| 3395 | if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) { |
| 3396 | ret = -ENOENT; |
| 3397 | goto out; |
| 3398 | } |
| 3399 | if (ocfs2_match(namelen, name, de)) { |
| 3400 | ret = -EEXIST; |
| 3401 | goto out; |
| 3402 | } |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3403 | /* |
| 3404 | * No need to check for a trailing dirent record here as |
| 3405 | * they're not used for inline dirs. |
| 3406 | */ |
| 3407 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3408 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
| 3409 | /* Ok, we found a spot. Return this bh and let |
| 3410 | * the caller actually fill it in. */ |
| 3411 | *ret_de_bh = di_bh; |
| 3412 | get_bh(*ret_de_bh); |
| 3413 | ret = 0; |
| 3414 | goto out; |
| 3415 | } |
| 3416 | |
| 3417 | last_de = de; |
| 3418 | de_buf += le16_to_cpu(de->rec_len); |
| 3419 | offset += le16_to_cpu(de->rec_len); |
| 3420 | } |
| 3421 | |
| 3422 | /* |
| 3423 | * We're going to require expansion of the directory - figure |
| 3424 | * out how many blocks we'll need so that a place for the |
| 3425 | * dirent can be found. |
| 3426 | */ |
| 3427 | *blocks_wanted = 1; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3428 | new_rec_len = le16_to_cpu(last_de->rec_len) + free_space; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3429 | if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) |
| 3430 | *blocks_wanted = 2; |
| 3431 | |
| 3432 | ret = -ENOSPC; |
| 3433 | out: |
| 3434 | return ret; |
| 3435 | } |
| 3436 | |
| 3437 | static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, |
| 3438 | int namelen, struct buffer_head **ret_de_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3439 | { |
| 3440 | unsigned long offset; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3441 | struct buffer_head *bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3442 | unsigned short rec_len; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3443 | struct ocfs2_dir_entry *de; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3444 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3445 | int status; |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3446 | int blocksize = dir->i_sb->s_blocksize; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3447 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 3448 | status = ocfs2_read_dir_block(dir, 0, &bh, 0); |
Daeseok Youn | 9b57269 | 2015-02-10 14:09:15 -0800 | [diff] [blame] | 3449 | if (status) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3450 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3451 | |
| 3452 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 3453 | offset = 0; |
| 3454 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 3455 | while (1) { |
| 3456 | if ((char *)de >= sb->s_blocksize + bh->b_data) { |
| 3457 | brelse(bh); |
| 3458 | bh = NULL; |
| 3459 | |
| 3460 | if (i_size_read(dir) <= offset) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3461 | /* |
| 3462 | * Caller will have to expand this |
| 3463 | * directory. |
| 3464 | */ |
| 3465 | status = -ENOSPC; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3466 | goto bail; |
| 3467 | } |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 3468 | status = ocfs2_read_dir_block(dir, |
| 3469 | offset >> sb->s_blocksize_bits, |
| 3470 | &bh, 0); |
Daeseok Youn | 9b57269 | 2015-02-10 14:09:15 -0800 | [diff] [blame] | 3471 | if (status) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3472 | goto bail; |
Daeseok Youn | 9b57269 | 2015-02-10 14:09:15 -0800 | [diff] [blame] | 3473 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3474 | /* move to next block */ |
| 3475 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 3476 | } |
| 3477 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 3478 | status = -ENOENT; |
| 3479 | goto bail; |
| 3480 | } |
| 3481 | if (ocfs2_match(namelen, name, de)) { |
| 3482 | status = -EEXIST; |
| 3483 | goto bail; |
| 3484 | } |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3485 | |
| 3486 | if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize, |
| 3487 | blocksize)) |
| 3488 | goto next; |
| 3489 | |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 3490 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3491 | /* Ok, we found a spot. Return this bh and let |
| 3492 | * the caller actually fill it in. */ |
| 3493 | *ret_de_bh = bh; |
| 3494 | get_bh(*ret_de_bh); |
| 3495 | status = 0; |
| 3496 | goto bail; |
| 3497 | } |
Mark Fasheh | 87d35a7 | 2008-12-10 17:36:25 -0800 | [diff] [blame] | 3498 | next: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3499 | offset += le16_to_cpu(de->rec_len); |
| 3500 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); |
| 3501 | } |
| 3502 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3503 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 3504 | brelse(bh); |
Tao Ma | c1e8d35 | 2011-03-07 16:43:21 +0800 | [diff] [blame] | 3505 | if (status) |
| 3506 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3507 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 3508 | return status; |
| 3509 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 3510 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3511 | static int dx_leaf_sort_cmp(const void *a, const void *b) |
| 3512 | { |
| 3513 | const struct ocfs2_dx_entry *entry1 = a; |
| 3514 | const struct ocfs2_dx_entry *entry2 = b; |
| 3515 | u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash); |
| 3516 | u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash); |
| 3517 | u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash); |
| 3518 | u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash); |
| 3519 | |
| 3520 | if (major_hash1 > major_hash2) |
| 3521 | return 1; |
| 3522 | if (major_hash1 < major_hash2) |
| 3523 | return -1; |
| 3524 | |
| 3525 | /* |
| 3526 | * It is not strictly necessary to sort by minor |
| 3527 | */ |
| 3528 | if (minor_hash1 > minor_hash2) |
| 3529 | return 1; |
| 3530 | if (minor_hash1 < minor_hash2) |
| 3531 | return -1; |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
| 3535 | static void dx_leaf_sort_swap(void *a, void *b, int size) |
| 3536 | { |
| 3537 | struct ocfs2_dx_entry *entry1 = a; |
| 3538 | struct ocfs2_dx_entry *entry2 = b; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3539 | |
| 3540 | BUG_ON(size != sizeof(*entry1)); |
| 3541 | |
Fabian Frederick | 2a28f98 | 2015-06-24 16:55:26 -0700 | [diff] [blame] | 3542 | swap(*entry1, *entry2); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3543 | } |
| 3544 | |
| 3545 | static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf) |
| 3546 | { |
| 3547 | struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; |
| 3548 | int i, num = le16_to_cpu(dl_list->de_num_used); |
| 3549 | |
| 3550 | for (i = 0; i < (num - 1); i++) { |
| 3551 | if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) != |
| 3552 | le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash)) |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | return 1; |
| 3557 | } |
| 3558 | |
| 3559 | /* |
| 3560 | * Find the optimal value to split this leaf on. This expects the leaf |
| 3561 | * entries to be in sorted order. |
| 3562 | * |
| 3563 | * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is |
| 3564 | * the hash we want to insert. |
| 3565 | * |
| 3566 | * This function is only concerned with the major hash - that which |
| 3567 | * determines which cluster an item belongs to. |
| 3568 | */ |
| 3569 | static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf, |
| 3570 | u32 leaf_cpos, u32 insert_hash, |
| 3571 | u32 *split_hash) |
| 3572 | { |
| 3573 | struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list; |
| 3574 | int i, num_used = le16_to_cpu(dl_list->de_num_used); |
| 3575 | int allsame; |
| 3576 | |
| 3577 | /* |
| 3578 | * There's a couple rare, but nasty corner cases we have to |
| 3579 | * check for here. All of them involve a leaf where all value |
| 3580 | * have the same hash, which is what we look for first. |
| 3581 | * |
| 3582 | * Most of the time, all of the above is false, and we simply |
| 3583 | * pick the median value for a split. |
| 3584 | */ |
| 3585 | allsame = ocfs2_dx_leaf_same_major(dx_leaf); |
| 3586 | if (allsame) { |
| 3587 | u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash); |
| 3588 | |
| 3589 | if (val == insert_hash) { |
| 3590 | /* |
| 3591 | * No matter where we would choose to split, |
| 3592 | * the new entry would want to occupy the same |
| 3593 | * block as these. Since there's no space left |
| 3594 | * in their existing block, we know there |
| 3595 | * won't be space after the split. |
| 3596 | */ |
| 3597 | return -ENOSPC; |
| 3598 | } |
| 3599 | |
| 3600 | if (val == leaf_cpos) { |
| 3601 | /* |
| 3602 | * Because val is the same as leaf_cpos (which |
| 3603 | * is the smallest value this leaf can have), |
| 3604 | * yet is not equal to insert_hash, then we |
| 3605 | * know that insert_hash *must* be larger than |
| 3606 | * val (and leaf_cpos). At least cpos+1 in value. |
| 3607 | * |
| 3608 | * We also know then, that there cannot be an |
| 3609 | * adjacent extent (otherwise we'd be looking |
| 3610 | * at it). Choosing this value gives us a |
| 3611 | * chance to get some contiguousness. |
| 3612 | */ |
| 3613 | *split_hash = leaf_cpos + 1; |
| 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | if (val > insert_hash) { |
| 3618 | /* |
| 3619 | * val can not be the same as insert hash, and |
| 3620 | * also must be larger than leaf_cpos. Also, |
| 3621 | * we know that there can't be a leaf between |
| 3622 | * cpos and val, otherwise the entries with |
| 3623 | * hash 'val' would be there. |
| 3624 | */ |
| 3625 | *split_hash = val; |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
| 3629 | *split_hash = insert_hash; |
| 3630 | return 0; |
| 3631 | } |
| 3632 | |
| 3633 | /* |
| 3634 | * Since the records are sorted and the checks above |
| 3635 | * guaranteed that not all records in this block are the same, |
| 3636 | * we simple travel forward, from the median, and pick the 1st |
| 3637 | * record whose value is larger than leaf_cpos. |
| 3638 | */ |
| 3639 | for (i = (num_used / 2); i < num_used; i++) |
| 3640 | if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) > |
| 3641 | leaf_cpos) |
| 3642 | break; |
| 3643 | |
| 3644 | BUG_ON(i == num_used); /* Should be impossible */ |
| 3645 | *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash); |
| 3646 | return 0; |
| 3647 | } |
| 3648 | |
| 3649 | /* |
| 3650 | * Transfer all entries in orig_dx_leaves whose major hash is equal to or |
| 3651 | * larger than split_hash into new_dx_leaves. We use a temporary |
| 3652 | * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks. |
| 3653 | * |
| 3654 | * Since the block offset inside a leaf (cluster) is a constant mask |
| 3655 | * of minor_hash, we can optimize - an item at block offset X within |
| 3656 | * the original cluster, will be at offset X within the new cluster. |
| 3657 | */ |
| 3658 | static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash, |
| 3659 | handle_t *handle, |
| 3660 | struct ocfs2_dx_leaf *tmp_dx_leaf, |
| 3661 | struct buffer_head **orig_dx_leaves, |
| 3662 | struct buffer_head **new_dx_leaves, |
| 3663 | int num_dx_leaves) |
| 3664 | { |
| 3665 | int i, j, num_used; |
| 3666 | u32 major_hash; |
| 3667 | struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf; |
| 3668 | struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list; |
| 3669 | struct ocfs2_dx_entry *dx_entry; |
| 3670 | |
| 3671 | tmp_list = &tmp_dx_leaf->dl_list; |
| 3672 | |
| 3673 | for (i = 0; i < num_dx_leaves; i++) { |
| 3674 | orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data; |
| 3675 | orig_list = &orig_dx_leaf->dl_list; |
| 3676 | new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data; |
| 3677 | new_list = &new_dx_leaf->dl_list; |
| 3678 | |
| 3679 | num_used = le16_to_cpu(orig_list->de_num_used); |
| 3680 | |
| 3681 | memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize); |
| 3682 | tmp_list->de_num_used = cpu_to_le16(0); |
| 3683 | memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used); |
| 3684 | |
| 3685 | for (j = 0; j < num_used; j++) { |
| 3686 | dx_entry = &orig_list->de_entries[j]; |
| 3687 | major_hash = le32_to_cpu(dx_entry->dx_major_hash); |
| 3688 | if (major_hash >= split_hash) |
| 3689 | ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf, |
| 3690 | dx_entry); |
| 3691 | else |
| 3692 | ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf, |
| 3693 | dx_entry); |
| 3694 | } |
| 3695 | memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize); |
| 3696 | |
| 3697 | ocfs2_journal_dirty(handle, orig_dx_leaves[i]); |
| 3698 | ocfs2_journal_dirty(handle, new_dx_leaves[i]); |
| 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb, |
| 3703 | struct ocfs2_dx_root_block *dx_root) |
| 3704 | { |
| 3705 | int credits = ocfs2_clusters_to_blocks(osb->sb, 2); |
| 3706 | |
Goldwyn Rodrigues | 06f9da6 | 2013-11-12 15:06:52 -0800 | [diff] [blame] | 3707 | credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3708 | credits += ocfs2_quota_trans_credits(osb->sb); |
| 3709 | return credits; |
| 3710 | } |
| 3711 | |
| 3712 | /* |
| 3713 | * Find the median value in dx_leaf_bh and allocate a new leaf to move |
| 3714 | * half our entries into. |
| 3715 | */ |
| 3716 | static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir, |
| 3717 | struct buffer_head *dx_root_bh, |
| 3718 | struct buffer_head *dx_leaf_bh, |
| 3719 | struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos, |
| 3720 | u64 leaf_blkno) |
| 3721 | { |
| 3722 | struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; |
| 3723 | int credits, ret, i, num_used, did_quota = 0; |
| 3724 | u32 cpos, split_hash, insert_hash = hinfo->major_hash; |
| 3725 | u64 orig_leaves_start; |
| 3726 | int num_dx_leaves; |
| 3727 | struct buffer_head **orig_dx_leaves = NULL; |
| 3728 | struct buffer_head **new_dx_leaves = NULL; |
| 3729 | struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL; |
| 3730 | struct ocfs2_extent_tree et; |
| 3731 | handle_t *handle = NULL; |
| 3732 | struct ocfs2_dx_root_block *dx_root; |
| 3733 | struct ocfs2_dx_leaf *tmp_dx_leaf = NULL; |
| 3734 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 3735 | trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 3736 | (unsigned long long)leaf_blkno, |
| 3737 | insert_hash); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3738 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 3739 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3740 | |
| 3741 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 3742 | /* |
| 3743 | * XXX: This is a rather large limit. We should use a more |
| 3744 | * realistic value. |
| 3745 | */ |
| 3746 | if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX) |
| 3747 | return -ENOSPC; |
| 3748 | |
| 3749 | num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used); |
| 3750 | if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) { |
| 3751 | mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: " |
| 3752 | "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 3753 | (unsigned long long)leaf_blkno, num_used); |
| 3754 | ret = -EIO; |
| 3755 | goto out; |
| 3756 | } |
| 3757 | |
| 3758 | orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); |
| 3759 | if (!orig_dx_leaves) { |
| 3760 | ret = -ENOMEM; |
| 3761 | mlog_errno(ret); |
| 3762 | goto out; |
| 3763 | } |
| 3764 | |
| 3765 | new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL); |
| 3766 | if (!new_dx_leaves) { |
| 3767 | ret = -ENOMEM; |
| 3768 | mlog_errno(ret); |
| 3769 | goto out; |
| 3770 | } |
| 3771 | |
| 3772 | ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac); |
| 3773 | if (ret) { |
| 3774 | if (ret != -ENOSPC) |
| 3775 | mlog_errno(ret); |
| 3776 | goto out; |
| 3777 | } |
| 3778 | |
| 3779 | credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root); |
| 3780 | handle = ocfs2_start_trans(osb, credits); |
| 3781 | if (IS_ERR(handle)) { |
| 3782 | ret = PTR_ERR(handle); |
| 3783 | handle = NULL; |
| 3784 | mlog_errno(ret); |
| 3785 | goto out; |
| 3786 | } |
| 3787 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 3788 | ret = dquot_alloc_space_nodirty(dir, |
| 3789 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); |
| 3790 | if (ret) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3791 | goto out_commit; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3792 | did_quota = 1; |
| 3793 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3794 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3795 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3796 | if (ret) { |
| 3797 | mlog_errno(ret); |
| 3798 | goto out_commit; |
| 3799 | } |
| 3800 | |
| 3801 | /* |
| 3802 | * This block is changing anyway, so we can sort it in place. |
| 3803 | */ |
| 3804 | sort(dx_leaf->dl_list.de_entries, num_used, |
| 3805 | sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp, |
| 3806 | dx_leaf_sort_swap); |
| 3807 | |
Joel Becker | ec20cec | 2010-03-19 14:13:52 -0700 | [diff] [blame] | 3808 | ocfs2_journal_dirty(handle, dx_leaf_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3809 | |
| 3810 | ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash, |
| 3811 | &split_hash); |
| 3812 | if (ret) { |
| 3813 | mlog_errno(ret); |
| 3814 | goto out_commit; |
| 3815 | } |
| 3816 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 3817 | trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3818 | |
| 3819 | /* |
| 3820 | * We have to carefully order operations here. There are items |
| 3821 | * which want to be in the new cluster before insert, but in |
| 3822 | * order to put those items in the new cluster, we alter the |
| 3823 | * old cluster. A failure to insert gets nasty. |
| 3824 | * |
| 3825 | * So, start by reserving writes to the old |
| 3826 | * cluster. ocfs2_dx_dir_new_cluster will reserve writes on |
| 3827 | * the new cluster for us, before inserting it. The insert |
| 3828 | * won't happen if there's an error before that. Once the |
| 3829 | * insert is done then, we can transfer from one leaf into the |
| 3830 | * other without fear of hitting any error. |
| 3831 | */ |
| 3832 | |
| 3833 | /* |
| 3834 | * The leaf transfer wants some scratch space so that we don't |
| 3835 | * wind up doing a bunch of expensive memmove(). |
| 3836 | */ |
| 3837 | tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS); |
| 3838 | if (!tmp_dx_leaf) { |
| 3839 | ret = -ENOMEM; |
| 3840 | mlog_errno(ret); |
| 3841 | goto out_commit; |
| 3842 | } |
| 3843 | |
Mark Fasheh | 1d46dc0 | 2009-02-19 13:17:05 -0800 | [diff] [blame] | 3844 | orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3845 | ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves, |
| 3846 | orig_dx_leaves); |
| 3847 | if (ret) { |
| 3848 | mlog_errno(ret); |
| 3849 | goto out_commit; |
| 3850 | } |
| 3851 | |
Tristan Ye | 0f4da21 | 2010-09-08 17:12:38 +0800 | [diff] [blame] | 3852 | cpos = split_hash; |
| 3853 | ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle, |
| 3854 | data_ac, meta_ac, new_dx_leaves, |
| 3855 | num_dx_leaves); |
| 3856 | if (ret) { |
| 3857 | mlog_errno(ret); |
| 3858 | goto out_commit; |
| 3859 | } |
| 3860 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3861 | for (i = 0; i < num_dx_leaves; i++) { |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 3862 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
| 3863 | orig_dx_leaves[i], |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3864 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3865 | if (ret) { |
| 3866 | mlog_errno(ret); |
| 3867 | goto out_commit; |
| 3868 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3869 | |
Tristan Ye | 0f4da21 | 2010-09-08 17:12:38 +0800 | [diff] [blame] | 3870 | ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), |
| 3871 | new_dx_leaves[i], |
| 3872 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 3873 | if (ret) { |
| 3874 | mlog_errno(ret); |
| 3875 | goto out_commit; |
| 3876 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3877 | } |
| 3878 | |
| 3879 | ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf, |
| 3880 | orig_dx_leaves, new_dx_leaves, num_dx_leaves); |
| 3881 | |
| 3882 | out_commit: |
| 3883 | if (ret < 0 && did_quota) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 3884 | dquot_free_space_nodirty(dir, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3885 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); |
| 3886 | |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 3887 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 3888 | ocfs2_commit_trans(osb, handle); |
| 3889 | |
| 3890 | out: |
| 3891 | if (orig_dx_leaves || new_dx_leaves) { |
| 3892 | for (i = 0; i < num_dx_leaves; i++) { |
| 3893 | if (orig_dx_leaves) |
| 3894 | brelse(orig_dx_leaves[i]); |
| 3895 | if (new_dx_leaves) |
| 3896 | brelse(new_dx_leaves[i]); |
| 3897 | } |
| 3898 | kfree(orig_dx_leaves); |
| 3899 | kfree(new_dx_leaves); |
| 3900 | } |
| 3901 | |
| 3902 | if (meta_ac) |
| 3903 | ocfs2_free_alloc_context(meta_ac); |
| 3904 | if (data_ac) |
| 3905 | ocfs2_free_alloc_context(data_ac); |
| 3906 | |
| 3907 | kfree(tmp_dx_leaf); |
| 3908 | return ret; |
| 3909 | } |
| 3910 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 3911 | static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir, |
| 3912 | struct buffer_head *di_bh, |
| 3913 | struct buffer_head *dx_root_bh, |
| 3914 | const char *name, int namelen, |
| 3915 | struct ocfs2_dir_lookup_result *lookup) |
| 3916 | { |
| 3917 | int ret, rebalanced = 0; |
| 3918 | struct ocfs2_dx_root_block *dx_root; |
| 3919 | struct buffer_head *dx_leaf_bh = NULL; |
| 3920 | struct ocfs2_dx_leaf *dx_leaf; |
| 3921 | u64 blkno; |
| 3922 | u32 leaf_cpos; |
| 3923 | |
| 3924 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 3925 | |
| 3926 | restart_search: |
| 3927 | ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo, |
| 3928 | &leaf_cpos, &blkno); |
| 3929 | if (ret) { |
| 3930 | mlog_errno(ret); |
| 3931 | goto out; |
| 3932 | } |
| 3933 | |
| 3934 | ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh); |
| 3935 | if (ret) { |
| 3936 | mlog_errno(ret); |
| 3937 | goto out; |
| 3938 | } |
| 3939 | |
| 3940 | dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data; |
| 3941 | |
| 3942 | if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >= |
| 3943 | le16_to_cpu(dx_leaf->dl_list.de_count)) { |
| 3944 | if (rebalanced) { |
| 3945 | /* |
| 3946 | * Rebalancing should have provided us with |
| 3947 | * space in an appropriate leaf. |
| 3948 | * |
| 3949 | * XXX: Is this an abnormal condition then? |
| 3950 | * Should we print a message here? |
| 3951 | */ |
| 3952 | ret = -ENOSPC; |
| 3953 | goto out; |
| 3954 | } |
| 3955 | |
| 3956 | ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh, |
| 3957 | &lookup->dl_hinfo, leaf_cpos, |
| 3958 | blkno); |
| 3959 | if (ret) { |
| 3960 | if (ret != -ENOSPC) |
| 3961 | mlog_errno(ret); |
| 3962 | goto out; |
| 3963 | } |
| 3964 | |
| 3965 | /* |
| 3966 | * Restart the lookup. The rebalance might have |
| 3967 | * changed which block our item fits into. Mark our |
| 3968 | * progress, so we only execute this once. |
| 3969 | */ |
| 3970 | brelse(dx_leaf_bh); |
| 3971 | dx_leaf_bh = NULL; |
| 3972 | rebalanced = 1; |
| 3973 | goto restart_search; |
| 3974 | } |
| 3975 | |
| 3976 | lookup->dl_dx_leaf_bh = dx_leaf_bh; |
| 3977 | dx_leaf_bh = NULL; |
| 3978 | |
| 3979 | out: |
| 3980 | brelse(dx_leaf_bh); |
| 3981 | return ret; |
| 3982 | } |
| 3983 | |
| 3984 | static int ocfs2_search_dx_free_list(struct inode *dir, |
| 3985 | struct buffer_head *dx_root_bh, |
| 3986 | int namelen, |
| 3987 | struct ocfs2_dir_lookup_result *lookup) |
| 3988 | { |
| 3989 | int ret = -ENOSPC; |
| 3990 | struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL; |
| 3991 | struct ocfs2_dir_block_trailer *db; |
| 3992 | u64 next_block; |
| 3993 | int rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 3994 | struct ocfs2_dx_root_block *dx_root; |
| 3995 | |
| 3996 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 3997 | next_block = le64_to_cpu(dx_root->dr_free_blk); |
| 3998 | |
| 3999 | while (next_block) { |
| 4000 | brelse(prev_leaf_bh); |
| 4001 | prev_leaf_bh = leaf_bh; |
| 4002 | leaf_bh = NULL; |
| 4003 | |
| 4004 | ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh); |
| 4005 | if (ret) { |
| 4006 | mlog_errno(ret); |
| 4007 | goto out; |
| 4008 | } |
| 4009 | |
| 4010 | db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb); |
| 4011 | if (rec_len <= le16_to_cpu(db->db_free_rec_len)) { |
| 4012 | lookup->dl_leaf_bh = leaf_bh; |
| 4013 | lookup->dl_prev_leaf_bh = prev_leaf_bh; |
| 4014 | leaf_bh = NULL; |
| 4015 | prev_leaf_bh = NULL; |
| 4016 | break; |
| 4017 | } |
| 4018 | |
| 4019 | next_block = le64_to_cpu(db->db_free_next); |
| 4020 | } |
| 4021 | |
| 4022 | if (!next_block) |
| 4023 | ret = -ENOSPC; |
| 4024 | |
| 4025 | out: |
| 4026 | |
| 4027 | brelse(leaf_bh); |
| 4028 | brelse(prev_leaf_bh); |
| 4029 | return ret; |
| 4030 | } |
| 4031 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4032 | static int ocfs2_expand_inline_dx_root(struct inode *dir, |
| 4033 | struct buffer_head *dx_root_bh) |
| 4034 | { |
| 4035 | int ret, num_dx_leaves, i, j, did_quota = 0; |
| 4036 | struct buffer_head **dx_leaves = NULL; |
| 4037 | struct ocfs2_extent_tree et; |
| 4038 | u64 insert_blkno; |
| 4039 | struct ocfs2_alloc_context *data_ac = NULL; |
| 4040 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 4041 | handle_t *handle = NULL; |
| 4042 | struct ocfs2_dx_root_block *dx_root; |
| 4043 | struct ocfs2_dx_entry_list *entry_list; |
| 4044 | struct ocfs2_dx_entry *dx_entry; |
| 4045 | struct ocfs2_dx_leaf *target_leaf; |
| 4046 | |
| 4047 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); |
| 4048 | if (ret) { |
| 4049 | mlog_errno(ret); |
| 4050 | goto out; |
| 4051 | } |
| 4052 | |
| 4053 | dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves); |
| 4054 | if (!dx_leaves) { |
| 4055 | ret = -ENOMEM; |
| 4056 | mlog_errno(ret); |
| 4057 | goto out; |
| 4058 | } |
| 4059 | |
| 4060 | handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb)); |
| 4061 | if (IS_ERR(handle)) { |
| 4062 | ret = PTR_ERR(handle); |
| 4063 | mlog_errno(ret); |
| 4064 | goto out; |
| 4065 | } |
| 4066 | |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 4067 | ret = dquot_alloc_space_nodirty(dir, |
| 4068 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
| 4069 | if (ret) |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4070 | goto out_commit; |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4071 | did_quota = 1; |
| 4072 | |
| 4073 | /* |
| 4074 | * We do this up front, before the allocation, so that a |
| 4075 | * failure to add the dx_root_bh to the journal won't result |
| 4076 | * us losing clusters. |
| 4077 | */ |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4078 | ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4079 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4080 | if (ret) { |
| 4081 | mlog_errno(ret); |
| 4082 | goto out_commit; |
| 4083 | } |
| 4084 | |
| 4085 | ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves, |
| 4086 | num_dx_leaves, &insert_blkno); |
| 4087 | if (ret) { |
| 4088 | mlog_errno(ret); |
| 4089 | goto out_commit; |
| 4090 | } |
| 4091 | |
| 4092 | /* |
| 4093 | * Transfer the entries from our dx_root into the appropriate |
| 4094 | * block |
| 4095 | */ |
| 4096 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; |
| 4097 | entry_list = &dx_root->dr_entries; |
| 4098 | |
| 4099 | for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) { |
| 4100 | dx_entry = &entry_list->de_entries[i]; |
| 4101 | |
| 4102 | j = __ocfs2_dx_dir_hash_idx(osb, |
| 4103 | le32_to_cpu(dx_entry->dx_minor_hash)); |
| 4104 | target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data; |
| 4105 | |
| 4106 | ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry); |
| 4107 | |
| 4108 | /* Each leaf has been passed to the journal already |
| 4109 | * via __ocfs2_dx_dir_new_cluster() */ |
| 4110 | } |
| 4111 | |
| 4112 | dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE; |
| 4113 | memset(&dx_root->dr_list, 0, osb->sb->s_blocksize - |
| 4114 | offsetof(struct ocfs2_dx_root_block, dr_list)); |
| 4115 | dx_root->dr_list.l_count = |
| 4116 | cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb)); |
| 4117 | |
| 4118 | /* This should never fail considering we start with an empty |
| 4119 | * dx_root. */ |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4120 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
Joel Becker | cc79d8c | 2009-02-13 03:24:43 -0800 | [diff] [blame] | 4121 | ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4122 | if (ret) |
| 4123 | mlog_errno(ret); |
| 4124 | did_quota = 0; |
| 4125 | |
Darrick J. Wong | 2931cdc | 2014-04-03 14:46:48 -0700 | [diff] [blame] | 4126 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4127 | ocfs2_journal_dirty(handle, dx_root_bh); |
| 4128 | |
| 4129 | out_commit: |
| 4130 | if (ret < 0 && did_quota) |
Christoph Hellwig | 5dd4056 | 2010-03-03 09:05:00 -0500 | [diff] [blame] | 4131 | dquot_free_space_nodirty(dir, |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4132 | ocfs2_clusters_to_bytes(dir->i_sb, 1)); |
| 4133 | |
| 4134 | ocfs2_commit_trans(osb, handle); |
| 4135 | |
| 4136 | out: |
| 4137 | if (data_ac) |
| 4138 | ocfs2_free_alloc_context(data_ac); |
| 4139 | |
| 4140 | if (dx_leaves) { |
| 4141 | for (i = 0; i < num_dx_leaves; i++) |
| 4142 | brelse(dx_leaves[i]); |
| 4143 | kfree(dx_leaves); |
| 4144 | } |
| 4145 | return ret; |
| 4146 | } |
| 4147 | |
| 4148 | static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh) |
| 4149 | { |
| 4150 | struct ocfs2_dx_root_block *dx_root; |
| 4151 | struct ocfs2_dx_entry_list *entry_list; |
| 4152 | |
| 4153 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; |
| 4154 | entry_list = &dx_root->dr_entries; |
| 4155 | |
| 4156 | if (le16_to_cpu(entry_list->de_num_used) >= |
| 4157 | le16_to_cpu(entry_list->de_count)) |
| 4158 | return -ENOSPC; |
| 4159 | |
| 4160 | return 0; |
| 4161 | } |
| 4162 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4163 | static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir, |
| 4164 | struct buffer_head *di_bh, |
| 4165 | const char *name, |
| 4166 | int namelen, |
| 4167 | struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4168 | { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4169 | int ret, free_dx_root = 1; |
| 4170 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4171 | struct buffer_head *dx_root_bh = NULL; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4172 | struct buffer_head *leaf_bh = NULL; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4173 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4174 | struct ocfs2_dx_root_block *dx_root; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4175 | |
| 4176 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); |
| 4177 | if (ret) { |
| 4178 | mlog_errno(ret); |
| 4179 | goto out; |
| 4180 | } |
| 4181 | |
| 4182 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
Mark Fasheh | e3a93c2 | 2009-02-17 15:29:35 -0800 | [diff] [blame] | 4183 | if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) { |
| 4184 | ret = -ENOSPC; |
| 4185 | mlog_errno(ret); |
| 4186 | goto out; |
| 4187 | } |
| 4188 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4189 | if (ocfs2_dx_root_inline(dx_root)) { |
| 4190 | ret = ocfs2_inline_dx_has_space(dx_root_bh); |
| 4191 | |
| 4192 | if (ret == 0) |
| 4193 | goto search_el; |
| 4194 | |
| 4195 | /* |
| 4196 | * We ran out of room in the root block. Expand it to |
| 4197 | * an extent, then allow ocfs2_find_dir_space_dx to do |
| 4198 | * the rest. |
| 4199 | */ |
| 4200 | ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh); |
| 4201 | if (ret) { |
| 4202 | mlog_errno(ret); |
| 4203 | goto out; |
| 4204 | } |
| 4205 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4206 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4207 | /* |
| 4208 | * Insert preparation for an indexed directory is split into two |
| 4209 | * steps. The call to find_dir_space_dx reserves room in the index for |
| 4210 | * an additional item. If we run out of space there, it's a real error |
| 4211 | * we can't continue on. |
| 4212 | */ |
| 4213 | ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name, |
| 4214 | namelen, lookup); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4215 | if (ret) { |
| 4216 | mlog_errno(ret); |
| 4217 | goto out; |
| 4218 | } |
| 4219 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4220 | search_el: |
| 4221 | /* |
| 4222 | * Next, we need to find space in the unindexed tree. This call |
| 4223 | * searches using the free space linked list. If the unindexed tree |
| 4224 | * lacks sufficient space, we'll expand it below. The expansion code |
| 4225 | * is smart enough to add any new blocks to the free space list. |
| 4226 | */ |
| 4227 | ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup); |
| 4228 | if (ret && ret != -ENOSPC) { |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4229 | mlog_errno(ret); |
| 4230 | goto out; |
| 4231 | } |
| 4232 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4233 | /* Do this up here - ocfs2_extend_dir might need the dx_root */ |
| 4234 | lookup->dl_dx_root_bh = dx_root_bh; |
| 4235 | free_dx_root = 0; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4236 | |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4237 | if (ret == -ENOSPC) { |
| 4238 | ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4239 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4240 | if (ret) { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4241 | mlog_errno(ret); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4242 | goto out; |
| 4243 | } |
| 4244 | |
| 4245 | /* |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4246 | * We make the assumption here that new leaf blocks are added |
| 4247 | * to the front of our free list. |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4248 | */ |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4249 | lookup->dl_prev_leaf_bh = NULL; |
| 4250 | lookup->dl_leaf_bh = leaf_bh; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4251 | } |
| 4252 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4253 | out: |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4254 | if (free_dx_root) |
| 4255 | brelse(dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4256 | return ret; |
| 4257 | } |
| 4258 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 4259 | /* |
| 4260 | * Get a directory ready for insert. Any directory allocation required |
| 4261 | * happens here. Success returns zero, and enough context in the dir |
| 4262 | * lookup result that ocfs2_add_entry() will be able complete the task |
| 4263 | * with minimal performance impact. |
| 4264 | */ |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4265 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, |
| 4266 | struct inode *dir, |
| 4267 | struct buffer_head *parent_fe_bh, |
| 4268 | const char *name, |
| 4269 | int namelen, |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 4270 | struct ocfs2_dir_lookup_result *lookup) |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4271 | { |
| 4272 | int ret; |
| 4273 | unsigned int blocks_wanted = 1; |
| 4274 | struct buffer_head *bh = NULL; |
| 4275 | |
Tao Ma | f1088d47 | 2011-02-23 22:30:23 +0800 | [diff] [blame] | 4276 | trace_ocfs2_prepare_dir_for_insert( |
| 4277 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4278 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4279 | if (!namelen) { |
| 4280 | ret = -EINVAL; |
| 4281 | mlog_errno(ret); |
| 4282 | goto out; |
| 4283 | } |
| 4284 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4285 | /* |
| 4286 | * Do this up front to reduce confusion. |
| 4287 | * |
| 4288 | * The directory might start inline, then be turned into an |
| 4289 | * indexed one, in which case we'd need to hash deep inside |
| 4290 | * ocfs2_find_dir_space_id(). Since |
| 4291 | * ocfs2_prepare_dx_dir_for_insert() also needs this hash |
| 4292 | * done, there seems no point in spreading out the calls. We |
| 4293 | * can optimize away the case where the file system doesn't |
| 4294 | * support indexing. |
| 4295 | */ |
| 4296 | if (ocfs2_supports_indexed_dirs(osb)) |
| 4297 | ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo); |
| 4298 | |
| 4299 | if (ocfs2_dir_indexed(dir)) { |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4300 | ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh, |
| 4301 | name, namelen, lookup); |
| 4302 | if (ret) |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4303 | mlog_errno(ret); |
Mark Fasheh | e7c17e4 | 2009-01-29 18:17:46 -0800 | [diff] [blame] | 4304 | goto out; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4305 | } |
| 4306 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4307 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 4308 | ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, |
| 4309 | namelen, &bh, &blocks_wanted); |
| 4310 | } else |
| 4311 | ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); |
| 4312 | |
| 4313 | if (ret && ret != -ENOSPC) { |
| 4314 | mlog_errno(ret); |
| 4315 | goto out; |
| 4316 | } |
| 4317 | |
| 4318 | if (ret == -ENOSPC) { |
| 4319 | /* |
| 4320 | * We have to expand the directory to add this name. |
| 4321 | */ |
| 4322 | BUG_ON(bh); |
| 4323 | |
| 4324 | ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4325 | lookup, &bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4326 | if (ret) { |
| 4327 | if (ret != -ENOSPC) |
| 4328 | mlog_errno(ret); |
| 4329 | goto out; |
| 4330 | } |
| 4331 | |
| 4332 | BUG_ON(!bh); |
| 4333 | } |
| 4334 | |
Mark Fasheh | 4a12ca3 | 2008-11-12 15:43:34 -0800 | [diff] [blame] | 4335 | lookup->dl_leaf_bh = bh; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4336 | bh = NULL; |
| 4337 | out: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 4338 | brelse(bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 4339 | return ret; |
| 4340 | } |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4341 | |
| 4342 | static int ocfs2_dx_dir_remove_index(struct inode *dir, |
| 4343 | struct buffer_head *di_bh, |
| 4344 | struct buffer_head *dx_root_bh) |
| 4345 | { |
| 4346 | int ret; |
| 4347 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 4348 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 4349 | struct ocfs2_dx_root_block *dx_root; |
| 4350 | struct inode *dx_alloc_inode = NULL; |
| 4351 | struct buffer_head *dx_alloc_bh = NULL; |
| 4352 | handle_t *handle; |
| 4353 | u64 blk; |
| 4354 | u16 bit; |
| 4355 | u64 bg_blkno; |
| 4356 | |
| 4357 | dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data; |
| 4358 | |
| 4359 | dx_alloc_inode = ocfs2_get_system_file_inode(osb, |
| 4360 | EXTENT_ALLOC_SYSTEM_INODE, |
| 4361 | le16_to_cpu(dx_root->dr_suballoc_slot)); |
| 4362 | if (!dx_alloc_inode) { |
| 4363 | ret = -ENOMEM; |
| 4364 | mlog_errno(ret); |
| 4365 | goto out; |
| 4366 | } |
| 4367 | mutex_lock(&dx_alloc_inode->i_mutex); |
| 4368 | |
| 4369 | ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1); |
| 4370 | if (ret) { |
| 4371 | mlog_errno(ret); |
| 4372 | goto out_mutex; |
| 4373 | } |
| 4374 | |
| 4375 | handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS); |
| 4376 | if (IS_ERR(handle)) { |
| 4377 | ret = PTR_ERR(handle); |
| 4378 | mlog_errno(ret); |
| 4379 | goto out_unlock; |
| 4380 | } |
| 4381 | |
Joel Becker | 0cf2f76 | 2009-02-12 16:41:25 -0800 | [diff] [blame] | 4382 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh, |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4383 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 4384 | if (ret) { |
| 4385 | mlog_errno(ret); |
| 4386 | goto out_commit; |
| 4387 | } |
| 4388 | |
Tao Ma | 8ac33dc | 2010-12-15 16:30:00 +0800 | [diff] [blame] | 4389 | spin_lock(&OCFS2_I(dir)->ip_lock); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4390 | OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL; |
| 4391 | di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features); |
Tao Ma | 8ac33dc | 2010-12-15 16:30:00 +0800 | [diff] [blame] | 4392 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4393 | di->i_dx_root = cpu_to_le64(0ULL); |
Darrick J. Wong | 6fdb702 | 2014-04-03 14:47:08 -0700 | [diff] [blame] | 4394 | ocfs2_update_inode_fsync_trans(handle, dir, 1); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4395 | |
| 4396 | ocfs2_journal_dirty(handle, di_bh); |
| 4397 | |
| 4398 | blk = le64_to_cpu(dx_root->dr_blkno); |
| 4399 | bit = le16_to_cpu(dx_root->dr_suballoc_bit); |
Tao Ma | 74380c4 | 2010-03-22 14:20:18 +0800 | [diff] [blame] | 4400 | if (dx_root->dr_suballoc_loc) |
| 4401 | bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc); |
| 4402 | else |
| 4403 | bg_blkno = ocfs2_which_suballoc_group(blk, bit); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4404 | ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh, |
| 4405 | bit, bg_blkno, 1); |
| 4406 | if (ret) |
| 4407 | mlog_errno(ret); |
| 4408 | |
| 4409 | out_commit: |
| 4410 | ocfs2_commit_trans(osb, handle); |
| 4411 | |
| 4412 | out_unlock: |
| 4413 | ocfs2_inode_unlock(dx_alloc_inode, 1); |
| 4414 | |
| 4415 | out_mutex: |
| 4416 | mutex_unlock(&dx_alloc_inode->i_mutex); |
| 4417 | brelse(dx_alloc_bh); |
| 4418 | out: |
| 4419 | iput(dx_alloc_inode); |
| 4420 | return ret; |
| 4421 | } |
| 4422 | |
| 4423 | int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh) |
| 4424 | { |
| 4425 | int ret; |
| 4426 | unsigned int uninitialized_var(clen); |
| 4427 | u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos); |
| 4428 | u64 uninitialized_var(blkno); |
| 4429 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 4430 | struct buffer_head *dx_root_bh = NULL; |
| 4431 | struct ocfs2_dx_root_block *dx_root; |
| 4432 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 4433 | struct ocfs2_cached_dealloc_ctxt dealloc; |
| 4434 | struct ocfs2_extent_tree et; |
| 4435 | |
| 4436 | ocfs2_init_dealloc_ctxt(&dealloc); |
| 4437 | |
| 4438 | if (!ocfs2_dir_indexed(dir)) |
| 4439 | return 0; |
| 4440 | |
| 4441 | ret = ocfs2_read_dx_root(dir, di, &dx_root_bh); |
| 4442 | if (ret) { |
| 4443 | mlog_errno(ret); |
| 4444 | goto out; |
| 4445 | } |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4446 | dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data; |
| 4447 | |
| 4448 | if (ocfs2_dx_root_inline(dx_root)) |
| 4449 | goto remove_index; |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4450 | |
Joel Becker | 5e404e9 | 2009-02-13 03:54:22 -0800 | [diff] [blame] | 4451 | ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4452 | |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4453 | /* XXX: What if dr_clusters is too large? */ |
| 4454 | while (le32_to_cpu(dx_root->dr_clusters)) { |
| 4455 | ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list, |
| 4456 | major_hash, &cpos, &blkno, &clen); |
| 4457 | if (ret) { |
| 4458 | mlog_errno(ret); |
| 4459 | goto out; |
| 4460 | } |
| 4461 | |
| 4462 | p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno); |
| 4463 | |
Tristan Ye | 78f9467 | 2010-05-11 17:54:42 +0800 | [diff] [blame] | 4464 | ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0, |
Junxiao Bi | f62f12b | 2014-12-18 16:17:32 -0800 | [diff] [blame] | 4465 | &dealloc, 0, false); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4466 | if (ret) { |
| 4467 | mlog_errno(ret); |
| 4468 | goto out; |
| 4469 | } |
| 4470 | |
| 4471 | if (cpos == 0) |
| 4472 | break; |
| 4473 | |
| 4474 | major_hash = cpos - 1; |
| 4475 | } |
| 4476 | |
Mark Fasheh | 4ed8a6b | 2008-11-24 17:02:08 -0800 | [diff] [blame] | 4477 | remove_index: |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4478 | ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh); |
| 4479 | if (ret) { |
| 4480 | mlog_errno(ret); |
| 4481 | goto out; |
| 4482 | } |
| 4483 | |
Joel Becker | 8cb471e | 2009-02-10 20:00:41 -0800 | [diff] [blame] | 4484 | ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh); |
Mark Fasheh | 9b7895e | 2008-11-12 16:27:44 -0800 | [diff] [blame] | 4485 | out: |
| 4486 | ocfs2_schedule_truncate_log_flush(osb, 1); |
| 4487 | ocfs2_run_deallocs(osb, &dealloc); |
| 4488 | |
| 4489 | brelse(dx_root_bh); |
| 4490 | return ret; |
| 4491 | } |