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" |
| 58 | #include "uptodate.h" |
| 59 | |
| 60 | #include "buffer_head_io.h" |
| 61 | |
| 62 | static unsigned char ocfs2_filetype_table[] = { |
| 63 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 64 | }; |
| 65 | |
| 66 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 67 | struct inode *dir, |
| 68 | struct buffer_head *parent_fe_bh, |
| 69 | struct buffer_head **new_de_bh); |
| 70 | /* |
| 71 | * ocfs2_readdir() |
| 72 | * |
| 73 | */ |
| 74 | int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 75 | { |
| 76 | int error = 0; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 77 | unsigned long offset, blk, last_ra_blk = 0; |
| 78 | int i, stored; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 79 | struct buffer_head * bh, * tmp; |
| 80 | struct ocfs2_dir_entry * de; |
| 81 | int err; |
| 82 | struct inode *inode = filp->f_dentry->d_inode; |
| 83 | struct super_block * sb = inode->i_sb; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 84 | unsigned int ra_sectors = 16; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 85 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 86 | mlog_entry("dirino=%llu\n", |
| 87 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 88 | |
| 89 | stored = 0; |
| 90 | bh = NULL; |
| 91 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 92 | error = ocfs2_meta_lock(inode, NULL, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 93 | if (error < 0) { |
| 94 | if (error != -ENOENT) |
| 95 | mlog_errno(error); |
| 96 | /* we haven't got any yet, so propagate the error. */ |
| 97 | stored = error; |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 98 | goto bail_nolock; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 99 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 100 | |
| 101 | offset = filp->f_pos & (sb->s_blocksize - 1); |
| 102 | |
| 103 | while (!error && !stored && filp->f_pos < i_size_read(inode)) { |
| 104 | blk = (filp->f_pos) >> sb->s_blocksize_bits; |
| 105 | bh = ocfs2_bread(inode, blk, &err, 0); |
| 106 | if (!bh) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 107 | mlog(ML_ERROR, |
| 108 | "directory #%llu contains a hole at offset %lld\n", |
| 109 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 110 | filp->f_pos); |
| 111 | filp->f_pos += sb->s_blocksize - offset; |
| 112 | continue; |
| 113 | } |
| 114 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 115 | /* The idea here is to begin with 8k read-ahead and to stay |
| 116 | * 4k ahead of our current position. |
| 117 | * |
| 118 | * TODO: Use the pagecache for this. We just need to |
| 119 | * make sure it's cluster-safe... */ |
| 120 | if (!last_ra_blk |
| 121 | || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) { |
| 122 | for (i = ra_sectors >> (sb->s_blocksize_bits - 9); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 123 | i > 0; i--) { |
| 124 | tmp = ocfs2_bread(inode, ++blk, &err, 1); |
| 125 | if (tmp) |
| 126 | brelse(tmp); |
| 127 | } |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 128 | last_ra_blk = blk; |
| 129 | ra_sectors = 8; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | revalidate: |
| 133 | /* If the dir block has changed since the last call to |
| 134 | * readdir(2), then we might be pointing to an invalid |
| 135 | * dirent right now. Scan from the start of the block |
| 136 | * to make sure. */ |
| 137 | if (filp->f_version != inode->i_version) { |
| 138 | for (i = 0; i < sb->s_blocksize && i < offset; ) { |
| 139 | de = (struct ocfs2_dir_entry *) (bh->b_data + i); |
| 140 | /* It's too expensive to do a full |
| 141 | * dirent test each time round this |
| 142 | * loop, but we do have to test at |
| 143 | * least that it is non-zero. A |
| 144 | * failure will be detected in the |
| 145 | * dirent test below. */ |
| 146 | if (le16_to_cpu(de->rec_len) < |
| 147 | OCFS2_DIR_REC_LEN(1)) |
| 148 | break; |
| 149 | i += le16_to_cpu(de->rec_len); |
| 150 | } |
| 151 | offset = i; |
| 152 | filp->f_pos = (filp->f_pos & ~(sb->s_blocksize - 1)) |
| 153 | | offset; |
| 154 | filp->f_version = inode->i_version; |
| 155 | } |
| 156 | |
| 157 | while (!error && filp->f_pos < i_size_read(inode) |
| 158 | && offset < sb->s_blocksize) { |
| 159 | de = (struct ocfs2_dir_entry *) (bh->b_data + offset); |
| 160 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 161 | /* On error, skip the f_pos to the |
| 162 | next block. */ |
| 163 | filp->f_pos = (filp->f_pos | |
| 164 | (sb->s_blocksize - 1)) + 1; |
| 165 | brelse(bh); |
| 166 | goto bail; |
| 167 | } |
| 168 | offset += le16_to_cpu(de->rec_len); |
| 169 | if (le64_to_cpu(de->inode)) { |
| 170 | /* We might block in the next section |
| 171 | * if the data destination is |
| 172 | * currently swapped out. So, use a |
| 173 | * version stamp to detect whether or |
| 174 | * not the directory has been modified |
| 175 | * during the copy operation. |
| 176 | */ |
| 177 | unsigned long version = filp->f_version; |
| 178 | unsigned char d_type = DT_UNKNOWN; |
| 179 | |
| 180 | if (de->file_type < OCFS2_FT_MAX) |
| 181 | d_type = ocfs2_filetype_table[de->file_type]; |
| 182 | error = filldir(dirent, de->name, |
| 183 | de->name_len, |
| 184 | filp->f_pos, |
| 185 | ino_from_blkno(sb, le64_to_cpu(de->inode)), |
| 186 | d_type); |
| 187 | if (error) |
| 188 | break; |
| 189 | if (version != filp->f_version) |
| 190 | goto revalidate; |
| 191 | stored ++; |
| 192 | } |
| 193 | filp->f_pos += le16_to_cpu(de->rec_len); |
| 194 | } |
| 195 | offset = 0; |
| 196 | brelse(bh); |
| 197 | } |
| 198 | |
| 199 | stored = 0; |
| 200 | bail: |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 201 | ocfs2_meta_unlock(inode, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 202 | |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 203 | bail_nolock: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 204 | mlog_exit(stored); |
| 205 | |
| 206 | return stored; |
| 207 | } |
| 208 | |
| 209 | /* |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 210 | * NOTE: this should always be called with parent dir i_mutex taken. |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 211 | */ |
| 212 | int ocfs2_find_files_on_disk(const char *name, |
| 213 | int namelen, |
| 214 | u64 *blkno, |
| 215 | struct inode *inode, |
| 216 | struct buffer_head **dirent_bh, |
| 217 | struct ocfs2_dir_entry **dirent) |
| 218 | { |
| 219 | int status = -ENOENT; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 220 | |
Joel Becker | 2b388c6 | 2006-05-10 18:28:59 -0700 | [diff] [blame] | 221 | mlog_entry("(name=%.*s, blkno=%p, inode=%p, dirent_bh=%p, dirent=%p)\n", |
| 222 | namelen, name, blkno, inode, dirent_bh, dirent); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 223 | |
| 224 | *dirent_bh = ocfs2_find_entry(name, namelen, inode, dirent); |
| 225 | if (!*dirent_bh || !*dirent) { |
| 226 | status = -ENOENT; |
| 227 | goto leave; |
| 228 | } |
| 229 | |
| 230 | *blkno = le64_to_cpu((*dirent)->inode); |
| 231 | |
| 232 | status = 0; |
| 233 | leave: |
| 234 | if (status < 0) { |
| 235 | *dirent = NULL; |
| 236 | if (*dirent_bh) { |
| 237 | brelse(*dirent_bh); |
| 238 | *dirent_bh = NULL; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | mlog_exit(status); |
| 243 | return status; |
| 244 | } |
| 245 | |
| 246 | /* Check for a name within a directory. |
| 247 | * |
| 248 | * Return 0 if the name does not exist |
| 249 | * Return -EEXIST if the directory contains the name |
| 250 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 251 | * Callers should have i_mutex + a cluster lock on dir |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 252 | */ |
| 253 | int ocfs2_check_dir_for_entry(struct inode *dir, |
| 254 | const char *name, |
| 255 | int namelen) |
| 256 | { |
| 257 | int ret; |
| 258 | struct buffer_head *dirent_bh = NULL; |
| 259 | struct ocfs2_dir_entry *dirent = NULL; |
| 260 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 261 | mlog_entry("dir %llu, name '%.*s'\n", |
| 262 | (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 263 | |
| 264 | ret = -EEXIST; |
| 265 | dirent_bh = ocfs2_find_entry(name, namelen, dir, &dirent); |
| 266 | if (dirent_bh) |
| 267 | goto bail; |
| 268 | |
| 269 | ret = 0; |
| 270 | bail: |
| 271 | if (dirent_bh) |
| 272 | brelse(dirent_bh); |
| 273 | |
| 274 | mlog_exit(ret); |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * routine to check that the specified directory is empty (for rmdir) |
| 280 | */ |
| 281 | int ocfs2_empty_dir(struct inode *inode) |
| 282 | { |
| 283 | unsigned long offset; |
| 284 | struct buffer_head * bh; |
| 285 | struct ocfs2_dir_entry * de, * de1; |
| 286 | struct super_block * sb; |
| 287 | int err; |
| 288 | |
| 289 | sb = inode->i_sb; |
| 290 | if ((i_size_read(inode) < |
| 291 | (OCFS2_DIR_REC_LEN(1) + OCFS2_DIR_REC_LEN(2))) || |
| 292 | !(bh = ocfs2_bread(inode, 0, &err, 0))) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 293 | mlog(ML_ERROR, "bad directory (dir #%llu) - no data block\n", |
| 294 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 299 | de1 = (struct ocfs2_dir_entry *) |
| 300 | ((char *)de + le16_to_cpu(de->rec_len)); |
| 301 | if ((le64_to_cpu(de->inode) != OCFS2_I(inode)->ip_blkno) || |
| 302 | !le64_to_cpu(de1->inode) || |
| 303 | strcmp(".", de->name) || |
| 304 | strcmp("..", de1->name)) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 305 | mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n", |
| 306 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 307 | brelse(bh); |
| 308 | return 1; |
| 309 | } |
| 310 | offset = le16_to_cpu(de->rec_len) + le16_to_cpu(de1->rec_len); |
| 311 | de = (struct ocfs2_dir_entry *)((char *)de1 + le16_to_cpu(de1->rec_len)); |
| 312 | while (offset < i_size_read(inode) ) { |
| 313 | if (!bh || (void *)de >= (void *)(bh->b_data + sb->s_blocksize)) { |
| 314 | brelse(bh); |
| 315 | bh = ocfs2_bread(inode, |
| 316 | offset >> sb->s_blocksize_bits, &err, 0); |
| 317 | if (!bh) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 318 | mlog(ML_ERROR, "dir %llu has a hole at %lu\n", |
| 319 | (unsigned long long)OCFS2_I(inode)->ip_blkno, offset); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 320 | offset += sb->s_blocksize; |
| 321 | continue; |
| 322 | } |
| 323 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 324 | } |
| 325 | if (!ocfs2_check_dir_entry(inode, de, bh, offset)) { |
| 326 | brelse(bh); |
| 327 | return 1; |
| 328 | } |
| 329 | if (le64_to_cpu(de->inode)) { |
| 330 | brelse(bh); |
| 331 | return 0; |
| 332 | } |
| 333 | offset += le16_to_cpu(de->rec_len); |
| 334 | de = (struct ocfs2_dir_entry *) |
| 335 | ((char *)de + le16_to_cpu(de->rec_len)); |
| 336 | } |
| 337 | brelse(bh); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | /* returns a bh of the 1st new block in the allocation. */ |
| 342 | int ocfs2_do_extend_dir(struct super_block *sb, |
| 343 | struct ocfs2_journal_handle *handle, |
| 344 | struct inode *dir, |
| 345 | struct buffer_head *parent_fe_bh, |
| 346 | struct ocfs2_alloc_context *data_ac, |
| 347 | struct ocfs2_alloc_context *meta_ac, |
| 348 | struct buffer_head **new_bh) |
| 349 | { |
| 350 | int status; |
| 351 | int extend; |
| 352 | u64 p_blkno; |
| 353 | |
| 354 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 355 | extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)); |
| 356 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 357 | |
| 358 | if (extend) { |
| 359 | status = ocfs2_do_extend_allocation(OCFS2_SB(sb), dir, 1, |
| 360 | parent_fe_bh, handle, |
| 361 | data_ac, meta_ac, NULL); |
| 362 | BUG_ON(status == -EAGAIN); |
| 363 | if (status < 0) { |
| 364 | mlog_errno(status); |
| 365 | goto bail; |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | status = ocfs2_extent_map_get_blocks(dir, (dir->i_blocks >> |
| 370 | (sb->s_blocksize_bits - 9)), |
| 371 | 1, &p_blkno, NULL); |
| 372 | if (status < 0) { |
| 373 | mlog_errno(status); |
| 374 | goto bail; |
| 375 | } |
| 376 | |
| 377 | *new_bh = sb_getblk(sb, p_blkno); |
| 378 | if (!*new_bh) { |
| 379 | status = -EIO; |
| 380 | mlog_errno(status); |
| 381 | goto bail; |
| 382 | } |
| 383 | status = 0; |
| 384 | bail: |
| 385 | mlog_exit(status); |
| 386 | return status; |
| 387 | } |
| 388 | |
| 389 | /* assumes you already have a cluster lock on the directory. */ |
| 390 | static int ocfs2_extend_dir(struct ocfs2_super *osb, |
| 391 | struct inode *dir, |
| 392 | struct buffer_head *parent_fe_bh, |
| 393 | struct buffer_head **new_de_bh) |
| 394 | { |
| 395 | int status = 0; |
| 396 | int credits, num_free_extents; |
| 397 | loff_t dir_i_size; |
| 398 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 399 | struct ocfs2_alloc_context *data_ac = NULL; |
| 400 | struct ocfs2_alloc_context *meta_ac = NULL; |
| 401 | struct ocfs2_journal_handle *handle = NULL; |
| 402 | struct buffer_head *new_bh = NULL; |
| 403 | struct ocfs2_dir_entry * de; |
| 404 | struct super_block *sb = osb->sb; |
| 405 | |
| 406 | mlog_entry_void(); |
| 407 | |
| 408 | dir_i_size = i_size_read(dir); |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 409 | mlog(0, "extending dir %llu (i_size = %lld)\n", |
| 410 | (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 411 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 412 | /* dir->i_size is always block aligned. */ |
| 413 | spin_lock(&OCFS2_I(dir)->ip_lock); |
| 414 | if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) { |
| 415 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 416 | num_free_extents = ocfs2_num_free_extents(osb, dir, fe); |
| 417 | if (num_free_extents < 0) { |
| 418 | status = num_free_extents; |
| 419 | mlog_errno(status); |
| 420 | goto bail; |
| 421 | } |
| 422 | |
| 423 | if (!num_free_extents) { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 424 | status = ocfs2_reserve_new_metadata(osb, fe, &meta_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 425 | if (status < 0) { |
| 426 | if (status != -ENOSPC) |
| 427 | mlog_errno(status); |
| 428 | goto bail; |
| 429 | } |
| 430 | } |
| 431 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 432 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 433 | if (status < 0) { |
| 434 | if (status != -ENOSPC) |
| 435 | mlog_errno(status); |
| 436 | goto bail; |
| 437 | } |
| 438 | |
| 439 | credits = ocfs2_calc_extend_credits(sb, fe, 1); |
| 440 | } else { |
| 441 | spin_unlock(&OCFS2_I(dir)->ip_lock); |
| 442 | credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS; |
| 443 | } |
| 444 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame^] | 445 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 446 | if (IS_ERR(handle)) { |
| 447 | status = PTR_ERR(handle); |
| 448 | handle = NULL; |
| 449 | mlog_errno(status); |
| 450 | goto bail; |
| 451 | } |
| 452 | |
| 453 | status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh, |
| 454 | data_ac, meta_ac, &new_bh); |
| 455 | if (status < 0) { |
| 456 | mlog_errno(status); |
| 457 | goto bail; |
| 458 | } |
| 459 | |
| 460 | ocfs2_set_new_buffer_uptodate(dir, new_bh); |
| 461 | |
| 462 | status = ocfs2_journal_access(handle, dir, new_bh, |
| 463 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 464 | if (status < 0) { |
| 465 | mlog_errno(status); |
| 466 | goto bail; |
| 467 | } |
| 468 | memset(new_bh->b_data, 0, sb->s_blocksize); |
| 469 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 470 | de->inode = 0; |
| 471 | de->rec_len = cpu_to_le16(sb->s_blocksize); |
| 472 | status = ocfs2_journal_dirty(handle, new_bh); |
| 473 | if (status < 0) { |
| 474 | mlog_errno(status); |
| 475 | goto bail; |
| 476 | } |
| 477 | |
| 478 | dir_i_size += dir->i_sb->s_blocksize; |
| 479 | i_size_write(dir, dir_i_size); |
| 480 | dir->i_blocks = ocfs2_align_bytes_to_sectors(dir_i_size); |
| 481 | status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 482 | if (status < 0) { |
| 483 | mlog_errno(status); |
| 484 | goto bail; |
| 485 | } |
| 486 | |
| 487 | *new_de_bh = new_bh; |
| 488 | get_bh(*new_de_bh); |
| 489 | bail: |
| 490 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 491 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 492 | |
| 493 | if (data_ac) |
| 494 | ocfs2_free_alloc_context(data_ac); |
| 495 | if (meta_ac) |
| 496 | ocfs2_free_alloc_context(meta_ac); |
| 497 | |
| 498 | if (new_bh) |
| 499 | brelse(new_bh); |
| 500 | |
| 501 | mlog_exit(status); |
| 502 | return status; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Search the dir for a good spot, extending it if necessary. The |
| 507 | * block containing an appropriate record is returned in ret_de_bh. |
| 508 | */ |
| 509 | int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb, |
| 510 | struct inode *dir, |
| 511 | struct buffer_head *parent_fe_bh, |
| 512 | const char *name, |
| 513 | int namelen, |
| 514 | struct buffer_head **ret_de_bh) |
| 515 | { |
| 516 | unsigned long offset; |
| 517 | struct buffer_head * bh = NULL; |
| 518 | unsigned short rec_len; |
| 519 | struct ocfs2_dinode *fe; |
| 520 | struct ocfs2_dir_entry *de; |
| 521 | struct super_block *sb; |
| 522 | int status; |
| 523 | |
| 524 | mlog_entry_void(); |
| 525 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 526 | mlog(0, "getting ready to insert namelen %d into dir %llu\n", |
| 527 | namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 528 | |
| 529 | BUG_ON(!S_ISDIR(dir->i_mode)); |
| 530 | fe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 531 | BUG_ON(le64_to_cpu(fe->i_size) != i_size_read(dir)); |
| 532 | |
| 533 | sb = dir->i_sb; |
| 534 | |
| 535 | if (!namelen) { |
| 536 | status = -EINVAL; |
| 537 | mlog_errno(status); |
| 538 | goto bail; |
| 539 | } |
| 540 | |
| 541 | bh = ocfs2_bread(dir, 0, &status, 0); |
| 542 | if (!bh) { |
| 543 | mlog_errno(status); |
| 544 | goto bail; |
| 545 | } |
| 546 | |
| 547 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 548 | offset = 0; |
| 549 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 550 | while (1) { |
| 551 | if ((char *)de >= sb->s_blocksize + bh->b_data) { |
| 552 | brelse(bh); |
| 553 | bh = NULL; |
| 554 | |
| 555 | if (i_size_read(dir) <= offset) { |
| 556 | status = ocfs2_extend_dir(osb, |
| 557 | dir, |
| 558 | parent_fe_bh, |
| 559 | &bh); |
| 560 | if (status < 0) { |
| 561 | mlog_errno(status); |
| 562 | goto bail; |
| 563 | } |
| 564 | BUG_ON(!bh); |
| 565 | *ret_de_bh = bh; |
| 566 | get_bh(*ret_de_bh); |
| 567 | goto bail; |
| 568 | } |
| 569 | bh = ocfs2_bread(dir, |
| 570 | offset >> sb->s_blocksize_bits, |
| 571 | &status, |
| 572 | 0); |
| 573 | if (!bh) { |
| 574 | mlog_errno(status); |
| 575 | goto bail; |
| 576 | } |
| 577 | /* move to next block */ |
| 578 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 579 | } |
| 580 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 581 | status = -ENOENT; |
| 582 | goto bail; |
| 583 | } |
| 584 | if (ocfs2_match(namelen, name, de)) { |
| 585 | status = -EEXIST; |
| 586 | goto bail; |
| 587 | } |
| 588 | if (((le64_to_cpu(de->inode) == 0) && |
| 589 | (le16_to_cpu(de->rec_len) >= rec_len)) || |
| 590 | (le16_to_cpu(de->rec_len) >= |
| 591 | (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) { |
| 592 | /* Ok, we found a spot. Return this bh and let |
| 593 | * the caller actually fill it in. */ |
| 594 | *ret_de_bh = bh; |
| 595 | get_bh(*ret_de_bh); |
| 596 | status = 0; |
| 597 | goto bail; |
| 598 | } |
| 599 | offset += le16_to_cpu(de->rec_len); |
| 600 | de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len)); |
| 601 | } |
| 602 | |
| 603 | status = 0; |
| 604 | bail: |
| 605 | if (bh) |
| 606 | brelse(bh); |
| 607 | |
| 608 | mlog_exit(status); |
| 609 | return status; |
| 610 | } |