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 | * |
| 21 | * Copyright (C) 1991, 1992 Linux Torvalds |
| 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 | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 44 | |
| 45 | #define MLOG_MASK_PREFIX ML_NAMEI |
| 46 | #include <cluster/masklog.h> |
| 47 | |
| 48 | #include "ocfs2.h" |
| 49 | |
| 50 | #include "alloc.h" |
| 51 | #include "dir.h" |
| 52 | #include "dlmglue.h" |
| 53 | #include "extent_map.h" |
| 54 | #include "file.h" |
| 55 | #include "inode.h" |
| 56 | #include "journal.h" |
| 57 | #include "namei.h" |
| 58 | #include "suballoc.h" |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 59 | #include "super.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 60 | #include "uptodate.h" |
| 61 | |
| 62 | #include "buffer_head_io.h" |
| 63 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 64 | #define NAMEI_RA_CHUNKS 2 |
| 65 | #define NAMEI_RA_BLOCKS 4 |
| 66 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
| 67 | #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) |
| 68 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 69 | static unsigned char ocfs2_filetype_table[] = { |
| 70 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 71 | }; |
| 72 | |
| 73 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 74 | struct inode *dir, |
| 75 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 76 | unsigned int blocks_wanted, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 77 | struct buffer_head **new_de_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 78 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 79 | handle_t *handle, |
| 80 | struct inode *dir, |
| 81 | struct buffer_head *parent_fe_bh, |
| 82 | struct ocfs2_alloc_context *data_ac, |
| 83 | struct ocfs2_alloc_context *meta_ac, |
| 84 | struct buffer_head **new_bh); |
| 85 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 86 | /* |
| 87 | * bh passed here can be an inode block or a dir data block, depending |
| 88 | * on the inode inline data flag. |
| 89 | */ |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 90 | static int ocfs2_check_dir_entry(struct inode * dir, |
| 91 | struct ocfs2_dir_entry * de, |
| 92 | struct buffer_head * bh, |
| 93 | unsigned long offset) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 94 | { |
| 95 | const char *error_msg = NULL; |
| 96 | const int rlen = le16_to_cpu(de->rec_len); |
| 97 | |
| 98 | if (rlen < OCFS2_DIR_REC_LEN(1)) |
| 99 | error_msg = "rec_len is smaller than minimal"; |
| 100 | else if (rlen % 4 != 0) |
| 101 | error_msg = "rec_len % 4 != 0"; |
| 102 | else if (rlen < OCFS2_DIR_REC_LEN(de->name_len)) |
| 103 | error_msg = "rec_len is too small for name_len"; |
| 104 | else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize) |
| 105 | error_msg = "directory entry across blocks"; |
| 106 | |
| 107 | if (error_msg != NULL) |
| 108 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " |
| 109 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", |
| 110 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, |
| 111 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, |
| 112 | de->name_len); |
| 113 | return error_msg == NULL ? 1 : 0; |
| 114 | } |
| 115 | |
| 116 | static inline int ocfs2_match(int len, |
| 117 | const char * const name, |
| 118 | struct ocfs2_dir_entry *de) |
| 119 | { |
| 120 | if (len != de->name_len) |
| 121 | return 0; |
| 122 | if (!de->inode) |
| 123 | return 0; |
| 124 | return !memcmp(name, de->name, len); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 129 | */ |
| 130 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, |
| 131 | struct inode *dir, |
| 132 | const char *name, int namelen, |
| 133 | unsigned long offset, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 134 | char *first_de, |
| 135 | unsigned int bytes, |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 136 | struct ocfs2_dir_entry **res_dir) |
| 137 | { |
| 138 | struct ocfs2_dir_entry *de; |
| 139 | char *dlimit, *de_buf; |
| 140 | int de_len; |
| 141 | int ret = 0; |
| 142 | |
| 143 | mlog_entry_void(); |
| 144 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 145 | de_buf = first_de; |
| 146 | dlimit = de_buf + bytes; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 147 | |
| 148 | while (de_buf < dlimit) { |
| 149 | /* this code is executed quadratically often */ |
| 150 | /* do minimal checking `by hand' */ |
| 151 | |
| 152 | de = (struct ocfs2_dir_entry *) de_buf; |
| 153 | |
| 154 | if (de_buf + namelen <= dlimit && |
| 155 | ocfs2_match(namelen, name, de)) { |
| 156 | /* found a match - just to be sure, do a full check */ |
| 157 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 158 | ret = -1; |
| 159 | goto bail; |
| 160 | } |
| 161 | *res_dir = de; |
| 162 | ret = 1; |
| 163 | goto bail; |
| 164 | } |
| 165 | |
| 166 | /* prevent looping on a bad block */ |
| 167 | de_len = le16_to_cpu(de->rec_len); |
| 168 | if (de_len <= 0) { |
| 169 | ret = -1; |
| 170 | goto bail; |
| 171 | } |
| 172 | |
| 173 | de_buf += de_len; |
| 174 | offset += de_len; |
| 175 | } |
| 176 | |
| 177 | bail: |
| 178 | mlog_exit(ret); |
| 179 | return ret; |
| 180 | } |
| 181 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 182 | static struct buffer_head *ocfs2_find_entry_id(const char *name, |
| 183 | int namelen, |
| 184 | struct inode *dir, |
| 185 | struct ocfs2_dir_entry **res_dir) |
| 186 | { |
| 187 | int ret, found; |
| 188 | struct buffer_head *di_bh = NULL; |
| 189 | struct ocfs2_dinode *di; |
| 190 | struct ocfs2_inline_data *data; |
| 191 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 192 | ret = ocfs2_read_inode_block(dir, &di_bh); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 193 | if (ret) { |
| 194 | mlog_errno(ret); |
| 195 | goto out; |
| 196 | } |
| 197 | |
| 198 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 199 | data = &di->id2.i_data; |
| 200 | |
| 201 | found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0, |
| 202 | data->id_data, i_size_read(dir), res_dir); |
| 203 | if (found == 1) |
| 204 | return di_bh; |
| 205 | |
| 206 | brelse(di_bh); |
| 207 | out: |
| 208 | return NULL; |
| 209 | } |
| 210 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 211 | static int ocfs2_validate_dir_block(struct super_block *sb, |
| 212 | struct buffer_head *bh) |
| 213 | { |
| 214 | /* |
| 215 | * Nothing yet. We don't validate dirents here, that's handled |
| 216 | * in-place when the code walks them. |
| 217 | */ |
Joel Becker | 970e493 | 2008-11-13 14:49:19 -0800 | [diff] [blame] | 218 | mlog(0, "Validating dirblock %llu\n", |
| 219 | (unsigned long long)bh->b_blocknr); |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * This function forces all errors to -EIO for consistency with its |
| 226 | * predecessor, ocfs2_bread(). We haven't audited what returning the |
| 227 | * real error codes would do to callers. We log the real codes with |
| 228 | * mlog_errno() before we squash them. |
| 229 | */ |
| 230 | static int ocfs2_read_dir_block(struct inode *inode, u64 v_block, |
| 231 | struct buffer_head **bh, int flags) |
| 232 | { |
| 233 | int rc = 0; |
| 234 | struct buffer_head *tmp = *bh; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 235 | |
Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 236 | rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags, |
| 237 | ocfs2_validate_dir_block); |
| 238 | if (rc) |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 239 | mlog_errno(rc); |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 240 | |
Joel Becker | 511308d | 2008-11-13 14:49:21 -0800 | [diff] [blame] | 241 | /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */ |
| 242 | if (!rc && !*bh) |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 243 | *bh = tmp; |
| 244 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 245 | return rc ? -EIO : 0; |
| 246 | } |
| 247 | |
Adrian Bunk | 0af4bd3 | 2007-10-24 18:23:27 +0200 | [diff] [blame] | 248 | static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen, |
| 249 | struct inode *dir, |
| 250 | struct ocfs2_dir_entry **res_dir) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 251 | { |
| 252 | struct super_block *sb; |
| 253 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 254 | struct buffer_head *bh, *ret = NULL; |
| 255 | unsigned long start, block, b; |
| 256 | int ra_max = 0; /* Number of bh's in the readahead |
| 257 | buffer, bh_use[] */ |
| 258 | int ra_ptr = 0; /* Current index into readahead |
| 259 | buffer */ |
| 260 | int num = 0; |
| 261 | int nblocks, i, err; |
| 262 | |
| 263 | mlog_entry_void(); |
| 264 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 265 | sb = dir->i_sb; |
| 266 | |
| 267 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 268 | start = OCFS2_I(dir)->ip_dir_start_lookup; |
| 269 | if (start >= nblocks) |
| 270 | start = 0; |
| 271 | block = start; |
| 272 | |
| 273 | restart: |
| 274 | do { |
| 275 | /* |
| 276 | * We deal with the read-ahead logic here. |
| 277 | */ |
| 278 | if (ra_ptr >= ra_max) { |
| 279 | /* Refill the readahead buffer */ |
| 280 | ra_ptr = 0; |
| 281 | b = block; |
| 282 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 283 | /* |
| 284 | * Terminate if we reach the end of the |
| 285 | * directory and must wrap, or if our |
| 286 | * search has finished at this block. |
| 287 | */ |
| 288 | if (b >= nblocks || (num && block == start)) { |
| 289 | bh_use[ra_max] = NULL; |
| 290 | break; |
| 291 | } |
| 292 | num++; |
| 293 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 294 | bh = NULL; |
| 295 | err = ocfs2_read_dir_block(dir, b++, &bh, |
| 296 | OCFS2_BH_READAHEAD); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 297 | bh_use[ra_max] = bh; |
| 298 | } |
| 299 | } |
| 300 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 301 | goto next; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 302 | if (ocfs2_read_dir_block(dir, block, &bh, 0)) { |
Joel Becker | 5e0b3de | 2008-10-09 17:20:33 -0700 | [diff] [blame] | 303 | /* read error, skip block & hope for the best. |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 304 | * ocfs2_read_dir_block() has released the bh. */ |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 305 | ocfs2_error(dir->i_sb, "reading directory %llu, " |
| 306 | "offset %lu\n", |
| 307 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 308 | block); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 309 | goto next; |
| 310 | } |
| 311 | i = ocfs2_search_dirblock(bh, dir, name, namelen, |
| 312 | block << sb->s_blocksize_bits, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 313 | bh->b_data, sb->s_blocksize, |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 314 | res_dir); |
| 315 | if (i == 1) { |
| 316 | OCFS2_I(dir)->ip_dir_start_lookup = block; |
| 317 | ret = bh; |
| 318 | goto cleanup_and_exit; |
| 319 | } else { |
| 320 | brelse(bh); |
| 321 | if (i < 0) |
| 322 | goto cleanup_and_exit; |
| 323 | } |
| 324 | next: |
| 325 | if (++block >= nblocks) |
| 326 | block = 0; |
| 327 | } while (block != start); |
| 328 | |
| 329 | /* |
| 330 | * If the directory has grown while we were searching, then |
| 331 | * search the last part of the directory before giving up. |
| 332 | */ |
| 333 | block = nblocks; |
| 334 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 335 | if (block < nblocks) { |
| 336 | start = 0; |
| 337 | goto restart; |
| 338 | } |
| 339 | |
| 340 | cleanup_and_exit: |
| 341 | /* Clean up the read-ahead blocks */ |
| 342 | for (; ra_ptr < ra_max; ra_ptr++) |
| 343 | brelse(bh_use[ra_ptr]); |
| 344 | |
| 345 | mlog_exit_ptr(ret); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 349 | /* |
| 350 | * Try to find an entry of the provided name within 'dir'. |
| 351 | * |
| 352 | * If nothing was found, NULL is returned. Otherwise, a buffer_head |
| 353 | * and pointer to the dir entry are passed back. |
| 354 | * |
| 355 | * Caller can NOT assume anything about the contents of the |
| 356 | * buffer_head - it is passed back only so that it can be passed into |
| 357 | * any one of the manipulation functions (add entry, delete entry, |
| 358 | * etc). As an example, bh in the extent directory case is a data |
| 359 | * block, in the inline-data case it actually points to an inode. |
| 360 | */ |
| 361 | struct buffer_head *ocfs2_find_entry(const char *name, int namelen, |
| 362 | struct inode *dir, |
| 363 | struct ocfs2_dir_entry **res_dir) |
| 364 | { |
| 365 | *res_dir = NULL; |
| 366 | |
| 367 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 368 | return ocfs2_find_entry_id(name, namelen, dir, res_dir); |
| 369 | |
| 370 | return ocfs2_find_entry_el(name, namelen, dir, res_dir); |
| 371 | } |
| 372 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 373 | /* |
| 374 | * Update inode number and type of a previously found directory entry. |
| 375 | */ |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 376 | int ocfs2_update_entry(struct inode *dir, handle_t *handle, |
| 377 | struct buffer_head *de_bh, struct ocfs2_dir_entry *de, |
| 378 | struct inode *new_entry_inode) |
| 379 | { |
| 380 | int ret; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 381 | ocfs2_journal_access_func access = ocfs2_journal_access_db; |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 382 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 383 | /* |
| 384 | * The same code works fine for both inline-data and extent |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 385 | * based directories, so no need to split this up. The only |
| 386 | * difference is the journal_access function. |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 387 | */ |
| 388 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 389 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 390 | access = ocfs2_journal_access_di; |
| 391 | |
| 392 | ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 38760e2 | 2007-09-11 17:21:56 -0700 | [diff] [blame] | 393 | if (ret) { |
| 394 | mlog_errno(ret); |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno); |
| 399 | ocfs2_set_de_type(de, new_entry_inode->i_mode); |
| 400 | |
| 401 | ocfs2_journal_dirty(handle, de_bh); |
| 402 | |
| 403 | out: |
| 404 | return ret; |
| 405 | } |
| 406 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 407 | static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir, |
| 408 | struct ocfs2_dir_entry *de_del, |
| 409 | struct buffer_head *bh, char *first_de, |
| 410 | unsigned int bytes) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 411 | { |
| 412 | struct ocfs2_dir_entry *de, *pde; |
| 413 | int i, status = -ENOENT; |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 414 | ocfs2_journal_access_func access = ocfs2_journal_access_db; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 415 | |
| 416 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh); |
| 417 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 418 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 419 | access = ocfs2_journal_access_di; |
| 420 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 421 | i = 0; |
| 422 | pde = NULL; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 423 | de = (struct ocfs2_dir_entry *) first_de; |
| 424 | while (i < bytes) { |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 425 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { |
| 426 | status = -EIO; |
| 427 | mlog_errno(status); |
| 428 | goto bail; |
| 429 | } |
| 430 | if (de == de_del) { |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 431 | status = access(handle, dir, bh, |
| 432 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 433 | if (status < 0) { |
| 434 | status = -EIO; |
| 435 | mlog_errno(status); |
| 436 | goto bail; |
| 437 | } |
| 438 | if (pde) |
Marcin Slusarz | 0dd3256 | 2008-02-13 00:06:18 +0100 | [diff] [blame] | 439 | le16_add_cpu(&pde->rec_len, |
| 440 | le16_to_cpu(de->rec_len)); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 441 | else |
| 442 | de->inode = 0; |
| 443 | dir->i_version++; |
| 444 | status = ocfs2_journal_dirty(handle, bh); |
| 445 | goto bail; |
| 446 | } |
| 447 | i += le16_to_cpu(de->rec_len); |
| 448 | pde = de; |
| 449 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); |
| 450 | } |
| 451 | bail: |
| 452 | mlog_exit(status); |
| 453 | return status; |
| 454 | } |
| 455 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 456 | static inline int ocfs2_delete_entry_id(handle_t *handle, |
| 457 | struct inode *dir, |
| 458 | struct ocfs2_dir_entry *de_del, |
| 459 | struct buffer_head *bh) |
| 460 | { |
| 461 | int ret; |
| 462 | struct buffer_head *di_bh = NULL; |
| 463 | struct ocfs2_dinode *di; |
| 464 | struct ocfs2_inline_data *data; |
| 465 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 466 | ret = ocfs2_read_inode_block(dir, &di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 467 | if (ret) { |
| 468 | mlog_errno(ret); |
| 469 | goto out; |
| 470 | } |
| 471 | |
| 472 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 473 | data = &di->id2.i_data; |
| 474 | |
| 475 | ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data, |
| 476 | i_size_read(dir)); |
| 477 | |
| 478 | brelse(di_bh); |
| 479 | out: |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static inline int ocfs2_delete_entry_el(handle_t *handle, |
| 484 | struct inode *dir, |
| 485 | struct ocfs2_dir_entry *de_del, |
| 486 | struct buffer_head *bh) |
| 487 | { |
| 488 | return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data, |
| 489 | bh->b_size); |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * ocfs2_delete_entry deletes a directory entry by merging it with the |
| 494 | * previous entry |
| 495 | */ |
| 496 | int ocfs2_delete_entry(handle_t *handle, |
| 497 | struct inode *dir, |
| 498 | struct ocfs2_dir_entry *de_del, |
| 499 | struct buffer_head *bh) |
| 500 | { |
| 501 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 502 | return ocfs2_delete_entry_id(handle, dir, de_del, bh); |
| 503 | |
| 504 | return ocfs2_delete_entry_el(handle, dir, de_del, bh); |
| 505 | } |
| 506 | |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 507 | /* |
| 508 | * Check whether 'de' has enough room to hold an entry of |
| 509 | * 'new_rec_len' bytes. |
| 510 | */ |
| 511 | static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de, |
| 512 | unsigned int new_rec_len) |
| 513 | { |
| 514 | unsigned int de_really_used; |
| 515 | |
| 516 | /* Check whether this is an empty record with enough space */ |
| 517 | if (le64_to_cpu(de->inode) == 0 && |
| 518 | le16_to_cpu(de->rec_len) >= new_rec_len) |
| 519 | return 1; |
| 520 | |
| 521 | /* |
| 522 | * Record might have free space at the end which we can |
| 523 | * use. |
| 524 | */ |
| 525 | de_really_used = OCFS2_DIR_REC_LEN(de->name_len); |
| 526 | if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len)) |
| 527 | return 1; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 532 | /* we don't always have a dentry for what we want to add, so people |
| 533 | * like orphan dir can call this instead. |
| 534 | * |
| 535 | * If you pass me insert_bh, I'll skip the search of the other dir |
| 536 | * blocks and put the record in there. |
| 537 | */ |
| 538 | int __ocfs2_add_entry(handle_t *handle, |
| 539 | struct inode *dir, |
| 540 | const char *name, int namelen, |
| 541 | struct inode *inode, u64 blkno, |
| 542 | struct buffer_head *parent_fe_bh, |
| 543 | struct buffer_head *insert_bh) |
| 544 | { |
| 545 | unsigned long offset; |
| 546 | unsigned short rec_len; |
| 547 | struct ocfs2_dir_entry *de, *de1; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 548 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data; |
| 549 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 550 | int retval, status; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 551 | unsigned int size = sb->s_blocksize; |
| 552 | char *data_start = insert_bh->b_data; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 553 | |
| 554 | mlog_entry_void(); |
| 555 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 556 | if (!namelen) |
| 557 | return -EINVAL; |
| 558 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 559 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 560 | data_start = di->id2.i_data.id_data; |
| 561 | size = i_size_read(dir); |
| 562 | |
| 563 | BUG_ON(insert_bh != parent_fe_bh); |
| 564 | } |
| 565 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 566 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 567 | offset = 0; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 568 | de = (struct ocfs2_dir_entry *) data_start; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 569 | while (1) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 570 | BUG_ON((char *)de >= (size + data_start)); |
| 571 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 572 | /* These checks should've already been passed by the |
| 573 | * prepare function, but I guess we can leave them |
| 574 | * here anyway. */ |
| 575 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { |
| 576 | retval = -ENOENT; |
| 577 | goto bail; |
| 578 | } |
| 579 | if (ocfs2_match(namelen, name, de)) { |
| 580 | retval = -EEXIST; |
| 581 | goto bail; |
| 582 | } |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 583 | |
| 584 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 585 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 586 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 587 | if (retval < 0) { |
| 588 | mlog_errno(retval); |
| 589 | goto bail; |
| 590 | } |
| 591 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 592 | if (insert_bh == parent_fe_bh) |
| 593 | status = ocfs2_journal_access_di(handle, dir, |
| 594 | insert_bh, |
| 595 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 596 | else |
| 597 | status = ocfs2_journal_access_db(handle, dir, |
| 598 | insert_bh, |
| 599 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 600 | /* By now the buffer is marked for journaling */ |
| 601 | offset += le16_to_cpu(de->rec_len); |
| 602 | if (le64_to_cpu(de->inode)) { |
| 603 | de1 = (struct ocfs2_dir_entry *)((char *) de + |
| 604 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 605 | de1->rec_len = |
| 606 | cpu_to_le16(le16_to_cpu(de->rec_len) - |
| 607 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 608 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 609 | de = de1; |
| 610 | } |
| 611 | de->file_type = OCFS2_FT_UNKNOWN; |
| 612 | if (blkno) { |
| 613 | de->inode = cpu_to_le64(blkno); |
| 614 | ocfs2_set_de_type(de, inode->i_mode); |
| 615 | } else |
| 616 | de->inode = 0; |
| 617 | de->name_len = namelen; |
| 618 | memcpy(de->name, name, namelen); |
| 619 | |
| 620 | dir->i_version++; |
| 621 | status = ocfs2_journal_dirty(handle, insert_bh); |
| 622 | retval = 0; |
| 623 | goto bail; |
| 624 | } |
| 625 | offset += le16_to_cpu(de->rec_len); |
| 626 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); |
| 627 | } |
| 628 | |
| 629 | /* when you think about it, the assert above should prevent us |
| 630 | * from ever getting here. */ |
| 631 | retval = -ENOSPC; |
| 632 | bail: |
| 633 | |
| 634 | mlog_exit(retval); |
| 635 | return retval; |
| 636 | } |
| 637 | |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 638 | static int ocfs2_dir_foreach_blk_id(struct inode *inode, |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 639 | u64 *f_version, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 640 | loff_t *f_pos, void *priv, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 641 | filldir_t filldir, int *filldir_err) |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 642 | { |
| 643 | int ret, i, filldir_ret; |
| 644 | unsigned long offset = *f_pos; |
| 645 | struct buffer_head *di_bh = NULL; |
| 646 | struct ocfs2_dinode *di; |
| 647 | struct ocfs2_inline_data *data; |
| 648 | struct ocfs2_dir_entry *de; |
| 649 | |
Joel Becker | b657c95 | 2008-11-13 14:49:11 -0800 | [diff] [blame] | 650 | ret = ocfs2_read_inode_block(inode, &di_bh); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 651 | if (ret) { |
| 652 | mlog(ML_ERROR, "Unable to read inode block for dir %llu\n", |
| 653 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | di = (struct ocfs2_dinode *)di_bh->b_data; |
| 658 | data = &di->id2.i_data; |
| 659 | |
| 660 | while (*f_pos < i_size_read(inode)) { |
| 661 | revalidate: |
| 662 | /* If the dir block has changed since the last call to |
| 663 | * readdir(2), then we might be pointing to an invalid |
| 664 | * dirent right now. Scan from the start of the block |
| 665 | * to make sure. */ |
| 666 | if (*f_version != inode->i_version) { |
| 667 | for (i = 0; i < i_size_read(inode) && i < offset; ) { |
| 668 | de = (struct ocfs2_dir_entry *) |
| 669 | (data->id_data + i); |
| 670 | /* It's too expensive to do a full |
| 671 | * dirent test each time round this |
| 672 | * loop, but we do have to test at |
| 673 | * least that it is non-zero. A |
| 674 | * failure will be detected in the |
| 675 | * dirent test below. */ |
| 676 | if (le16_to_cpu(de->rec_len) < |
| 677 | OCFS2_DIR_REC_LEN(1)) |
| 678 | break; |
| 679 | i += le16_to_cpu(de->rec_len); |
| 680 | } |
| 681 | *f_pos = offset = i; |
| 682 | *f_version = inode->i_version; |
| 683 | } |
| 684 | |
| 685 | de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos); |
| 686 | if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) { |
| 687 | /* On error, skip the f_pos to the end. */ |
| 688 | *f_pos = i_size_read(inode); |
| 689 | goto out; |
| 690 | } |
| 691 | offset += le16_to_cpu(de->rec_len); |
| 692 | if (le64_to_cpu(de->inode)) { |
| 693 | /* We might block in the next section |
| 694 | * if the data destination is |
| 695 | * currently swapped out. So, use a |
| 696 | * version stamp to detect whether or |
| 697 | * not the directory has been modified |
| 698 | * during the copy operation. |
| 699 | */ |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 700 | u64 version = *f_version; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 701 | unsigned char d_type = DT_UNKNOWN; |
| 702 | |
| 703 | if (de->file_type < OCFS2_FT_MAX) |
| 704 | d_type = ocfs2_filetype_table[de->file_type]; |
| 705 | |
| 706 | filldir_ret = filldir(priv, de->name, |
| 707 | de->name_len, |
| 708 | *f_pos, |
| 709 | le64_to_cpu(de->inode), |
| 710 | d_type); |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 711 | if (filldir_ret) { |
| 712 | if (filldir_err) |
| 713 | *filldir_err = filldir_ret; |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 714 | break; |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 715 | } |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 716 | if (version != *f_version) |
| 717 | goto revalidate; |
| 718 | } |
| 719 | *f_pos += le16_to_cpu(de->rec_len); |
| 720 | } |
| 721 | |
| 722 | out: |
| 723 | brelse(di_bh); |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static int ocfs2_dir_foreach_blk_el(struct inode *inode, |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 729 | u64 *f_version, |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 730 | loff_t *f_pos, void *priv, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 731 | filldir_t filldir, int *filldir_err) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 732 | { |
| 733 | int error = 0; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 734 | unsigned long offset, blk, last_ra_blk = 0; |
| 735 | int i, stored; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 736 | struct buffer_head * bh, * tmp; |
| 737 | struct ocfs2_dir_entry * de; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 738 | struct super_block * sb = inode->i_sb; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 739 | unsigned int ra_sectors = 16; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 740 | |
| 741 | stored = 0; |
| 742 | bh = NULL; |
| 743 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 744 | offset = (*f_pos) & (sb->s_blocksize - 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 745 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 746 | while (!error && !stored && *f_pos < i_size_read(inode)) { |
| 747 | blk = (*f_pos) >> sb->s_blocksize_bits; |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 748 | if (ocfs2_read_dir_block(inode, blk, &bh, 0)) { |
| 749 | /* Skip the corrupt dirblock and keep trying */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 750 | *f_pos += sb->s_blocksize - offset; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 751 | continue; |
| 752 | } |
| 753 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 754 | /* The idea here is to begin with 8k read-ahead and to stay |
| 755 | * 4k ahead of our current position. |
| 756 | * |
| 757 | * TODO: Use the pagecache for this. We just need to |
| 758 | * make sure it's cluster-safe... */ |
| 759 | if (!last_ra_blk |
| 760 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { |
| 761 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 762 | i > 0; i--) { |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 763 | tmp = NULL; |
| 764 | if (!ocfs2_read_dir_block(inode, ++blk, &tmp, |
| 765 | OCFS2_BH_READAHEAD)) |
| 766 | brelse(tmp); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 767 | } |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 768 | last_ra_blk = blk; |
| 769 | ra_sectors = 8; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | revalidate: |
| 773 | /* If the dir block has changed since the last call to |
| 774 | * readdir(2), then we might be pointing to an invalid |
| 775 | * dirent right now. Scan from the start of the block |
| 776 | * to make sure. */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 777 | if (*f_version != inode->i_version) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 778 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
| 779 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); |
| 780 | /* It's too expensive to do a full |
| 781 | * dirent test each time round this |
| 782 | * loop, but we do have to test at |
| 783 | * least that it is non-zero. A |
| 784 | * failure will be detected in the |
| 785 | * dirent test below. */ |
| 786 | if (le16_to_cpu(de->rec_len) < |
| 787 | OCFS2_DIR_REC_LEN(1)) |
| 788 | break; |
| 789 | i += le16_to_cpu(de->rec_len); |
| 790 | } |
| 791 | offset = i; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 792 | *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 793 | | offset; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 794 | *f_version = inode->i_version; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 795 | } |
| 796 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 797 | while (!error && *f_pos < i_size_read(inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 798 | && offset < sb->s_blocksize) { |
| 799 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); |
| 800 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 801 | /* On error, skip the f_pos to the |
| 802 | next block. */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 803 | *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 804 | brelse(bh); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 805 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 806 | } |
| 807 | offset += le16_to_cpu(de->rec_len); |
| 808 | if (le64_to_cpu(de->inode)) { |
| 809 | /* We might block in the next section |
| 810 | * if the data destination is |
| 811 | * currently swapped out. So, use a |
| 812 | * version stamp to detect whether or |
| 813 | * not the directory has been modified |
| 814 | * during the copy operation. |
| 815 | */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 816 | unsigned long version = *f_version; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 817 | unsigned char d_type = DT_UNKNOWN; |
| 818 | |
| 819 | if (de->file_type < OCFS2_FT_MAX) |
| 820 | d_type = ocfs2_filetype_table[de->file_type]; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 821 | error = filldir(priv, de->name, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 822 | de->name_len, |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 823 | *f_pos, |
Mark Fasheh | 7e85367 | 2007-09-10 17:30:26 -0700 | [diff] [blame] | 824 | le64_to_cpu(de->inode), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 825 | d_type); |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 826 | if (error) { |
| 827 | if (filldir_err) |
| 828 | *filldir_err = error; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 829 | break; |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 830 | } |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 831 | if (version != *f_version) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 832 | goto revalidate; |
| 833 | stored ++; |
| 834 | } |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 835 | *f_pos += le16_to_cpu(de->rec_len); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 836 | } |
| 837 | offset = 0; |
| 838 | brelse(bh); |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 839 | bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | stored = 0; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 843 | out: |
| 844 | return stored; |
| 845 | } |
| 846 | |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 847 | static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 848 | loff_t *f_pos, void *priv, filldir_t filldir, |
| 849 | int *filldir_err) |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 850 | { |
| 851 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 852 | return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 853 | filldir, filldir_err); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 854 | |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 855 | return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir, |
| 856 | filldir_err); |
Mark Fasheh | 23193e5 | 2007-09-12 13:01:18 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 859 | /* |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 860 | * This is intended to be called from inside other kernel functions, |
| 861 | * so we fake some arguments. |
| 862 | */ |
| 863 | int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv, |
| 864 | filldir_t filldir) |
| 865 | { |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 866 | int ret = 0, filldir_err = 0; |
Mathieu Desnoyers | 2b47c36 | 2007-10-16 23:27:21 -0700 | [diff] [blame] | 867 | u64 version = inode->i_version; |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 868 | |
| 869 | while (*f_pos < i_size_read(inode)) { |
| 870 | ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 871 | filldir, &filldir_err); |
| 872 | if (ret || filldir_err) |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 873 | break; |
| 874 | } |
| 875 | |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 876 | if (ret > 0) |
| 877 | ret = -EIO; |
| 878 | |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame] | 879 | return 0; |
| 880 | } |
| 881 | |
| 882 | /* |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 883 | * ocfs2_readdir() |
| 884 | * |
| 885 | */ |
| 886 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 887 | { |
| 888 | int error = 0; |
| 889 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 890 | int lock_level = 0; |
| 891 | |
| 892 | mlog_entry("dirino=%llu\n", |
| 893 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 894 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 895 | error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 896 | if (lock_level && error >= 0) { |
| 897 | /* We release EX lock which used to update atime |
| 898 | * and get PR lock again to reduce contention |
| 899 | * on commonly accessed directories. */ |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 900 | ocfs2_inode_unlock(inode, 1); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 901 | lock_level = 0; |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 902 | error = ocfs2_inode_lock(inode, NULL, 0); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 903 | } |
| 904 | if (error < 0) { |
| 905 | if (error != -ENOENT) |
| 906 | mlog_errno(error); |
| 907 | /* we haven't got any yet, so propagate the error. */ |
| 908 | goto bail_nolock; |
| 909 | } |
| 910 | |
| 911 | error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos, |
Mark Fasheh | e7b3401 | 2007-09-24 14:25:27 -0700 | [diff] [blame] | 912 | dirent, filldir, NULL); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 913 | |
Mark Fasheh | e63aecb6 | 2007-10-18 15:30:42 -0700 | [diff] [blame] | 914 | ocfs2_inode_unlock(inode, lock_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 915 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 916 | bail_nolock: |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 917 | mlog_exit(error); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 918 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 919 | return error; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 923 | * NOTE: this should always be called with parent dir i_mutex taken. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 924 | */ |
| 925 | int ocfs2_find_files_on_disk(const char *name, |
| 926 | int namelen, |
| 927 | u64 *blkno, |
| 928 | struct inode *inode, |
| 929 | struct buffer_head **dirent_bh, |
| 930 | struct ocfs2_dir_entry **dirent) |
| 931 | { |
| 932 | int status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 933 | |
Joel Becker | 2b388c6 | 2006-05-10 18:28:59 -0700 | [diff] [blame] | 934 | mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", |
| 935 | namelen, name, blkno, inode, dirent_bh, dirent); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 936 | |
| 937 | *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); |
| 938 | if (!*dirent_bh || !*dirent) { |
| 939 | status = -ENOENT; |
| 940 | goto leave; |
| 941 | } |
| 942 | |
| 943 | *blkno = le64_to_cpu((*dirent)->inode); |
| 944 | |
| 945 | status = 0; |
| 946 | leave: |
| 947 | if (status < 0) { |
| 948 | *dirent = NULL; |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 949 | brelse(*dirent_bh); |
| 950 | *dirent_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | mlog_exit(status); |
| 954 | return status; |
| 955 | } |
| 956 | |
Mark Fasheh | be94d11 | 2007-09-11 15:22:06 -0700 | [diff] [blame] | 957 | /* |
| 958 | * Convenience function for callers which just want the block number |
| 959 | * mapped to a name and don't require the full dirent info, etc. |
| 960 | */ |
| 961 | int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name, |
| 962 | int namelen, u64 *blkno) |
| 963 | { |
| 964 | int ret; |
| 965 | struct buffer_head *bh = NULL; |
| 966 | struct ocfs2_dir_entry *dirent = NULL; |
| 967 | |
| 968 | ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &bh, &dirent); |
| 969 | brelse(bh); |
| 970 | |
| 971 | return ret; |
| 972 | } |
| 973 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 974 | /* Check for a name within a directory. |
| 975 | * |
| 976 | * Return 0 if the name does not exist |
| 977 | * Return -EEXIST if the directory contains the name |
| 978 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 979 | * Callers should have i_mutex + a cluster lock on dir |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 980 | */ |
| 981 | int ocfs2_check_dir_for_entry(struct inode *dir, |
| 982 | const char *name, |
| 983 | int namelen) |
| 984 | { |
| 985 | int ret; |
| 986 | struct buffer_head *dirent_bh = NULL; |
| 987 | struct ocfs2_dir_entry *dirent = NULL; |
| 988 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 989 | mlog_entry("dir %llu, name '%.*s'\n", |
| 990 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 991 | |
| 992 | ret = -EEXIST; |
| 993 | dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); |
| 994 | if (dirent_bh) |
| 995 | goto bail; |
| 996 | |
| 997 | ret = 0; |
| 998 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 999 | brelse(dirent_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1000 | |
| 1001 | mlog_exit(ret); |
| 1002 | return ret; |
| 1003 | } |
| 1004 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1005 | struct ocfs2_empty_dir_priv { |
| 1006 | unsigned seen_dot; |
| 1007 | unsigned seen_dot_dot; |
| 1008 | unsigned seen_other; |
| 1009 | }; |
| 1010 | static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len, |
| 1011 | loff_t pos, u64 ino, unsigned type) |
| 1012 | { |
| 1013 | struct ocfs2_empty_dir_priv *p = priv; |
| 1014 | |
| 1015 | /* |
| 1016 | * Check the positions of "." and ".." records to be sure |
| 1017 | * they're in the correct place. |
| 1018 | */ |
| 1019 | if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) { |
| 1020 | p->seen_dot = 1; |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | if (name_len == 2 && !strncmp("..", name, 2) && |
| 1025 | pos == OCFS2_DIR_REC_LEN(1)) { |
| 1026 | p->seen_dot_dot = 1; |
| 1027 | return 0; |
| 1028 | } |
| 1029 | |
| 1030 | p->seen_other = 1; |
| 1031 | return 1; |
| 1032 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1033 | /* |
| 1034 | * routine to check that the specified directory is empty (for rmdir) |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1035 | * |
| 1036 | * Returns 1 if dir is empty, zero otherwise. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1037 | */ |
| 1038 | int ocfs2_empty_dir(struct inode *inode) |
| 1039 | { |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1040 | int ret; |
| 1041 | loff_t start = 0; |
| 1042 | struct ocfs2_empty_dir_priv priv; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1043 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1044 | memset(&priv, 0, sizeof(priv)); |
| 1045 | |
| 1046 | ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir); |
| 1047 | if (ret) |
| 1048 | mlog_errno(ret); |
| 1049 | |
| 1050 | if (!priv.seen_dot || !priv.seen_dot_dot) { |
| 1051 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1052 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * XXX: Is it really safe to allow an unlink to continue? |
| 1055 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1056 | return 1; |
| 1057 | } |
| 1058 | |
Mark Fasheh | 0bfbbf6 | 2007-09-12 11:19:00 -0700 | [diff] [blame] | 1059 | return !priv.seen_other; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1062 | static void ocfs2_fill_initial_dirents(struct inode *inode, |
| 1063 | struct inode *parent, |
| 1064 | char *start, unsigned int size) |
| 1065 | { |
| 1066 | struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start; |
| 1067 | |
| 1068 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); |
| 1069 | de->name_len = 1; |
| 1070 | de->rec_len = |
| 1071 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 1072 | strcpy(de->name, "."); |
| 1073 | ocfs2_set_de_type(de, S_IFDIR); |
| 1074 | |
| 1075 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); |
| 1076 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); |
| 1077 | de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1)); |
| 1078 | de->name_len = 2; |
| 1079 | strcpy(de->name, ".."); |
| 1080 | ocfs2_set_de_type(de, S_IFDIR); |
| 1081 | } |
| 1082 | |
| 1083 | /* |
| 1084 | * This works together with code in ocfs2_mknod_locked() which sets |
| 1085 | * the inline-data flag and initializes the inline-data section. |
| 1086 | */ |
| 1087 | static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb, |
| 1088 | handle_t *handle, |
| 1089 | struct inode *parent, |
| 1090 | struct inode *inode, |
| 1091 | struct buffer_head *di_bh) |
| 1092 | { |
| 1093 | int ret; |
| 1094 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1095 | struct ocfs2_inline_data *data = &di->id2.i_data; |
| 1096 | unsigned int size = le16_to_cpu(data->id_count); |
| 1097 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 1098 | ret = ocfs2_journal_access_di(handle, inode, di_bh, |
| 1099 | OCFS2_JOURNAL_ACCESS_WRITE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1100 | if (ret) { |
| 1101 | mlog_errno(ret); |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
| 1105 | ocfs2_fill_initial_dirents(inode, parent, data->id_data, size); |
| 1106 | |
| 1107 | ocfs2_journal_dirty(handle, di_bh); |
| 1108 | if (ret) { |
| 1109 | mlog_errno(ret); |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
| 1113 | i_size_write(inode, size); |
| 1114 | inode->i_nlink = 2; |
| 1115 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 1116 | |
| 1117 | ret = ocfs2_mark_inode_dirty(handle, inode, di_bh); |
| 1118 | if (ret < 0) |
| 1119 | mlog_errno(ret); |
| 1120 | |
| 1121 | out: |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb, |
| 1126 | handle_t *handle, |
| 1127 | struct inode *parent, |
| 1128 | struct inode *inode, |
| 1129 | struct buffer_head *fe_bh, |
| 1130 | struct ocfs2_alloc_context *data_ac) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1131 | { |
| 1132 | int status; |
| 1133 | struct buffer_head *new_bh = NULL; |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1134 | |
| 1135 | mlog_entry_void(); |
| 1136 | |
| 1137 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, |
| 1138 | data_ac, NULL, &new_bh); |
| 1139 | if (status < 0) { |
| 1140 | mlog_errno(status); |
| 1141 | goto bail; |
| 1142 | } |
| 1143 | |
| 1144 | ocfs2_set_new_buffer_uptodate(inode, new_bh); |
| 1145 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 1146 | status = ocfs2_journal_access_db(handle, inode, new_bh, |
| 1147 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1148 | if (status < 0) { |
| 1149 | mlog_errno(status); |
| 1150 | goto bail; |
| 1151 | } |
| 1152 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); |
| 1153 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1154 | ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, |
| 1155 | osb->sb->s_blocksize); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1156 | |
| 1157 | status = ocfs2_journal_dirty(handle, new_bh); |
| 1158 | if (status < 0) { |
| 1159 | mlog_errno(status); |
| 1160 | goto bail; |
| 1161 | } |
| 1162 | |
| 1163 | i_size_write(inode, inode->i_sb->s_blocksize); |
| 1164 | inode->i_nlink = 2; |
| 1165 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 1166 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 1167 | if (status < 0) { |
| 1168 | mlog_errno(status); |
| 1169 | goto bail; |
| 1170 | } |
| 1171 | |
| 1172 | status = 0; |
| 1173 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1174 | brelse(new_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1175 | |
| 1176 | mlog_exit(status); |
| 1177 | return status; |
| 1178 | } |
| 1179 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1180 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
| 1181 | handle_t *handle, |
| 1182 | struct inode *parent, |
| 1183 | struct inode *inode, |
| 1184 | struct buffer_head *fe_bh, |
| 1185 | struct ocfs2_alloc_context *data_ac) |
| 1186 | { |
| 1187 | BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL); |
| 1188 | |
| 1189 | if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) |
| 1190 | return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh); |
| 1191 | |
| 1192 | return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh, |
| 1193 | data_ac); |
| 1194 | } |
| 1195 | |
| 1196 | static void ocfs2_expand_last_dirent(char *start, unsigned int old_size, |
| 1197 | unsigned int new_size) |
| 1198 | { |
| 1199 | struct ocfs2_dir_entry *de; |
| 1200 | struct ocfs2_dir_entry *prev_de; |
| 1201 | char *de_buf, *limit; |
| 1202 | unsigned int bytes = new_size - old_size; |
| 1203 | |
| 1204 | limit = start + old_size; |
| 1205 | de_buf = start; |
| 1206 | de = (struct ocfs2_dir_entry *)de_buf; |
| 1207 | do { |
| 1208 | prev_de = de; |
| 1209 | de_buf += le16_to_cpu(de->rec_len); |
| 1210 | de = (struct ocfs2_dir_entry *)de_buf; |
| 1211 | } while (de_buf < limit); |
| 1212 | |
| 1213 | le16_add_cpu(&prev_de->rec_len, bytes); |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * We allocate enough clusters to fulfill "blocks_wanted", but set |
| 1218 | * i_size to exactly one block. Ocfs2_extend_dir() will handle the |
| 1219 | * rest automatically for us. |
| 1220 | * |
| 1221 | * *first_block_bh is a pointer to the 1st data block allocated to the |
| 1222 | * directory. |
| 1223 | */ |
| 1224 | static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh, |
| 1225 | unsigned int blocks_wanted, |
| 1226 | struct buffer_head **first_block_bh) |
| 1227 | { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1228 | u32 alloc, bit_off, len; |
| 1229 | struct super_block *sb = dir->i_sb; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1230 | int ret, credits = ocfs2_inline_to_extents_credits(sb); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1231 | u64 blkno, bytes = blocks_wanted << sb->s_blocksize_bits; |
| 1232 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 1233 | struct ocfs2_inode_info *oi = OCFS2_I(dir); |
| 1234 | struct ocfs2_alloc_context *data_ac; |
| 1235 | struct buffer_head *dirdata_bh = NULL; |
| 1236 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1237 | handle_t *handle; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1238 | struct ocfs2_extent_tree et; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1239 | int did_quota = 0; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1240 | |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 1241 | ocfs2_init_dinode_extent_tree(&et, dir, di_bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1242 | |
| 1243 | alloc = ocfs2_clusters_for_bytes(sb, bytes); |
| 1244 | |
| 1245 | /* |
| 1246 | * We should never need more than 2 clusters for this - |
| 1247 | * maximum dirent size is far less than one block. In fact, |
| 1248 | * the only time we'd need more than one cluster is if |
| 1249 | * blocksize == clustersize and the dirent won't fit in the |
| 1250 | * extra space that the expansion to a single block gives. As |
| 1251 | * of today, that only happens on 4k/4k file systems. |
| 1252 | */ |
| 1253 | BUG_ON(alloc > 2); |
| 1254 | |
| 1255 | ret = ocfs2_reserve_clusters(osb, alloc, &data_ac); |
| 1256 | if (ret) { |
| 1257 | mlog_errno(ret); |
| 1258 | goto out; |
| 1259 | } |
| 1260 | |
| 1261 | down_write(&oi->ip_alloc_sem); |
| 1262 | |
| 1263 | /* |
Joe Perches | c78bad1 | 2008-02-03 17:33:42 +0200 | [diff] [blame] | 1264 | * Prepare for worst case allocation scenario of two separate |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1265 | * extents. |
| 1266 | */ |
| 1267 | if (alloc == 2) |
| 1268 | credits += OCFS2_SUBALLOC_ALLOC; |
| 1269 | |
| 1270 | handle = ocfs2_start_trans(osb, credits); |
| 1271 | if (IS_ERR(handle)) { |
| 1272 | ret = PTR_ERR(handle); |
| 1273 | mlog_errno(ret); |
| 1274 | goto out_sem; |
| 1275 | } |
| 1276 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1277 | if (vfs_dq_alloc_space_nodirty(dir, |
| 1278 | ocfs2_clusters_to_bytes(osb->sb, alloc))) { |
| 1279 | ret = -EDQUOT; |
| 1280 | goto out_commit; |
| 1281 | } |
| 1282 | did_quota = 1; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1283 | /* |
| 1284 | * Try to claim as many clusters as the bitmap can give though |
| 1285 | * if we only get one now, that's enough to continue. The rest |
| 1286 | * will be claimed after the conversion to extents. |
| 1287 | */ |
| 1288 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len); |
| 1289 | if (ret) { |
| 1290 | mlog_errno(ret); |
| 1291 | goto out_commit; |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * Operations are carefully ordered so that we set up the new |
| 1296 | * data block first. The conversion from inline data to |
| 1297 | * extents follows. |
| 1298 | */ |
| 1299 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); |
| 1300 | dirdata_bh = sb_getblk(sb, blkno); |
| 1301 | if (!dirdata_bh) { |
| 1302 | ret = -EIO; |
| 1303 | mlog_errno(ret); |
| 1304 | goto out_commit; |
| 1305 | } |
| 1306 | |
| 1307 | ocfs2_set_new_buffer_uptodate(dir, dirdata_bh); |
| 1308 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 1309 | ret = ocfs2_journal_access_db(handle, dir, dirdata_bh, |
| 1310 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1311 | if (ret) { |
| 1312 | mlog_errno(ret); |
| 1313 | goto out_commit; |
| 1314 | } |
| 1315 | |
| 1316 | memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir)); |
| 1317 | memset(dirdata_bh->b_data + i_size_read(dir), 0, |
| 1318 | sb->s_blocksize - i_size_read(dir)); |
| 1319 | ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), |
| 1320 | sb->s_blocksize); |
| 1321 | |
| 1322 | ret = ocfs2_journal_dirty(handle, dirdata_bh); |
| 1323 | if (ret) { |
| 1324 | mlog_errno(ret); |
| 1325 | goto out_commit; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Set extent, i_size, etc on the directory. After this, the |
| 1330 | * inode should contain the same exact dirents as before and |
| 1331 | * be fully accessible from system calls. |
| 1332 | * |
| 1333 | * We let the later dirent insert modify c/mtime - to the user |
| 1334 | * the data hasn't changed. |
| 1335 | */ |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 1336 | ret = ocfs2_journal_access_di(handle, dir, di_bh, |
| 1337 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1338 | if (ret) { |
| 1339 | mlog_errno(ret); |
| 1340 | goto out_commit; |
| 1341 | } |
| 1342 | |
| 1343 | spin_lock(&oi->ip_lock); |
| 1344 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; |
| 1345 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); |
| 1346 | spin_unlock(&oi->ip_lock); |
| 1347 | |
| 1348 | ocfs2_dinode_new_extent_list(dir, di); |
| 1349 | |
| 1350 | i_size_write(dir, sb->s_blocksize); |
| 1351 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 1352 | |
| 1353 | di->i_size = cpu_to_le64(sb->s_blocksize); |
| 1354 | di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec); |
| 1355 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * This should never fail as our extent list is empty and all |
| 1359 | * related blocks have been journaled already. |
| 1360 | */ |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1361 | ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len, |
| 1362 | 0, NULL); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1363 | if (ret) { |
| 1364 | mlog_errno(ret); |
Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 1365 | goto out_commit; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Mark Fasheh | 9780eb6 | 2008-08-05 11:32:46 -0700 | [diff] [blame] | 1368 | /* |
| 1369 | * Set i_blocks after the extent insert for the most up to |
| 1370 | * date ip_clusters value. |
| 1371 | */ |
| 1372 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
| 1373 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1374 | ret = ocfs2_journal_dirty(handle, di_bh); |
| 1375 | if (ret) { |
| 1376 | mlog_errno(ret); |
| 1377 | goto out_commit; |
| 1378 | } |
| 1379 | |
| 1380 | /* |
| 1381 | * We asked for two clusters, but only got one in the 1st |
| 1382 | * pass. Claim the 2nd cluster as a separate extent. |
| 1383 | */ |
| 1384 | if (alloc > len) { |
| 1385 | ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, |
| 1386 | &len); |
| 1387 | if (ret) { |
| 1388 | mlog_errno(ret); |
| 1389 | goto out_commit; |
| 1390 | } |
| 1391 | blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off); |
| 1392 | |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1393 | ret = ocfs2_insert_extent(osb, handle, dir, &et, 1, |
| 1394 | blkno, len, 0, NULL); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1395 | if (ret) { |
| 1396 | mlog_errno(ret); |
Tao Ma | 83cab53 | 2008-08-21 14:14:27 +0800 | [diff] [blame] | 1397 | goto out_commit; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | *first_block_bh = dirdata_bh; |
| 1402 | dirdata_bh = NULL; |
| 1403 | |
| 1404 | out_commit: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1405 | if (ret < 0 && did_quota) |
| 1406 | vfs_dq_free_space_nodirty(dir, |
| 1407 | ocfs2_clusters_to_bytes(osb->sb, 2)); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1408 | ocfs2_commit_trans(osb, handle); |
| 1409 | |
| 1410 | out_sem: |
| 1411 | up_write(&oi->ip_alloc_sem); |
| 1412 | |
| 1413 | out: |
| 1414 | if (data_ac) |
| 1415 | ocfs2_free_alloc_context(data_ac); |
| 1416 | |
| 1417 | brelse(dirdata_bh); |
| 1418 | |
| 1419 | return ret; |
| 1420 | } |
| 1421 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1422 | /* returns a bh of the 1st new block in the allocation. */ |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 1423 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 1424 | handle_t *handle, |
| 1425 | struct inode *dir, |
| 1426 | struct buffer_head *parent_fe_bh, |
| 1427 | struct ocfs2_alloc_context *data_ac, |
| 1428 | struct ocfs2_alloc_context *meta_ac, |
| 1429 | struct buffer_head **new_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1430 | { |
| 1431 | int status; |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1432 | int extend, did_quota = 0; |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1433 | u64 p_blkno, v_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1434 | |
| 1435 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 1436 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); |
| 1437 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 1438 | |
| 1439 | if (extend) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1440 | u32 offset = OCFS2_I(dir)->ip_clusters; |
| 1441 | |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1442 | if (vfs_dq_alloc_space_nodirty(dir, |
| 1443 | ocfs2_clusters_to_bytes(sb, 1))) { |
| 1444 | status = -EDQUOT; |
| 1445 | goto bail; |
| 1446 | } |
| 1447 | did_quota = 1; |
| 1448 | |
Tao Ma | 0eb8d47 | 2008-08-18 17:38:45 +0800 | [diff] [blame] | 1449 | status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset, |
| 1450 | 1, 0, parent_fe_bh, handle, |
| 1451 | data_ac, meta_ac, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1452 | BUG_ON(status == -EAGAIN); |
| 1453 | if (status < 0) { |
| 1454 | mlog_errno(status); |
| 1455 | goto bail; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1459 | v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); |
| 1460 | 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] | 1461 | if (status < 0) { |
| 1462 | mlog_errno(status); |
| 1463 | goto bail; |
| 1464 | } |
| 1465 | |
| 1466 | *new_bh = sb_getblk(sb, p_blkno); |
| 1467 | if (!*new_bh) { |
| 1468 | status = -EIO; |
| 1469 | mlog_errno(status); |
| 1470 | goto bail; |
| 1471 | } |
| 1472 | status = 0; |
| 1473 | bail: |
Jan Kara | a90714c | 2008-10-09 19:38:40 +0200 | [diff] [blame] | 1474 | if (did_quota && status < 0) |
| 1475 | vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1)); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1476 | mlog_exit(status); |
| 1477 | return status; |
| 1478 | } |
| 1479 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1480 | /* |
| 1481 | * Assumes you already have a cluster lock on the directory. |
| 1482 | * |
| 1483 | * 'blocks_wanted' is only used if we have an inline directory which |
| 1484 | * is to be turned into an extent based one. The size of the dirent to |
| 1485 | * insert might be larger than the space gained by growing to just one |
| 1486 | * block, so we may have to grow the inode by two blocks in that case. |
| 1487 | */ |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1488 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 1489 | struct inode *dir, |
| 1490 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1491 | unsigned int blocks_wanted, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1492 | struct buffer_head **new_de_bh) |
| 1493 | { |
| 1494 | int status = 0; |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1495 | int credits, num_free_extents, drop_alloc_sem = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1496 | loff_t dir_i_size; |
| 1497 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1498 | struct ocfs2_extent_list *el = &fe->id2.i_list; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1499 | struct ocfs2_alloc_context *data_ac = NULL; |
| 1500 | struct ocfs2_alloc_context *meta_ac = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1501 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1502 | struct buffer_head *new_bh = NULL; |
| 1503 | struct ocfs2_dir_entry * de; |
| 1504 | struct super_block *sb = osb->sb; |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1505 | struct ocfs2_extent_tree et; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1506 | |
| 1507 | mlog_entry_void(); |
| 1508 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1509 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1510 | status = ocfs2_expand_inline_dir(dir, parent_fe_bh, |
| 1511 | blocks_wanted, &new_bh); |
| 1512 | if (status) { |
| 1513 | mlog_errno(status); |
| 1514 | goto bail; |
| 1515 | } |
| 1516 | |
| 1517 | if (blocks_wanted == 1) { |
| 1518 | /* |
| 1519 | * If the new dirent will fit inside the space |
| 1520 | * created by pushing out to one block, then |
| 1521 | * we can complete the operation |
| 1522 | * here. Otherwise we have to expand i_size |
| 1523 | * and format the 2nd block below. |
| 1524 | */ |
| 1525 | BUG_ON(new_bh == NULL); |
| 1526 | goto bail_bh; |
| 1527 | } |
| 1528 | |
| 1529 | /* |
| 1530 | * Get rid of 'new_bh' - we want to format the 2nd |
| 1531 | * data block and return that instead. |
| 1532 | */ |
| 1533 | brelse(new_bh); |
| 1534 | new_bh = NULL; |
| 1535 | |
| 1536 | dir_i_size = i_size_read(dir); |
| 1537 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 1538 | goto do_extend; |
| 1539 | } |
| 1540 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1541 | dir_i_size = i_size_read(dir); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1542 | mlog(0, "extending dir %llu (i_size = %lld)\n", |
| 1543 | (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1544 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1545 | /* dir->i_size is always block aligned. */ |
| 1546 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 1547 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { |
| 1548 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
Joel Becker | 8d6220d | 2008-08-22 12:46:09 -0700 | [diff] [blame] | 1549 | ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh); |
Joel Becker | f99b9b7 | 2008-08-20 19:36:33 -0700 | [diff] [blame] | 1550 | num_free_extents = ocfs2_num_free_extents(osb, dir, &et); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1551 | if (num_free_extents < 0) { |
| 1552 | status = num_free_extents; |
| 1553 | mlog_errno(status); |
| 1554 | goto bail; |
| 1555 | } |
| 1556 | |
| 1557 | if (!num_free_extents) { |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1558 | status = ocfs2_reserve_new_metadata(osb, el, &meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1559 | if (status < 0) { |
| 1560 | if (status != -ENOSPC) |
| 1561 | mlog_errno(status); |
| 1562 | goto bail; |
| 1563 | } |
| 1564 | } |
| 1565 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1566 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1567 | if (status < 0) { |
| 1568 | if (status != -ENOSPC) |
| 1569 | mlog_errno(status); |
| 1570 | goto bail; |
| 1571 | } |
| 1572 | |
Tao Ma | 811f933 | 2008-08-18 17:38:43 +0800 | [diff] [blame] | 1573 | credits = ocfs2_calc_extend_credits(sb, el, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1574 | } else { |
| 1575 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 1576 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 1577 | } |
| 1578 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1579 | do_extend: |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1580 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
| 1581 | drop_alloc_sem = 1; |
| 1582 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1583 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1584 | if (IS_ERR(handle)) { |
| 1585 | status = PTR_ERR(handle); |
| 1586 | handle = NULL; |
| 1587 | mlog_errno(status); |
| 1588 | goto bail; |
| 1589 | } |
| 1590 | |
| 1591 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, |
| 1592 | data_ac, meta_ac, &new_bh); |
| 1593 | if (status < 0) { |
| 1594 | mlog_errno(status); |
| 1595 | goto bail; |
| 1596 | } |
| 1597 | |
| 1598 | ocfs2_set_new_buffer_uptodate(dir, new_bh); |
| 1599 | |
Joel Becker | 13723d0 | 2008-10-17 19:25:01 -0700 | [diff] [blame^] | 1600 | status = ocfs2_journal_access_db(handle, dir, new_bh, |
| 1601 | OCFS2_JOURNAL_ACCESS_CREATE); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1602 | if (status < 0) { |
| 1603 | mlog_errno(status); |
| 1604 | goto bail; |
| 1605 | } |
| 1606 | memset(new_bh->b_data, 0, sb->s_blocksize); |
| 1607 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 1608 | de->inode = 0; |
| 1609 | de->rec_len = cpu_to_le16(sb->s_blocksize); |
| 1610 | status = ocfs2_journal_dirty(handle, new_bh); |
| 1611 | if (status < 0) { |
| 1612 | mlog_errno(status); |
| 1613 | goto bail; |
| 1614 | } |
| 1615 | |
| 1616 | dir_i_size += dir->i_sb->s_blocksize; |
| 1617 | i_size_write(dir, dir_i_size); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1618 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1619 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 1620 | if (status < 0) { |
| 1621 | mlog_errno(status); |
| 1622 | goto bail; |
| 1623 | } |
| 1624 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1625 | bail_bh: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1626 | *new_de_bh = new_bh; |
| 1627 | get_bh(*new_de_bh); |
| 1628 | bail: |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 1629 | if (drop_alloc_sem) |
| 1630 | up_write(&OCFS2_I(dir)->ip_alloc_sem); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1631 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1632 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1633 | |
| 1634 | if (data_ac) |
| 1635 | ocfs2_free_alloc_context(data_ac); |
| 1636 | if (meta_ac) |
| 1637 | ocfs2_free_alloc_context(meta_ac); |
| 1638 | |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1639 | brelse(new_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1640 | |
| 1641 | mlog_exit(status); |
| 1642 | return status; |
| 1643 | } |
| 1644 | |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1645 | static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh, |
| 1646 | const char *name, int namelen, |
| 1647 | struct buffer_head **ret_de_bh, |
| 1648 | unsigned int *blocks_wanted) |
| 1649 | { |
| 1650 | int ret; |
| 1651 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
| 1652 | struct ocfs2_dir_entry *de, *last_de = NULL; |
| 1653 | char *de_buf, *limit; |
| 1654 | unsigned long offset = 0; |
| 1655 | unsigned int rec_len, new_rec_len; |
| 1656 | |
| 1657 | de_buf = di->id2.i_data.id_data; |
| 1658 | limit = de_buf + i_size_read(dir); |
| 1659 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 1660 | |
| 1661 | while (de_buf < limit) { |
| 1662 | de = (struct ocfs2_dir_entry *)de_buf; |
| 1663 | |
| 1664 | if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) { |
| 1665 | ret = -ENOENT; |
| 1666 | goto out; |
| 1667 | } |
| 1668 | if (ocfs2_match(namelen, name, de)) { |
| 1669 | ret = -EEXIST; |
| 1670 | goto out; |
| 1671 | } |
| 1672 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
| 1673 | /* Ok, we found a spot. Return this bh and let |
| 1674 | * the caller actually fill it in. */ |
| 1675 | *ret_de_bh = di_bh; |
| 1676 | get_bh(*ret_de_bh); |
| 1677 | ret = 0; |
| 1678 | goto out; |
| 1679 | } |
| 1680 | |
| 1681 | last_de = de; |
| 1682 | de_buf += le16_to_cpu(de->rec_len); |
| 1683 | offset += le16_to_cpu(de->rec_len); |
| 1684 | } |
| 1685 | |
| 1686 | /* |
| 1687 | * We're going to require expansion of the directory - figure |
| 1688 | * out how many blocks we'll need so that a place for the |
| 1689 | * dirent can be found. |
| 1690 | */ |
| 1691 | *blocks_wanted = 1; |
| 1692 | new_rec_len = le16_to_cpu(last_de->rec_len) + (dir->i_sb->s_blocksize - i_size_read(dir)); |
| 1693 | if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len))) |
| 1694 | *blocks_wanted = 2; |
| 1695 | |
| 1696 | ret = -ENOSPC; |
| 1697 | out: |
| 1698 | return ret; |
| 1699 | } |
| 1700 | |
| 1701 | static int ocfs2_find_dir_space_el(struct inode *dir, const char *name, |
| 1702 | int namelen, struct buffer_head **ret_de_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1703 | { |
| 1704 | unsigned long offset; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1705 | struct buffer_head *bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1706 | unsigned short rec_len; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1707 | struct ocfs2_dir_entry *de; |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1708 | struct super_block *sb = dir->i_sb; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1709 | int status; |
| 1710 | |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1711 | status = ocfs2_read_dir_block(dir, 0, &bh, 0); |
| 1712 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1713 | mlog_errno(status); |
| 1714 | goto bail; |
| 1715 | } |
| 1716 | |
| 1717 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 1718 | offset = 0; |
| 1719 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 1720 | while (1) { |
| 1721 | if ((char *)de >= sb->s_blocksize + bh->b_data) { |
| 1722 | brelse(bh); |
| 1723 | bh = NULL; |
| 1724 | |
| 1725 | if (i_size_read(dir) <= offset) { |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1726 | /* |
| 1727 | * Caller will have to expand this |
| 1728 | * directory. |
| 1729 | */ |
| 1730 | status = -ENOSPC; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1731 | goto bail; |
| 1732 | } |
Joel Becker | a22305c | 2008-11-13 14:49:17 -0800 | [diff] [blame] | 1733 | status = ocfs2_read_dir_block(dir, |
| 1734 | offset >> sb->s_blocksize_bits, |
| 1735 | &bh, 0); |
| 1736 | if (status) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1737 | mlog_errno(status); |
| 1738 | goto bail; |
| 1739 | } |
| 1740 | /* move to next block */ |
| 1741 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 1742 | } |
| 1743 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 1744 | status = -ENOENT; |
| 1745 | goto bail; |
| 1746 | } |
| 1747 | if (ocfs2_match(namelen, name, de)) { |
| 1748 | status = -EEXIST; |
| 1749 | goto bail; |
| 1750 | } |
Mark Fasheh | 8553cf4 | 2007-09-13 16:29:01 -0700 | [diff] [blame] | 1751 | if (ocfs2_dirent_would_fit(de, rec_len)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1752 | /* Ok, we found a spot. Return this bh and let |
| 1753 | * the caller actually fill it in. */ |
| 1754 | *ret_de_bh = bh; |
| 1755 | get_bh(*ret_de_bh); |
| 1756 | status = 0; |
| 1757 | goto bail; |
| 1758 | } |
| 1759 | offset += le16_to_cpu(de->rec_len); |
| 1760 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); |
| 1761 | } |
| 1762 | |
| 1763 | status = 0; |
| 1764 | bail: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1765 | brelse(bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1766 | |
| 1767 | mlog_exit(status); |
| 1768 | return status; |
| 1769 | } |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1770 | |
| 1771 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, |
| 1772 | struct inode *dir, |
| 1773 | struct buffer_head *parent_fe_bh, |
| 1774 | const char *name, |
| 1775 | int namelen, |
| 1776 | struct buffer_head **ret_de_bh) |
| 1777 | { |
| 1778 | int ret; |
| 1779 | unsigned int blocks_wanted = 1; |
| 1780 | struct buffer_head *bh = NULL; |
| 1781 | |
| 1782 | mlog(0, "getting ready to insert namelen %d into dir %llu\n", |
| 1783 | namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); |
| 1784 | |
| 1785 | *ret_de_bh = NULL; |
| 1786 | |
| 1787 | if (!namelen) { |
| 1788 | ret = -EINVAL; |
| 1789 | mlog_errno(ret); |
| 1790 | goto out; |
| 1791 | } |
| 1792 | |
| 1793 | if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) { |
| 1794 | ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name, |
| 1795 | namelen, &bh, &blocks_wanted); |
| 1796 | } else |
| 1797 | ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh); |
| 1798 | |
| 1799 | if (ret && ret != -ENOSPC) { |
| 1800 | mlog_errno(ret); |
| 1801 | goto out; |
| 1802 | } |
| 1803 | |
| 1804 | if (ret == -ENOSPC) { |
| 1805 | /* |
| 1806 | * We have to expand the directory to add this name. |
| 1807 | */ |
| 1808 | BUG_ON(bh); |
| 1809 | |
| 1810 | ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted, |
| 1811 | &bh); |
| 1812 | if (ret) { |
| 1813 | if (ret != -ENOSPC) |
| 1814 | mlog_errno(ret); |
| 1815 | goto out; |
| 1816 | } |
| 1817 | |
| 1818 | BUG_ON(!bh); |
| 1819 | } |
| 1820 | |
| 1821 | *ret_de_bh = bh; |
| 1822 | bh = NULL; |
| 1823 | out: |
Mark Fasheh | a81cb88 | 2008-10-07 14:25:16 -0700 | [diff] [blame] | 1824 | brelse(bh); |
Mark Fasheh | 5b6a3a2 | 2007-09-13 16:33:54 -0700 | [diff] [blame] | 1825 | return ret; |
| 1826 | } |