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, |
| 75 | struct buffer_head **new_de_bh); |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 76 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 77 | handle_t *handle, |
| 78 | struct inode *dir, |
| 79 | struct buffer_head *parent_fe_bh, |
| 80 | struct ocfs2_alloc_context *data_ac, |
| 81 | struct ocfs2_alloc_context *meta_ac, |
| 82 | struct buffer_head **new_bh); |
| 83 | |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame^] | 84 | static int ocfs2_check_dir_entry(struct inode * dir, |
| 85 | struct ocfs2_dir_entry * de, |
| 86 | struct buffer_head * bh, |
| 87 | unsigned long offset) |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 88 | { |
| 89 | const char *error_msg = NULL; |
| 90 | const int rlen = le16_to_cpu(de->rec_len); |
| 91 | |
| 92 | if (rlen < OCFS2_DIR_REC_LEN(1)) |
| 93 | error_msg = "rec_len is smaller than minimal"; |
| 94 | else if (rlen % 4 != 0) |
| 95 | error_msg = "rec_len % 4 != 0"; |
| 96 | else if (rlen < OCFS2_DIR_REC_LEN(de->name_len)) |
| 97 | error_msg = "rec_len is too small for name_len"; |
| 98 | else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize) |
| 99 | error_msg = "directory entry across blocks"; |
| 100 | |
| 101 | if (error_msg != NULL) |
| 102 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " |
| 103 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", |
| 104 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, |
| 105 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, |
| 106 | de->name_len); |
| 107 | return error_msg == NULL ? 1 : 0; |
| 108 | } |
| 109 | |
| 110 | static inline int ocfs2_match(int len, |
| 111 | const char * const name, |
| 112 | struct ocfs2_dir_entry *de) |
| 113 | { |
| 114 | if (len != de->name_len) |
| 115 | return 0; |
| 116 | if (!de->inode) |
| 117 | return 0; |
| 118 | return !memcmp(name, de->name, len); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 123 | */ |
| 124 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, |
| 125 | struct inode *dir, |
| 126 | const char *name, int namelen, |
| 127 | unsigned long offset, |
| 128 | struct ocfs2_dir_entry **res_dir) |
| 129 | { |
| 130 | struct ocfs2_dir_entry *de; |
| 131 | char *dlimit, *de_buf; |
| 132 | int de_len; |
| 133 | int ret = 0; |
| 134 | |
| 135 | mlog_entry_void(); |
| 136 | |
| 137 | de_buf = bh->b_data; |
| 138 | dlimit = de_buf + dir->i_sb->s_blocksize; |
| 139 | |
| 140 | while (de_buf < dlimit) { |
| 141 | /* this code is executed quadratically often */ |
| 142 | /* do minimal checking `by hand' */ |
| 143 | |
| 144 | de = (struct ocfs2_dir_entry *) de_buf; |
| 145 | |
| 146 | if (de_buf + namelen <= dlimit && |
| 147 | ocfs2_match(namelen, name, de)) { |
| 148 | /* found a match - just to be sure, do a full check */ |
| 149 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 150 | ret = -1; |
| 151 | goto bail; |
| 152 | } |
| 153 | *res_dir = de; |
| 154 | ret = 1; |
| 155 | goto bail; |
| 156 | } |
| 157 | |
| 158 | /* prevent looping on a bad block */ |
| 159 | de_len = le16_to_cpu(de->rec_len); |
| 160 | if (de_len <= 0) { |
| 161 | ret = -1; |
| 162 | goto bail; |
| 163 | } |
| 164 | |
| 165 | de_buf += de_len; |
| 166 | offset += de_len; |
| 167 | } |
| 168 | |
| 169 | bail: |
| 170 | mlog_exit(ret); |
| 171 | return ret; |
| 172 | } |
| 173 | |
| 174 | struct buffer_head *ocfs2_find_entry(const char *name, int namelen, |
| 175 | struct inode *dir, |
| 176 | struct ocfs2_dir_entry **res_dir) |
| 177 | { |
| 178 | struct super_block *sb; |
| 179 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 180 | struct buffer_head *bh, *ret = NULL; |
| 181 | unsigned long start, block, b; |
| 182 | int ra_max = 0; /* Number of bh's in the readahead |
| 183 | buffer, bh_use[] */ |
| 184 | int ra_ptr = 0; /* Current index into readahead |
| 185 | buffer */ |
| 186 | int num = 0; |
| 187 | int nblocks, i, err; |
| 188 | |
| 189 | mlog_entry_void(); |
| 190 | |
| 191 | *res_dir = NULL; |
| 192 | sb = dir->i_sb; |
| 193 | |
| 194 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 195 | start = OCFS2_I(dir)->ip_dir_start_lookup; |
| 196 | if (start >= nblocks) |
| 197 | start = 0; |
| 198 | block = start; |
| 199 | |
| 200 | restart: |
| 201 | do { |
| 202 | /* |
| 203 | * We deal with the read-ahead logic here. |
| 204 | */ |
| 205 | if (ra_ptr >= ra_max) { |
| 206 | /* Refill the readahead buffer */ |
| 207 | ra_ptr = 0; |
| 208 | b = block; |
| 209 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 210 | /* |
| 211 | * Terminate if we reach the end of the |
| 212 | * directory and must wrap, or if our |
| 213 | * search has finished at this block. |
| 214 | */ |
| 215 | if (b >= nblocks || (num && block == start)) { |
| 216 | bh_use[ra_max] = NULL; |
| 217 | break; |
| 218 | } |
| 219 | num++; |
| 220 | |
| 221 | bh = ocfs2_bread(dir, b++, &err, 1); |
| 222 | bh_use[ra_max] = bh; |
| 223 | } |
| 224 | } |
| 225 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 226 | goto next; |
| 227 | wait_on_buffer(bh); |
| 228 | if (!buffer_uptodate(bh)) { |
| 229 | /* read error, skip block & hope for the best */ |
| 230 | ocfs2_error(dir->i_sb, "reading directory %llu, " |
| 231 | "offset %lu\n", |
| 232 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 233 | block); |
| 234 | brelse(bh); |
| 235 | goto next; |
| 236 | } |
| 237 | i = ocfs2_search_dirblock(bh, dir, name, namelen, |
| 238 | block << sb->s_blocksize_bits, |
| 239 | res_dir); |
| 240 | if (i == 1) { |
| 241 | OCFS2_I(dir)->ip_dir_start_lookup = block; |
| 242 | ret = bh; |
| 243 | goto cleanup_and_exit; |
| 244 | } else { |
| 245 | brelse(bh); |
| 246 | if (i < 0) |
| 247 | goto cleanup_and_exit; |
| 248 | } |
| 249 | next: |
| 250 | if (++block >= nblocks) |
| 251 | block = 0; |
| 252 | } while (block != start); |
| 253 | |
| 254 | /* |
| 255 | * If the directory has grown while we were searching, then |
| 256 | * search the last part of the directory before giving up. |
| 257 | */ |
| 258 | block = nblocks; |
| 259 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 260 | if (block < nblocks) { |
| 261 | start = 0; |
| 262 | goto restart; |
| 263 | } |
| 264 | |
| 265 | cleanup_and_exit: |
| 266 | /* Clean up the read-ahead blocks */ |
| 267 | for (; ra_ptr < ra_max; ra_ptr++) |
| 268 | brelse(bh_use[ra_ptr]); |
| 269 | |
| 270 | mlog_exit_ptr(ret); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | /* |
| 275 | * ocfs2_delete_entry deletes a directory entry by merging it with the |
| 276 | * previous entry |
| 277 | */ |
| 278 | int ocfs2_delete_entry(handle_t *handle, |
| 279 | struct inode *dir, |
| 280 | struct ocfs2_dir_entry *de_del, |
| 281 | struct buffer_head *bh) |
| 282 | { |
| 283 | struct ocfs2_dir_entry *de, *pde; |
| 284 | int i, status = -ENOENT; |
| 285 | |
| 286 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh); |
| 287 | |
| 288 | i = 0; |
| 289 | pde = NULL; |
| 290 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 291 | while (i < bh->b_size) { |
| 292 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { |
| 293 | status = -EIO; |
| 294 | mlog_errno(status); |
| 295 | goto bail; |
| 296 | } |
| 297 | if (de == de_del) { |
| 298 | status = ocfs2_journal_access(handle, dir, bh, |
| 299 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 300 | if (status < 0) { |
| 301 | status = -EIO; |
| 302 | mlog_errno(status); |
| 303 | goto bail; |
| 304 | } |
| 305 | if (pde) |
| 306 | pde->rec_len = |
| 307 | cpu_to_le16(le16_to_cpu(pde->rec_len) + |
| 308 | le16_to_cpu(de->rec_len)); |
| 309 | else |
| 310 | de->inode = 0; |
| 311 | dir->i_version++; |
| 312 | status = ocfs2_journal_dirty(handle, bh); |
| 313 | goto bail; |
| 314 | } |
| 315 | i += le16_to_cpu(de->rec_len); |
| 316 | pde = de; |
| 317 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); |
| 318 | } |
| 319 | bail: |
| 320 | mlog_exit(status); |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | /* we don't always have a dentry for what we want to add, so people |
| 325 | * like orphan dir can call this instead. |
| 326 | * |
| 327 | * If you pass me insert_bh, I'll skip the search of the other dir |
| 328 | * blocks and put the record in there. |
| 329 | */ |
| 330 | int __ocfs2_add_entry(handle_t *handle, |
| 331 | struct inode *dir, |
| 332 | const char *name, int namelen, |
| 333 | struct inode *inode, u64 blkno, |
| 334 | struct buffer_head *parent_fe_bh, |
| 335 | struct buffer_head *insert_bh) |
| 336 | { |
| 337 | unsigned long offset; |
| 338 | unsigned short rec_len; |
| 339 | struct ocfs2_dir_entry *de, *de1; |
| 340 | struct super_block *sb; |
| 341 | int retval, status; |
| 342 | |
| 343 | mlog_entry_void(); |
| 344 | |
| 345 | sb = dir->i_sb; |
| 346 | |
| 347 | if (!namelen) |
| 348 | return -EINVAL; |
| 349 | |
| 350 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 351 | offset = 0; |
| 352 | de = (struct ocfs2_dir_entry *) insert_bh->b_data; |
| 353 | while (1) { |
| 354 | BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data); |
| 355 | /* These checks should've already been passed by the |
| 356 | * prepare function, but I guess we can leave them |
| 357 | * here anyway. */ |
| 358 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { |
| 359 | retval = -ENOENT; |
| 360 | goto bail; |
| 361 | } |
| 362 | if (ocfs2_match(namelen, name, de)) { |
| 363 | retval = -EEXIST; |
| 364 | goto bail; |
| 365 | } |
| 366 | if (((le64_to_cpu(de->inode) == 0) && |
| 367 | (le16_to_cpu(de->rec_len) >= rec_len)) || |
| 368 | (le16_to_cpu(de->rec_len) >= |
| 369 | (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) { |
| 370 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 371 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 372 | if (retval < 0) { |
| 373 | mlog_errno(retval); |
| 374 | goto bail; |
| 375 | } |
| 376 | |
| 377 | status = ocfs2_journal_access(handle, dir, insert_bh, |
| 378 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 379 | /* By now the buffer is marked for journaling */ |
| 380 | offset += le16_to_cpu(de->rec_len); |
| 381 | if (le64_to_cpu(de->inode)) { |
| 382 | de1 = (struct ocfs2_dir_entry *)((char *) de + |
| 383 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 384 | de1->rec_len = |
| 385 | cpu_to_le16(le16_to_cpu(de->rec_len) - |
| 386 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 387 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 388 | de = de1; |
| 389 | } |
| 390 | de->file_type = OCFS2_FT_UNKNOWN; |
| 391 | if (blkno) { |
| 392 | de->inode = cpu_to_le64(blkno); |
| 393 | ocfs2_set_de_type(de, inode->i_mode); |
| 394 | } else |
| 395 | de->inode = 0; |
| 396 | de->name_len = namelen; |
| 397 | memcpy(de->name, name, namelen); |
| 398 | |
| 399 | dir->i_version++; |
| 400 | status = ocfs2_journal_dirty(handle, insert_bh); |
| 401 | retval = 0; |
| 402 | goto bail; |
| 403 | } |
| 404 | offset += le16_to_cpu(de->rec_len); |
| 405 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); |
| 406 | } |
| 407 | |
| 408 | /* when you think about it, the assert above should prevent us |
| 409 | * from ever getting here. */ |
| 410 | retval = -ENOSPC; |
| 411 | bail: |
| 412 | |
| 413 | mlog_exit(retval); |
| 414 | return retval; |
| 415 | } |
| 416 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 417 | static int ocfs2_dir_foreach_blk(struct inode *inode, unsigned long *f_version, |
| 418 | loff_t *f_pos, void *priv, filldir_t filldir) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 419 | { |
| 420 | int error = 0; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 421 | unsigned long offset, blk, last_ra_blk = 0; |
| 422 | int i, stored; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 423 | struct buffer_head * bh, * tmp; |
| 424 | struct ocfs2_dir_entry * de; |
| 425 | int err; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 426 | struct super_block * sb = inode->i_sb; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 427 | unsigned int ra_sectors = 16; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 428 | |
| 429 | stored = 0; |
| 430 | bh = NULL; |
| 431 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 432 | offset = (*f_pos) & (sb->s_blocksize - 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 433 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 434 | while (!error && !stored && *f_pos < i_size_read(inode)) { |
| 435 | blk = (*f_pos) >> sb->s_blocksize_bits; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 436 | bh = ocfs2_bread(inode, blk, &err, 0); |
| 437 | if (!bh) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 438 | mlog(ML_ERROR, |
| 439 | "directory #%llu contains a hole at offset %lld\n", |
| 440 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 441 | *f_pos); |
| 442 | *f_pos += sb->s_blocksize - offset; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 443 | continue; |
| 444 | } |
| 445 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 446 | /* The idea here is to begin with 8k read-ahead and to stay |
| 447 | * 4k ahead of our current position. |
| 448 | * |
| 449 | * TODO: Use the pagecache for this. We just need to |
| 450 | * make sure it's cluster-safe... */ |
| 451 | if (!last_ra_blk |
| 452 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { |
| 453 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 454 | i > 0; i--) { |
| 455 | tmp = ocfs2_bread(inode, ++blk, &err, 1); |
| 456 | if (tmp) |
| 457 | brelse(tmp); |
| 458 | } |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 459 | last_ra_blk = blk; |
| 460 | ra_sectors = 8; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | revalidate: |
| 464 | /* If the dir block has changed since the last call to |
| 465 | * readdir(2), then we might be pointing to an invalid |
| 466 | * dirent right now. Scan from the start of the block |
| 467 | * to make sure. */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 468 | if (*f_version != inode->i_version) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 469 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
| 470 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); |
| 471 | /* It's too expensive to do a full |
| 472 | * dirent test each time round this |
| 473 | * loop, but we do have to test at |
| 474 | * least that it is non-zero. A |
| 475 | * failure will be detected in the |
| 476 | * dirent test below. */ |
| 477 | if (le16_to_cpu(de->rec_len) < |
| 478 | OCFS2_DIR_REC_LEN(1)) |
| 479 | break; |
| 480 | i += le16_to_cpu(de->rec_len); |
| 481 | } |
| 482 | offset = i; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 483 | *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1)) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 484 | | offset; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 485 | *f_version = inode->i_version; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 488 | while (!error && *f_pos < i_size_read(inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 489 | && offset < sb->s_blocksize) { |
| 490 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); |
| 491 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 492 | /* On error, skip the f_pos to the |
| 493 | next block. */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 494 | *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 495 | brelse(bh); |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 496 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 497 | } |
| 498 | offset += le16_to_cpu(de->rec_len); |
| 499 | if (le64_to_cpu(de->inode)) { |
| 500 | /* We might block in the next section |
| 501 | * if the data destination is |
| 502 | * currently swapped out. So, use a |
| 503 | * version stamp to detect whether or |
| 504 | * not the directory has been modified |
| 505 | * during the copy operation. |
| 506 | */ |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 507 | unsigned long version = *f_version; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 508 | unsigned char d_type = DT_UNKNOWN; |
| 509 | |
| 510 | if (de->file_type < OCFS2_FT_MAX) |
| 511 | d_type = ocfs2_filetype_table[de->file_type]; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 512 | error = filldir(priv, de->name, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 513 | de->name_len, |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 514 | *f_pos, |
Mark Fasheh | 7e85367 | 2007-09-10 17:30:26 -0700 | [diff] [blame] | 515 | le64_to_cpu(de->inode), |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 516 | d_type); |
| 517 | if (error) |
| 518 | break; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 519 | if (version != *f_version) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 520 | goto revalidate; |
| 521 | stored ++; |
| 522 | } |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 523 | *f_pos += le16_to_cpu(de->rec_len); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 524 | } |
| 525 | offset = 0; |
| 526 | brelse(bh); |
| 527 | } |
| 528 | |
| 529 | stored = 0; |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 530 | out: |
| 531 | return stored; |
| 532 | } |
| 533 | |
| 534 | /* |
Mark Fasheh | 5eae5b9 | 2007-09-10 17:50:51 -0700 | [diff] [blame^] | 535 | * This is intended to be called from inside other kernel functions, |
| 536 | * so we fake some arguments. |
| 537 | */ |
| 538 | int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv, |
| 539 | filldir_t filldir) |
| 540 | { |
| 541 | int ret = 0; |
| 542 | unsigned long version = inode->i_version; |
| 543 | |
| 544 | while (*f_pos < i_size_read(inode)) { |
| 545 | ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv, |
| 546 | filldir); |
| 547 | if (ret) |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | /* |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 555 | * ocfs2_readdir() |
| 556 | * |
| 557 | */ |
| 558 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 559 | { |
| 560 | int error = 0; |
| 561 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 562 | int lock_level = 0; |
| 563 | |
| 564 | mlog_entry("dirino=%llu\n", |
| 565 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
| 566 | |
| 567 | error = ocfs2_meta_lock_atime(inode, filp->f_vfsmnt, &lock_level); |
| 568 | if (lock_level && error >= 0) { |
| 569 | /* We release EX lock which used to update atime |
| 570 | * and get PR lock again to reduce contention |
| 571 | * on commonly accessed directories. */ |
| 572 | ocfs2_meta_unlock(inode, 1); |
| 573 | lock_level = 0; |
| 574 | error = ocfs2_meta_lock(inode, NULL, 0); |
| 575 | } |
| 576 | if (error < 0) { |
| 577 | if (error != -ENOENT) |
| 578 | mlog_errno(error); |
| 579 | /* we haven't got any yet, so propagate the error. */ |
| 580 | goto bail_nolock; |
| 581 | } |
| 582 | |
| 583 | error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos, |
| 584 | dirent, filldir); |
| 585 | |
Tiger Yang | 25899de | 2006-11-15 15:49:02 +0800 | [diff] [blame] | 586 | ocfs2_meta_unlock(inode, lock_level); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 587 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 588 | bail_nolock: |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 589 | mlog_exit(error); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 590 | |
Mark Fasheh | b8bc5f4 | 2007-09-10 17:17:52 -0700 | [diff] [blame] | 591 | return error; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 595 | * NOTE: this should always be called with parent dir i_mutex taken. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 596 | */ |
| 597 | int ocfs2_find_files_on_disk(const char *name, |
| 598 | int namelen, |
| 599 | u64 *blkno, |
| 600 | struct inode *inode, |
| 601 | struct buffer_head **dirent_bh, |
| 602 | struct ocfs2_dir_entry **dirent) |
| 603 | { |
| 604 | int status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 605 | |
Joel Becker | 2b388c6 | 2006-05-10 18:28:59 -0700 | [diff] [blame] | 606 | mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", |
| 607 | namelen, name, blkno, inode, dirent_bh, dirent); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 608 | |
| 609 | *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); |
| 610 | if (!*dirent_bh || !*dirent) { |
| 611 | status = -ENOENT; |
| 612 | goto leave; |
| 613 | } |
| 614 | |
| 615 | *blkno = le64_to_cpu((*dirent)->inode); |
| 616 | |
| 617 | status = 0; |
| 618 | leave: |
| 619 | if (status < 0) { |
| 620 | *dirent = NULL; |
| 621 | if (*dirent_bh) { |
| 622 | brelse(*dirent_bh); |
| 623 | *dirent_bh = NULL; |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | mlog_exit(status); |
| 628 | return status; |
| 629 | } |
| 630 | |
| 631 | /* Check for a name within a directory. |
| 632 | * |
| 633 | * Return 0 if the name does not exist |
| 634 | * Return -EEXIST if the directory contains the name |
| 635 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 636 | * Callers should have i_mutex + a cluster lock on dir |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 637 | */ |
| 638 | int ocfs2_check_dir_for_entry(struct inode *dir, |
| 639 | const char *name, |
| 640 | int namelen) |
| 641 | { |
| 642 | int ret; |
| 643 | struct buffer_head *dirent_bh = NULL; |
| 644 | struct ocfs2_dir_entry *dirent = NULL; |
| 645 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 646 | mlog_entry("dir %llu, name '%.*s'\n", |
| 647 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 648 | |
| 649 | ret = -EEXIST; |
| 650 | dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); |
| 651 | if (dirent_bh) |
| 652 | goto bail; |
| 653 | |
| 654 | ret = 0; |
| 655 | bail: |
| 656 | if (dirent_bh) |
| 657 | brelse(dirent_bh); |
| 658 | |
| 659 | mlog_exit(ret); |
| 660 | return ret; |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | * routine to check that the specified directory is empty (for rmdir) |
| 665 | */ |
| 666 | int ocfs2_empty_dir(struct inode *inode) |
| 667 | { |
| 668 | unsigned long offset; |
| 669 | struct buffer_head * bh; |
| 670 | struct ocfs2_dir_entry * de, * de1; |
| 671 | struct super_block * sb; |
| 672 | int err; |
| 673 | |
| 674 | sb = inode->i_sb; |
| 675 | if ((i_size_read(inode) < |
| 676 | (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) || |
| 677 | !(bh = ocfs2_bread(inode, 0, &err, 0))) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 678 | mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n", |
| 679 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 680 | return 1; |
| 681 | } |
| 682 | |
| 683 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 684 | de1 = (struct ocfs2_dir_entry *) |
| 685 | ((char *)de + le16_to_cpu(de->rec_len)); |
| 686 | if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) || |
| 687 | !le64_to_cpu(de1->inode) || |
| 688 | strcmp(".", de->name) || |
| 689 | strcmp("..", de1->name)) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 690 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", |
| 691 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 692 | brelse(bh); |
| 693 | return 1; |
| 694 | } |
| 695 | offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len); |
| 696 | de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len)); |
| 697 | while (offset < i_size_read(inode) ) { |
| 698 | if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) { |
| 699 | brelse(bh); |
| 700 | bh = ocfs2_bread(inode, |
| 701 | offset >> sb->s_blocksize_bits, &err, 0); |
| 702 | if (!bh) { |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 703 | mlog(ML_ERROR, "dir %llu has a hole at %lu\n", |
| 704 | (unsigned long long)OCFS2_I(inode)->ip_blkno, offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 705 | offset += sb->s_blocksize; |
| 706 | continue; |
| 707 | } |
| 708 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 709 | } |
| 710 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 711 | brelse(bh); |
| 712 | return 1; |
| 713 | } |
| 714 | if (le64_to_cpu(de->inode)) { |
| 715 | brelse(bh); |
| 716 | return 0; |
| 717 | } |
| 718 | offset += le16_to_cpu(de->rec_len); |
| 719 | de = (struct ocfs2_dir_entry *) |
| 720 | ((char *)de + le16_to_cpu(de->rec_len)); |
| 721 | } |
| 722 | brelse(bh); |
| 723 | return 1; |
| 724 | } |
| 725 | |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 726 | int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
| 727 | handle_t *handle, |
| 728 | struct inode *parent, |
| 729 | struct inode *inode, |
| 730 | struct buffer_head *fe_bh, |
| 731 | struct ocfs2_alloc_context *data_ac) |
| 732 | { |
| 733 | int status; |
| 734 | struct buffer_head *new_bh = NULL; |
| 735 | struct ocfs2_dir_entry *de = NULL; |
| 736 | |
| 737 | mlog_entry_void(); |
| 738 | |
| 739 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, |
| 740 | data_ac, NULL, &new_bh); |
| 741 | if (status < 0) { |
| 742 | mlog_errno(status); |
| 743 | goto bail; |
| 744 | } |
| 745 | |
| 746 | ocfs2_set_new_buffer_uptodate(inode, new_bh); |
| 747 | |
| 748 | status = ocfs2_journal_access(handle, inode, new_bh, |
| 749 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 750 | if (status < 0) { |
| 751 | mlog_errno(status); |
| 752 | goto bail; |
| 753 | } |
| 754 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); |
| 755 | |
| 756 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 757 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); |
| 758 | de->name_len = 1; |
| 759 | de->rec_len = |
| 760 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 761 | strcpy(de->name, "."); |
| 762 | ocfs2_set_de_type(de, S_IFDIR); |
| 763 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); |
| 764 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); |
| 765 | de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize - |
| 766 | OCFS2_DIR_REC_LEN(1)); |
| 767 | de->name_len = 2; |
| 768 | strcpy(de->name, ".."); |
| 769 | ocfs2_set_de_type(de, S_IFDIR); |
| 770 | |
| 771 | status = ocfs2_journal_dirty(handle, new_bh); |
| 772 | if (status < 0) { |
| 773 | mlog_errno(status); |
| 774 | goto bail; |
| 775 | } |
| 776 | |
| 777 | i_size_write(inode, inode->i_sb->s_blocksize); |
| 778 | inode->i_nlink = 2; |
| 779 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
| 780 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 781 | if (status < 0) { |
| 782 | mlog_errno(status); |
| 783 | goto bail; |
| 784 | } |
| 785 | |
| 786 | status = 0; |
| 787 | bail: |
| 788 | if (new_bh) |
| 789 | brelse(new_bh); |
| 790 | |
| 791 | mlog_exit(status); |
| 792 | return status; |
| 793 | } |
| 794 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 795 | /* returns a bh of the 1st new block in the allocation. */ |
Mark Fasheh | 316f4b9 | 2007-09-07 18:21:26 -0700 | [diff] [blame] | 796 | static int ocfs2_do_extend_dir(struct super_block *sb, |
| 797 | handle_t *handle, |
| 798 | struct inode *dir, |
| 799 | struct buffer_head *parent_fe_bh, |
| 800 | struct ocfs2_alloc_context *data_ac, |
| 801 | struct ocfs2_alloc_context *meta_ac, |
| 802 | struct buffer_head **new_bh) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 803 | { |
| 804 | int status; |
| 805 | int extend; |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 806 | u64 p_blkno, v_blkno; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 807 | |
| 808 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 809 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); |
| 810 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 811 | |
| 812 | if (extend) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 813 | u32 offset = OCFS2_I(dir)->ip_clusters; |
| 814 | |
| 815 | status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, &offset, |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 816 | 1, 0, parent_fe_bh, handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 817 | data_ac, meta_ac, NULL); |
| 818 | BUG_ON(status == -EAGAIN); |
| 819 | if (status < 0) { |
| 820 | mlog_errno(status); |
| 821 | goto bail; |
| 822 | } |
| 823 | } |
| 824 | |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 825 | v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir)); |
| 826 | 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] | 827 | if (status < 0) { |
| 828 | mlog_errno(status); |
| 829 | goto bail; |
| 830 | } |
| 831 | |
| 832 | *new_bh = sb_getblk(sb, p_blkno); |
| 833 | if (!*new_bh) { |
| 834 | status = -EIO; |
| 835 | mlog_errno(status); |
| 836 | goto bail; |
| 837 | } |
| 838 | status = 0; |
| 839 | bail: |
| 840 | mlog_exit(status); |
| 841 | return status; |
| 842 | } |
| 843 | |
| 844 | /* assumes you already have a cluster lock on the directory. */ |
| 845 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 846 | struct inode *dir, |
| 847 | struct buffer_head *parent_fe_bh, |
| 848 | struct buffer_head **new_de_bh) |
| 849 | { |
| 850 | int status = 0; |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 851 | int credits, num_free_extents, drop_alloc_sem = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 852 | loff_t dir_i_size; |
| 853 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 854 | struct ocfs2_alloc_context *data_ac = NULL; |
| 855 | struct ocfs2_alloc_context *meta_ac = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 856 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 857 | struct buffer_head *new_bh = NULL; |
| 858 | struct ocfs2_dir_entry * de; |
| 859 | struct super_block *sb = osb->sb; |
| 860 | |
| 861 | mlog_entry_void(); |
| 862 | |
| 863 | dir_i_size = i_size_read(dir); |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 864 | mlog(0, "extending dir %llu (i_size = %lld)\n", |
| 865 | (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 866 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 867 | /* dir->i_size is always block aligned. */ |
| 868 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 869 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { |
| 870 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 871 | num_free_extents = ocfs2_num_free_extents(osb, dir, fe); |
| 872 | if (num_free_extents < 0) { |
| 873 | status = num_free_extents; |
| 874 | mlog_errno(status); |
| 875 | goto bail; |
| 876 | } |
| 877 | |
| 878 | if (!num_free_extents) { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 879 | status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 880 | if (status < 0) { |
| 881 | if (status != -ENOSPC) |
| 882 | mlog_errno(status); |
| 883 | goto bail; |
| 884 | } |
| 885 | } |
| 886 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 887 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 888 | if (status < 0) { |
| 889 | if (status != -ENOSPC) |
| 890 | mlog_errno(status); |
| 891 | goto bail; |
| 892 | } |
| 893 | |
| 894 | credits = ocfs2_calc_extend_credits(sb, fe, 1); |
| 895 | } else { |
| 896 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 897 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 898 | } |
| 899 | |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 900 | down_write(&OCFS2_I(dir)->ip_alloc_sem); |
| 901 | drop_alloc_sem = 1; |
| 902 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 903 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 904 | if (IS_ERR(handle)) { |
| 905 | status = PTR_ERR(handle); |
| 906 | handle = NULL; |
| 907 | mlog_errno(status); |
| 908 | goto bail; |
| 909 | } |
| 910 | |
| 911 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, |
| 912 | data_ac, meta_ac, &new_bh); |
| 913 | if (status < 0) { |
| 914 | mlog_errno(status); |
| 915 | goto bail; |
| 916 | } |
| 917 | |
| 918 | ocfs2_set_new_buffer_uptodate(dir, new_bh); |
| 919 | |
| 920 | status = ocfs2_journal_access(handle, dir, new_bh, |
| 921 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 922 | if (status < 0) { |
| 923 | mlog_errno(status); |
| 924 | goto bail; |
| 925 | } |
| 926 | memset(new_bh->b_data, 0, sb->s_blocksize); |
| 927 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 928 | de->inode = 0; |
| 929 | de->rec_len = cpu_to_le16(sb->s_blocksize); |
| 930 | status = ocfs2_journal_dirty(handle, new_bh); |
| 931 | if (status < 0) { |
| 932 | mlog_errno(status); |
| 933 | goto bail; |
| 934 | } |
| 935 | |
| 936 | dir_i_size += dir->i_sb->s_blocksize; |
| 937 | i_size_write(dir, dir_i_size); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 938 | dir->i_blocks = ocfs2_inode_sector_count(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 939 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 940 | if (status < 0) { |
| 941 | mlog_errno(status); |
| 942 | goto bail; |
| 943 | } |
| 944 | |
| 945 | *new_de_bh = new_bh; |
| 946 | get_bh(*new_de_bh); |
| 947 | bail: |
Joel Becker | ee19a77 | 2007-03-28 18:27:07 -0700 | [diff] [blame] | 948 | if (drop_alloc_sem) |
| 949 | up_write(&OCFS2_I(dir)->ip_alloc_sem); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 950 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 951 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 952 | |
| 953 | if (data_ac) |
| 954 | ocfs2_free_alloc_context(data_ac); |
| 955 | if (meta_ac) |
| 956 | ocfs2_free_alloc_context(meta_ac); |
| 957 | |
| 958 | if (new_bh) |
| 959 | brelse(new_bh); |
| 960 | |
| 961 | mlog_exit(status); |
| 962 | return status; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Search the dir for a good spot, extending it if necessary. The |
| 967 | * block containing an appropriate record is returned in ret_de_bh. |
| 968 | */ |
| 969 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, |
| 970 | struct inode *dir, |
| 971 | struct buffer_head *parent_fe_bh, |
| 972 | const char *name, |
| 973 | int namelen, |
| 974 | struct buffer_head **ret_de_bh) |
| 975 | { |
| 976 | unsigned long offset; |
| 977 | struct buffer_head * bh = NULL; |
| 978 | unsigned short rec_len; |
| 979 | struct ocfs2_dinode *fe; |
| 980 | struct ocfs2_dir_entry *de; |
| 981 | struct super_block *sb; |
| 982 | int status; |
| 983 | |
| 984 | mlog_entry_void(); |
| 985 | |
Mark Fasheh | b0697053 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 986 | mlog(0, "getting ready to insert namelen %d into dir %llu\n", |
| 987 | namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 988 | |
| 989 | BUG_ON(!S_ISDIR(dir->i_mode)); |
| 990 | fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 991 | BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir)); |
| 992 | |
| 993 | sb = dir->i_sb; |
| 994 | |
| 995 | if (!namelen) { |
| 996 | status = -EINVAL; |
| 997 | mlog_errno(status); |
| 998 | goto bail; |
| 999 | } |
| 1000 | |
| 1001 | bh = ocfs2_bread(dir, 0, &status, 0); |
| 1002 | if (!bh) { |
| 1003 | mlog_errno(status); |
| 1004 | goto bail; |
| 1005 | } |
| 1006 | |
| 1007 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 1008 | offset = 0; |
| 1009 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 1010 | while (1) { |
| 1011 | if ((char *)de >= sb->s_blocksize + bh->b_data) { |
| 1012 | brelse(bh); |
| 1013 | bh = NULL; |
| 1014 | |
| 1015 | if (i_size_read(dir) <= offset) { |
| 1016 | status = ocfs2_extend_dir(osb, |
| 1017 | dir, |
| 1018 | parent_fe_bh, |
| 1019 | &bh); |
| 1020 | if (status < 0) { |
| 1021 | mlog_errno(status); |
| 1022 | goto bail; |
| 1023 | } |
| 1024 | BUG_ON(!bh); |
| 1025 | *ret_de_bh = bh; |
| 1026 | get_bh(*ret_de_bh); |
| 1027 | goto bail; |
| 1028 | } |
| 1029 | bh = ocfs2_bread(dir, |
| 1030 | offset >> sb->s_blocksize_bits, |
| 1031 | &status, |
| 1032 | 0); |
| 1033 | if (!bh) { |
| 1034 | mlog_errno(status); |
| 1035 | goto bail; |
| 1036 | } |
| 1037 | /* move to next block */ |
| 1038 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 1039 | } |
| 1040 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 1041 | status = -ENOENT; |
| 1042 | goto bail; |
| 1043 | } |
| 1044 | if (ocfs2_match(namelen, name, de)) { |
| 1045 | status = -EEXIST; |
| 1046 | goto bail; |
| 1047 | } |
| 1048 | if (((le64_to_cpu(de->inode) == 0) && |
| 1049 | (le16_to_cpu(de->rec_len) >= rec_len)) || |
| 1050 | (le16_to_cpu(de->rec_len) >= |
| 1051 | (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) { |
| 1052 | /* Ok, we found a spot. Return this bh and let |
| 1053 | * the caller actually fill it in. */ |
| 1054 | *ret_de_bh = bh; |
| 1055 | get_bh(*ret_de_bh); |
| 1056 | status = 0; |
| 1057 | goto bail; |
| 1058 | } |
| 1059 | offset += le16_to_cpu(de->rec_len); |
| 1060 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); |
| 1061 | } |
| 1062 | |
| 1063 | status = 0; |
| 1064 | bail: |
| 1065 | if (bh) |
| 1066 | brelse(bh); |
| 1067 | |
| 1068 | mlog_exit(status); |
| 1069 | return status; |
| 1070 | } |