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