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