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