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 | * namei.c |
| 5 | * |
| 6 | * Create and rename file, directory, symlinks |
| 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 "dcache.h" |
| 51 | #include "dir.h" |
| 52 | #include "dlmglue.h" |
| 53 | #include "extent_map.h" |
| 54 | #include "file.h" |
| 55 | #include "inode.h" |
| 56 | #include "journal.h" |
| 57 | #include "namei.h" |
| 58 | #include "suballoc.h" |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 59 | #include "super.h" |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 60 | #include "symlink.h" |
| 61 | #include "sysfile.h" |
| 62 | #include "uptodate.h" |
| 63 | #include "vote.h" |
| 64 | |
| 65 | #include "buffer_head_io.h" |
| 66 | |
| 67 | #define NAMEI_RA_CHUNKS 2 |
| 68 | #define NAMEI_RA_BLOCKS 4 |
| 69 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
| 70 | #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) |
| 71 | |
| 72 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, |
| 73 | struct inode *dir, |
| 74 | const char *name, int namelen, |
| 75 | unsigned long offset, |
| 76 | struct ocfs2_dir_entry **res_dir); |
| 77 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 78 | static int ocfs2_delete_entry(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 79 | struct inode *dir, |
| 80 | struct ocfs2_dir_entry *de_del, |
| 81 | struct buffer_head *bh); |
| 82 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 83 | static int __ocfs2_add_entry(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 84 | struct inode *dir, |
| 85 | const char *name, int namelen, |
| 86 | struct inode *inode, u64 blkno, |
| 87 | struct buffer_head *parent_fe_bh, |
| 88 | struct buffer_head *insert_bh); |
| 89 | |
| 90 | static int ocfs2_mknod_locked(struct ocfs2_super *osb, |
| 91 | struct inode *dir, |
| 92 | struct dentry *dentry, int mode, |
| 93 | dev_t dev, |
| 94 | struct buffer_head **new_fe_bh, |
| 95 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 96 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 97 | struct inode **ret_inode, |
| 98 | struct ocfs2_alloc_context *inode_ac); |
| 99 | |
| 100 | static int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 101 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 102 | struct inode *parent, |
| 103 | struct inode *inode, |
| 104 | struct buffer_head *fe_bh, |
| 105 | struct ocfs2_alloc_context *data_ac); |
| 106 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 107 | static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 108 | struct inode **ret_orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 109 | struct inode *inode, |
| 110 | char *name, |
| 111 | struct buffer_head **de_bh); |
| 112 | |
| 113 | static int ocfs2_orphan_add(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 114 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 115 | struct inode *inode, |
| 116 | struct ocfs2_dinode *fe, |
| 117 | char *name, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 118 | struct buffer_head *de_bh, |
| 119 | struct inode *orphan_dir_inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 120 | |
| 121 | static int ocfs2_create_symlink_data(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 122 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 123 | struct inode *inode, |
| 124 | const char *symname); |
| 125 | |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 126 | static inline int ocfs2_add_entry(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 127 | struct dentry *dentry, |
| 128 | struct inode *inode, u64 blkno, |
| 129 | struct buffer_head *parent_fe_bh, |
| 130 | struct buffer_head *insert_bh) |
| 131 | { |
| 132 | return __ocfs2_add_entry(handle, dentry->d_parent->d_inode, |
| 133 | dentry->d_name.name, dentry->d_name.len, |
| 134 | inode, blkno, parent_fe_bh, insert_bh); |
| 135 | } |
| 136 | |
| 137 | /* An orphan dir name is an 8 byte value, printed as a hex string */ |
| 138 | #define OCFS2_ORPHAN_NAMELEN ((int)(2 * sizeof(u64))) |
| 139 | |
| 140 | static struct dentry *ocfs2_lookup(struct inode *dir, struct dentry *dentry, |
| 141 | struct nameidata *nd) |
| 142 | { |
| 143 | int status; |
| 144 | u64 blkno; |
| 145 | struct buffer_head *dirent_bh = NULL; |
| 146 | struct inode *inode = NULL; |
| 147 | struct dentry *ret; |
| 148 | struct ocfs2_dir_entry *dirent; |
| 149 | struct ocfs2_inode_info *oi; |
| 150 | |
| 151 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry, |
| 152 | dentry->d_name.len, dentry->d_name.name); |
| 153 | |
| 154 | if (dentry->d_name.len > OCFS2_MAX_FILENAME_LEN) { |
| 155 | ret = ERR_PTR(-ENAMETOOLONG); |
| 156 | goto bail; |
| 157 | } |
| 158 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 159 | mlog(0, "find name %.*s in directory %llu\n", dentry->d_name.len, |
| 160 | dentry->d_name.name, (unsigned long long)OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 161 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 162 | status = ocfs2_meta_lock(dir, NULL, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 163 | if (status < 0) { |
| 164 | if (status != -ENOENT) |
| 165 | mlog_errno(status); |
| 166 | ret = ERR_PTR(status); |
| 167 | goto bail; |
| 168 | } |
| 169 | |
| 170 | status = ocfs2_find_files_on_disk(dentry->d_name.name, |
| 171 | dentry->d_name.len, &blkno, |
| 172 | dir, &dirent_bh, &dirent); |
| 173 | if (status < 0) |
| 174 | goto bail_add; |
| 175 | |
Mark Fasheh | 24c19ef | 2006-09-22 17:28:19 -0700 | [diff] [blame] | 176 | inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 177 | if (IS_ERR(inode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 178 | ret = ERR_PTR(-EACCES); |
| 179 | goto bail_unlock; |
| 180 | } |
| 181 | |
| 182 | oi = OCFS2_I(inode); |
| 183 | /* Clear any orphaned state... If we were able to look up the |
| 184 | * inode from a directory, it certainly can't be orphaned. We |
| 185 | * might have the bad state from a node which intended to |
| 186 | * orphan this inode but crashed before it could commit the |
| 187 | * unlink. */ |
| 188 | spin_lock(&oi->ip_lock); |
| 189 | oi->ip_flags &= ~OCFS2_INODE_MAYBE_ORPHANED; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 190 | spin_unlock(&oi->ip_lock); |
| 191 | |
| 192 | bail_add: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 193 | dentry->d_op = &ocfs2_dentry_ops; |
| 194 | ret = d_splice_alias(inode, dentry); |
| 195 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 196 | if (inode) { |
| 197 | /* |
| 198 | * If d_splice_alias() finds a DCACHE_DISCONNECTED |
| 199 | * dentry, it will d_move() it on top of ourse. The |
| 200 | * return value will indicate this however, so in |
| 201 | * those cases, we switch them around for the locking |
| 202 | * code. |
| 203 | * |
| 204 | * NOTE: This dentry already has ->d_op set from |
| 205 | * ocfs2_get_parent() and ocfs2_get_dentry() |
| 206 | */ |
| 207 | if (ret) |
| 208 | dentry = ret; |
| 209 | |
| 210 | status = ocfs2_dentry_attach_lock(dentry, inode, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 211 | OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 212 | if (status) { |
| 213 | mlog_errno(status); |
| 214 | ret = ERR_PTR(status); |
| 215 | goto bail_unlock; |
| 216 | } |
| 217 | } |
| 218 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 219 | bail_unlock: |
| 220 | /* Don't drop the cluster lock until *after* the d_add -- |
| 221 | * unlink on another node will message us to remove that |
| 222 | * dentry under this lock so otherwise we can race this with |
| 223 | * the vote thread and have a stale dentry. */ |
| 224 | ocfs2_meta_unlock(dir, 0); |
| 225 | |
| 226 | bail: |
| 227 | if (dirent_bh) |
| 228 | brelse(dirent_bh); |
| 229 | |
| 230 | mlog_exit_ptr(ret); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int ocfs2_fill_new_dir(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 236 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 237 | struct inode *parent, |
| 238 | struct inode *inode, |
| 239 | struct buffer_head *fe_bh, |
| 240 | struct ocfs2_alloc_context *data_ac) |
| 241 | { |
| 242 | int status; |
| 243 | struct buffer_head *new_bh = NULL; |
| 244 | struct ocfs2_dir_entry *de = NULL; |
| 245 | |
| 246 | mlog_entry_void(); |
| 247 | |
| 248 | status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh, |
| 249 | data_ac, NULL, &new_bh); |
| 250 | if (status < 0) { |
| 251 | mlog_errno(status); |
| 252 | goto bail; |
| 253 | } |
| 254 | |
| 255 | ocfs2_set_new_buffer_uptodate(inode, new_bh); |
| 256 | |
| 257 | status = ocfs2_journal_access(handle, inode, new_bh, |
| 258 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 259 | if (status < 0) { |
| 260 | mlog_errno(status); |
| 261 | goto bail; |
| 262 | } |
| 263 | memset(new_bh->b_data, 0, osb->sb->s_blocksize); |
| 264 | |
| 265 | de = (struct ocfs2_dir_entry *) new_bh->b_data; |
| 266 | de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno); |
| 267 | de->name_len = 1; |
| 268 | de->rec_len = |
| 269 | cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 270 | strcpy(de->name, "."); |
| 271 | ocfs2_set_de_type(de, S_IFDIR); |
| 272 | de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len)); |
| 273 | de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno); |
| 274 | de->rec_len = cpu_to_le16(inode->i_sb->s_blocksize - |
| 275 | OCFS2_DIR_REC_LEN(1)); |
| 276 | de->name_len = 2; |
| 277 | strcpy(de->name, ".."); |
| 278 | ocfs2_set_de_type(de, S_IFDIR); |
| 279 | |
| 280 | status = ocfs2_journal_dirty(handle, new_bh); |
| 281 | if (status < 0) { |
| 282 | mlog_errno(status); |
| 283 | goto bail; |
| 284 | } |
| 285 | |
| 286 | i_size_write(inode, inode->i_sb->s_blocksize); |
| 287 | inode->i_nlink = 2; |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 288 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 289 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); |
| 290 | if (status < 0) { |
| 291 | mlog_errno(status); |
| 292 | goto bail; |
| 293 | } |
| 294 | |
| 295 | status = 0; |
| 296 | bail: |
| 297 | if (new_bh) |
| 298 | brelse(new_bh); |
| 299 | |
| 300 | mlog_exit(status); |
| 301 | return status; |
| 302 | } |
| 303 | |
| 304 | static int ocfs2_mknod(struct inode *dir, |
| 305 | struct dentry *dentry, |
| 306 | int mode, |
| 307 | dev_t dev) |
| 308 | { |
| 309 | int status = 0; |
| 310 | struct buffer_head *parent_fe_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 311 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 312 | struct ocfs2_super *osb; |
| 313 | struct ocfs2_dinode *dirfe; |
| 314 | struct buffer_head *new_fe_bh = NULL; |
| 315 | struct buffer_head *de_bh = NULL; |
| 316 | struct inode *inode = NULL; |
| 317 | struct ocfs2_alloc_context *inode_ac = NULL; |
| 318 | struct ocfs2_alloc_context *data_ac = NULL; |
| 319 | |
| 320 | mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode, |
| 321 | (unsigned long)dev, dentry->d_name.len, |
| 322 | dentry->d_name.name); |
| 323 | |
| 324 | /* get our super block */ |
| 325 | osb = OCFS2_SB(dir->i_sb); |
| 326 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 327 | status = ocfs2_meta_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | e3a8213 | 2006-10-05 16:04:17 -0700 | [diff] [blame] | 328 | if (status < 0) { |
| 329 | if (status != -ENOENT) |
| 330 | mlog_errno(status); |
| 331 | return status; |
| 332 | } |
| 333 | |
Mark Fasheh | a663e30 | 2006-08-09 11:45:07 -0700 | [diff] [blame] | 334 | if (S_ISDIR(mode) && (dir->i_nlink >= OCFS2_LINK_MAX)) { |
| 335 | status = -EMLINK; |
| 336 | goto leave; |
| 337 | } |
| 338 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 339 | dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 340 | if (!dirfe->i_links_count) { |
| 341 | /* can't make a file in a deleted directory. */ |
| 342 | status = -ENOENT; |
| 343 | goto leave; |
| 344 | } |
| 345 | |
| 346 | status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 347 | dentry->d_name.len); |
| 348 | if (status) |
| 349 | goto leave; |
| 350 | |
| 351 | /* get a spot inside the dir. */ |
| 352 | status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 353 | dentry->d_name.name, |
| 354 | dentry->d_name.len, &de_bh); |
| 355 | if (status < 0) { |
| 356 | mlog_errno(status); |
| 357 | goto leave; |
| 358 | } |
| 359 | |
| 360 | /* reserve an inode spot */ |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 361 | status = ocfs2_reserve_new_inode(osb, &inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 362 | if (status < 0) { |
| 363 | if (status != -ENOSPC) |
| 364 | mlog_errno(status); |
| 365 | goto leave; |
| 366 | } |
| 367 | |
| 368 | /* are we making a directory? If so, reserve a cluster for his |
| 369 | * 1st extent. */ |
| 370 | if (S_ISDIR(mode)) { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 371 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 372 | if (status < 0) { |
| 373 | if (status != -ENOSPC) |
| 374 | mlog_errno(status); |
| 375 | goto leave; |
| 376 | } |
| 377 | } |
| 378 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 379 | handle = ocfs2_start_trans(osb, OCFS2_MKNOD_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 380 | if (IS_ERR(handle)) { |
| 381 | status = PTR_ERR(handle); |
| 382 | handle = NULL; |
| 383 | mlog_errno(status); |
| 384 | goto leave; |
| 385 | } |
| 386 | |
| 387 | /* do the real work now. */ |
| 388 | status = ocfs2_mknod_locked(osb, dir, dentry, mode, dev, |
| 389 | &new_fe_bh, parent_fe_bh, handle, |
| 390 | &inode, inode_ac); |
| 391 | if (status < 0) { |
| 392 | mlog_errno(status); |
| 393 | goto leave; |
| 394 | } |
| 395 | |
| 396 | if (S_ISDIR(mode)) { |
| 397 | status = ocfs2_fill_new_dir(osb, handle, dir, inode, |
| 398 | new_fe_bh, data_ac); |
| 399 | if (status < 0) { |
| 400 | mlog_errno(status); |
| 401 | goto leave; |
| 402 | } |
| 403 | |
| 404 | status = ocfs2_journal_access(handle, dir, parent_fe_bh, |
| 405 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 406 | if (status < 0) { |
| 407 | mlog_errno(status); |
| 408 | goto leave; |
| 409 | } |
| 410 | le16_add_cpu(&dirfe->i_links_count, 1); |
| 411 | status = ocfs2_journal_dirty(handle, parent_fe_bh); |
| 412 | if (status < 0) { |
| 413 | mlog_errno(status); |
| 414 | goto leave; |
| 415 | } |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 416 | inc_nlink(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | status = ocfs2_add_entry(handle, dentry, inode, |
| 420 | OCFS2_I(inode)->ip_blkno, parent_fe_bh, |
| 421 | de_bh); |
| 422 | if (status < 0) { |
| 423 | mlog_errno(status); |
| 424 | goto leave; |
| 425 | } |
| 426 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 427 | status = ocfs2_dentry_attach_lock(dentry, inode, |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 428 | OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 429 | if (status) { |
| 430 | mlog_errno(status); |
| 431 | goto leave; |
| 432 | } |
| 433 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 434 | insert_inode_hash(inode); |
| 435 | dentry->d_op = &ocfs2_dentry_ops; |
| 436 | d_instantiate(dentry, inode); |
| 437 | status = 0; |
| 438 | leave: |
| 439 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 440 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 441 | |
Mark Fasheh | e3a8213 | 2006-10-05 16:04:17 -0700 | [diff] [blame] | 442 | ocfs2_meta_unlock(dir, 1); |
| 443 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 444 | if (status == -ENOSPC) |
| 445 | mlog(0, "Disk is full\n"); |
| 446 | |
| 447 | if (new_fe_bh) |
| 448 | brelse(new_fe_bh); |
| 449 | |
| 450 | if (de_bh) |
| 451 | brelse(de_bh); |
| 452 | |
| 453 | if (parent_fe_bh) |
| 454 | brelse(parent_fe_bh); |
| 455 | |
| 456 | if ((status < 0) && inode) |
| 457 | iput(inode); |
| 458 | |
| 459 | if (inode_ac) |
| 460 | ocfs2_free_alloc_context(inode_ac); |
| 461 | |
| 462 | if (data_ac) |
| 463 | ocfs2_free_alloc_context(data_ac); |
| 464 | |
| 465 | mlog_exit(status); |
| 466 | |
| 467 | return status; |
| 468 | } |
| 469 | |
| 470 | static int ocfs2_mknod_locked(struct ocfs2_super *osb, |
| 471 | struct inode *dir, |
| 472 | struct dentry *dentry, int mode, |
| 473 | dev_t dev, |
| 474 | struct buffer_head **new_fe_bh, |
| 475 | struct buffer_head *parent_fe_bh, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 476 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 477 | struct inode **ret_inode, |
| 478 | struct ocfs2_alloc_context *inode_ac) |
| 479 | { |
| 480 | int status = 0; |
| 481 | struct ocfs2_dinode *fe = NULL; |
| 482 | struct ocfs2_extent_list *fel; |
| 483 | u64 fe_blkno = 0; |
| 484 | u16 suballoc_bit; |
| 485 | struct inode *inode = NULL; |
| 486 | |
| 487 | mlog_entry("(0x%p, 0x%p, %d, %lu, '%.*s')\n", dir, dentry, mode, |
| 488 | (unsigned long)dev, dentry->d_name.len, |
| 489 | dentry->d_name.name); |
| 490 | |
| 491 | *new_fe_bh = NULL; |
| 492 | *ret_inode = NULL; |
| 493 | |
| 494 | status = ocfs2_claim_new_inode(osb, handle, inode_ac, &suballoc_bit, |
| 495 | &fe_blkno); |
| 496 | if (status < 0) { |
| 497 | mlog_errno(status); |
| 498 | goto leave; |
| 499 | } |
| 500 | |
| 501 | inode = new_inode(dir->i_sb); |
| 502 | if (IS_ERR(inode)) { |
| 503 | status = PTR_ERR(inode); |
| 504 | mlog(ML_ERROR, "new_inode failed!\n"); |
| 505 | goto leave; |
| 506 | } |
| 507 | |
| 508 | /* populate as many fields early on as possible - many of |
| 509 | * these are used by the support functions here and in |
| 510 | * callers. */ |
| 511 | inode->i_ino = ino_from_blkno(osb->sb, fe_blkno); |
| 512 | OCFS2_I(inode)->ip_blkno = fe_blkno; |
| 513 | if (S_ISDIR(mode)) |
| 514 | inode->i_nlink = 2; |
| 515 | else |
| 516 | inode->i_nlink = 1; |
| 517 | inode->i_mode = mode; |
| 518 | spin_lock(&osb->osb_lock); |
| 519 | inode->i_generation = osb->s_next_generation++; |
| 520 | spin_unlock(&osb->osb_lock); |
| 521 | |
| 522 | *new_fe_bh = sb_getblk(osb->sb, fe_blkno); |
| 523 | if (!*new_fe_bh) { |
| 524 | status = -EIO; |
| 525 | mlog_errno(status); |
| 526 | goto leave; |
| 527 | } |
| 528 | ocfs2_set_new_buffer_uptodate(inode, *new_fe_bh); |
| 529 | |
| 530 | status = ocfs2_journal_access(handle, inode, *new_fe_bh, |
| 531 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 532 | if (status < 0) { |
| 533 | mlog_errno(status); |
| 534 | goto leave; |
| 535 | } |
| 536 | |
| 537 | fe = (struct ocfs2_dinode *) (*new_fe_bh)->b_data; |
| 538 | memset(fe, 0, osb->sb->s_blocksize); |
| 539 | |
| 540 | fe->i_generation = cpu_to_le32(inode->i_generation); |
| 541 | fe->i_fs_generation = cpu_to_le32(osb->fs_generation); |
| 542 | fe->i_blkno = cpu_to_le64(fe_blkno); |
| 543 | fe->i_suballoc_bit = cpu_to_le16(suballoc_bit); |
| 544 | fe->i_suballoc_slot = cpu_to_le16(osb->slot_num); |
| 545 | fe->i_uid = cpu_to_le32(current->fsuid); |
| 546 | if (dir->i_mode & S_ISGID) { |
| 547 | fe->i_gid = cpu_to_le32(dir->i_gid); |
| 548 | if (S_ISDIR(mode)) |
| 549 | mode |= S_ISGID; |
| 550 | } else |
| 551 | fe->i_gid = cpu_to_le32(current->fsgid); |
| 552 | fe->i_mode = cpu_to_le16(mode); |
| 553 | if (S_ISCHR(mode) || S_ISBLK(mode)) |
| 554 | fe->id1.dev1.i_rdev = cpu_to_le64(huge_encode_dev(dev)); |
| 555 | |
| 556 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
| 557 | |
| 558 | fe->i_last_eb_blk = 0; |
| 559 | strcpy(fe->i_signature, OCFS2_INODE_SIGNATURE); |
| 560 | le32_add_cpu(&fe->i_flags, OCFS2_VALID_FL); |
| 561 | fe->i_atime = fe->i_ctime = fe->i_mtime = |
| 562 | cpu_to_le64(CURRENT_TIME.tv_sec); |
| 563 | fe->i_mtime_nsec = fe->i_ctime_nsec = fe->i_atime_nsec = |
| 564 | cpu_to_le32(CURRENT_TIME.tv_nsec); |
| 565 | fe->i_dtime = 0; |
| 566 | |
| 567 | fel = &fe->id2.i_list; |
| 568 | fel->l_tree_depth = 0; |
| 569 | fel->l_next_free_rec = 0; |
| 570 | fel->l_count = cpu_to_le16(ocfs2_extent_recs_per_inode(osb->sb)); |
| 571 | |
| 572 | status = ocfs2_journal_dirty(handle, *new_fe_bh); |
| 573 | if (status < 0) { |
| 574 | mlog_errno(status); |
| 575 | goto leave; |
| 576 | } |
| 577 | |
| 578 | if (ocfs2_populate_inode(inode, fe, 1) < 0) { |
| 579 | mlog(ML_ERROR, "populate inode failed! bh->b_blocknr=%llu, " |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 580 | "i_blkno=%llu, i_ino=%lu\n", |
Mark Fasheh | 1ca1a11 | 2007-04-27 16:01:25 -0700 | [diff] [blame] | 581 | (unsigned long long)(*new_fe_bh)->b_blocknr, |
| 582 | (unsigned long long)le64_to_cpu(fe->i_blkno), |
| 583 | inode->i_ino); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 584 | BUG(); |
| 585 | } |
| 586 | |
| 587 | ocfs2_inode_set_new(osb, inode); |
Sunil Mushran | c271c5c | 2006-12-05 17:56:35 -0800 | [diff] [blame] | 588 | if (!ocfs2_mount_local(osb)) { |
| 589 | status = ocfs2_create_new_inode_locks(inode); |
| 590 | if (status < 0) |
| 591 | mlog_errno(status); |
| 592 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 593 | |
| 594 | status = 0; /* error in ocfs2_create_new_inode_locks is not |
| 595 | * critical */ |
| 596 | |
| 597 | *ret_inode = inode; |
| 598 | leave: |
| 599 | if (status < 0) { |
| 600 | if (*new_fe_bh) { |
| 601 | brelse(*new_fe_bh); |
| 602 | *new_fe_bh = NULL; |
| 603 | } |
| 604 | if (inode) |
| 605 | iput(inode); |
| 606 | } |
| 607 | |
| 608 | mlog_exit(status); |
| 609 | return status; |
| 610 | } |
| 611 | |
| 612 | static int ocfs2_mkdir(struct inode *dir, |
| 613 | struct dentry *dentry, |
| 614 | int mode) |
| 615 | { |
| 616 | int ret; |
| 617 | |
| 618 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, |
| 619 | dentry->d_name.len, dentry->d_name.name); |
| 620 | ret = ocfs2_mknod(dir, dentry, mode | S_IFDIR, 0); |
| 621 | mlog_exit(ret); |
| 622 | |
| 623 | return ret; |
| 624 | } |
| 625 | |
| 626 | static int ocfs2_create(struct inode *dir, |
| 627 | struct dentry *dentry, |
| 628 | int mode, |
| 629 | struct nameidata *nd) |
| 630 | { |
| 631 | int ret; |
| 632 | |
| 633 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", dir, dentry, mode, |
| 634 | dentry->d_name.len, dentry->d_name.name); |
| 635 | ret = ocfs2_mknod(dir, dentry, mode | S_IFREG, 0); |
| 636 | mlog_exit(ret); |
| 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | static int ocfs2_link(struct dentry *old_dentry, |
| 642 | struct inode *dir, |
| 643 | struct dentry *dentry) |
| 644 | { |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 645 | handle_t *handle; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 646 | struct inode *inode = old_dentry->d_inode; |
| 647 | int err; |
| 648 | struct buffer_head *fe_bh = NULL; |
| 649 | struct buffer_head *parent_fe_bh = NULL; |
| 650 | struct buffer_head *de_bh = NULL; |
| 651 | struct ocfs2_dinode *fe = NULL; |
| 652 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 653 | |
| 654 | mlog_entry("(inode=%lu, old='%.*s' new='%.*s')\n", inode->i_ino, |
| 655 | old_dentry->d_name.len, old_dentry->d_name.name, |
| 656 | dentry->d_name.len, dentry->d_name.name); |
| 657 | |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 658 | if (S_ISDIR(inode->i_mode)) |
| 659 | return -EPERM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 660 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 661 | err = ocfs2_meta_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 662 | if (err < 0) { |
| 663 | if (err != -ENOENT) |
| 664 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 665 | return err; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Tiger Yang | 0f62de2 | 2006-08-31 20:39:47 -0700 | [diff] [blame] | 668 | if (!dir->i_nlink) { |
| 669 | err = -ENOENT; |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 670 | goto out; |
Tiger Yang | 0f62de2 | 2006-08-31 20:39:47 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 673 | err = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 674 | dentry->d_name.len); |
| 675 | if (err) |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 676 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 677 | |
| 678 | err = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 679 | dentry->d_name.name, |
| 680 | dentry->d_name.len, &de_bh); |
| 681 | if (err < 0) { |
| 682 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 683 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 686 | err = ocfs2_meta_lock(inode, &fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 687 | if (err < 0) { |
| 688 | if (err != -ENOENT) |
| 689 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 690 | goto out; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 694 | if (le16_to_cpu(fe->i_links_count) >= OCFS2_LINK_MAX) { |
| 695 | err = -EMLINK; |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 696 | goto out_unlock_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 699 | handle = ocfs2_start_trans(osb, OCFS2_LINK_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 700 | if (IS_ERR(handle)) { |
| 701 | err = PTR_ERR(handle); |
| 702 | handle = NULL; |
| 703 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 704 | goto out_unlock_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | err = ocfs2_journal_access(handle, inode, fe_bh, |
| 708 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 709 | if (err < 0) { |
| 710 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 711 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 714 | inc_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 715 | inode->i_ctime = CURRENT_TIME; |
| 716 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
| 717 | fe->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec); |
| 718 | fe->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); |
| 719 | |
| 720 | err = ocfs2_journal_dirty(handle, fe_bh); |
| 721 | if (err < 0) { |
| 722 | le16_add_cpu(&fe->i_links_count, -1); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 723 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 724 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 725 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | err = ocfs2_add_entry(handle, dentry, inode, |
| 729 | OCFS2_I(inode)->ip_blkno, |
| 730 | parent_fe_bh, de_bh); |
| 731 | if (err) { |
| 732 | le16_add_cpu(&fe->i_links_count, -1); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 733 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 734 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 735 | goto out_commit; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 736 | } |
| 737 | |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 738 | err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 739 | if (err) { |
| 740 | mlog_errno(err); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 741 | goto out_commit; |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 744 | atomic_inc(&inode->i_count); |
| 745 | dentry->d_op = &ocfs2_dentry_ops; |
| 746 | d_instantiate(dentry, inode); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 747 | |
| 748 | out_commit: |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 749 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 123a964 | 2006-10-05 16:48:23 -0700 | [diff] [blame] | 750 | out_unlock_inode: |
| 751 | ocfs2_meta_unlock(inode, 1); |
| 752 | |
| 753 | out: |
| 754 | ocfs2_meta_unlock(dir, 1); |
| 755 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 756 | if (de_bh) |
| 757 | brelse(de_bh); |
| 758 | if (fe_bh) |
| 759 | brelse(fe_bh); |
| 760 | if (parent_fe_bh) |
| 761 | brelse(parent_fe_bh); |
| 762 | |
| 763 | mlog_exit(err); |
| 764 | |
| 765 | return err; |
| 766 | } |
| 767 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 768 | /* |
| 769 | * Takes and drops an exclusive lock on the given dentry. This will |
| 770 | * force other nodes to drop it. |
| 771 | */ |
| 772 | static int ocfs2_remote_dentry_delete(struct dentry *dentry) |
| 773 | { |
| 774 | int ret; |
| 775 | |
| 776 | ret = ocfs2_dentry_lock(dentry, 1); |
| 777 | if (ret) |
| 778 | mlog_errno(ret); |
| 779 | else |
| 780 | ocfs2_dentry_unlock(dentry, 1); |
| 781 | |
| 782 | return ret; |
| 783 | } |
| 784 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 785 | static inline int inode_is_unlinkable(struct inode *inode) |
| 786 | { |
| 787 | if (S_ISDIR(inode->i_mode)) { |
| 788 | if (inode->i_nlink == 2) |
| 789 | return 1; |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | if (inode->i_nlink == 1) |
| 794 | return 1; |
| 795 | return 0; |
| 796 | } |
| 797 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 798 | static int ocfs2_unlink(struct inode *dir, |
| 799 | struct dentry *dentry) |
| 800 | { |
| 801 | int status; |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 802 | int child_locked = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 803 | struct inode *inode = dentry->d_inode; |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 804 | struct inode *orphan_dir = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 805 | struct ocfs2_super *osb = OCFS2_SB(dir->i_sb); |
| 806 | u64 blkno; |
| 807 | struct ocfs2_dinode *fe = NULL; |
| 808 | struct buffer_head *fe_bh = NULL; |
| 809 | struct buffer_head *parent_node_bh = NULL; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 810 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 811 | struct ocfs2_dir_entry *dirent = NULL; |
| 812 | struct buffer_head *dirent_bh = NULL; |
| 813 | char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; |
| 814 | struct buffer_head *orphan_entry_bh = NULL; |
| 815 | |
| 816 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry, |
| 817 | dentry->d_name.len, dentry->d_name.name); |
| 818 | |
| 819 | BUG_ON(dentry->d_parent->d_inode != dir); |
| 820 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 821 | mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 822 | |
| 823 | if (inode == osb->root_inode) { |
| 824 | mlog(0, "Cannot delete the root directory\n"); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 825 | return -EPERM; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 828 | status = ocfs2_meta_lock(dir, &parent_node_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 829 | if (status < 0) { |
| 830 | if (status != -ENOENT) |
| 831 | mlog_errno(status); |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 832 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | status = ocfs2_find_files_on_disk(dentry->d_name.name, |
| 836 | dentry->d_name.len, &blkno, |
| 837 | dir, &dirent_bh, &dirent); |
| 838 | if (status < 0) { |
| 839 | if (status != -ENOENT) |
| 840 | mlog_errno(status); |
| 841 | goto leave; |
| 842 | } |
| 843 | |
| 844 | if (OCFS2_I(inode)->ip_blkno != blkno) { |
| 845 | status = -ENOENT; |
| 846 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 847 | mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n", |
| 848 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
| 849 | (unsigned long long)blkno, OCFS2_I(inode)->ip_flags); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 850 | goto leave; |
| 851 | } |
| 852 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 853 | status = ocfs2_meta_lock(inode, &fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 854 | if (status < 0) { |
| 855 | if (status != -ENOENT) |
| 856 | mlog_errno(status); |
| 857 | goto leave; |
| 858 | } |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 859 | child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 860 | |
| 861 | if (S_ISDIR(inode->i_mode)) { |
| 862 | if (!ocfs2_empty_dir(inode)) { |
| 863 | status = -ENOTEMPTY; |
| 864 | goto leave; |
| 865 | } else if (inode->i_nlink != 2) { |
| 866 | status = -ENOTEMPTY; |
| 867 | goto leave; |
| 868 | } |
| 869 | } |
| 870 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 871 | status = ocfs2_remote_dentry_delete(dentry); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 872 | if (status < 0) { |
| 873 | /* This vote should succeed under all normal |
| 874 | * circumstances. */ |
| 875 | mlog_errno(status); |
| 876 | goto leave; |
| 877 | } |
| 878 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 879 | if (inode_is_unlinkable(inode)) { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 880 | status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, inode, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 881 | orphan_name, |
| 882 | &orphan_entry_bh); |
| 883 | if (status < 0) { |
| 884 | mlog_errno(status); |
| 885 | goto leave; |
| 886 | } |
| 887 | } |
| 888 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 889 | handle = ocfs2_start_trans(osb, OCFS2_UNLINK_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 890 | if (IS_ERR(handle)) { |
| 891 | status = PTR_ERR(handle); |
| 892 | handle = NULL; |
| 893 | mlog_errno(status); |
| 894 | goto leave; |
| 895 | } |
| 896 | |
| 897 | status = ocfs2_journal_access(handle, inode, fe_bh, |
| 898 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 899 | if (status < 0) { |
| 900 | mlog_errno(status); |
| 901 | goto leave; |
| 902 | } |
| 903 | |
| 904 | fe = (struct ocfs2_dinode *) fe_bh->b_data; |
| 905 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 906 | if (inode_is_unlinkable(inode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 907 | status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 908 | orphan_entry_bh, orphan_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 909 | if (status < 0) { |
| 910 | mlog_errno(status); |
| 911 | goto leave; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /* delete the name from the parent dir */ |
| 916 | status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh); |
| 917 | if (status < 0) { |
| 918 | mlog_errno(status); |
| 919 | goto leave; |
| 920 | } |
| 921 | |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 922 | if (S_ISDIR(inode->i_mode)) |
| 923 | drop_nlink(inode); |
| 924 | drop_nlink(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 925 | fe->i_links_count = cpu_to_le16(inode->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 926 | |
| 927 | status = ocfs2_journal_dirty(handle, fe_bh); |
| 928 | if (status < 0) { |
| 929 | mlog_errno(status); |
| 930 | goto leave; |
| 931 | } |
| 932 | |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 933 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 934 | if (S_ISDIR(inode->i_mode)) |
Mark Fasheh | 17ff785 | 2006-09-30 23:29:05 -0700 | [diff] [blame] | 935 | drop_nlink(dir); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 936 | |
| 937 | status = ocfs2_mark_inode_dirty(handle, dir, parent_node_bh); |
| 938 | if (status < 0) { |
| 939 | mlog_errno(status); |
| 940 | if (S_ISDIR(inode->i_mode)) |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 941 | inc_nlink(dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 945 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 946 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 947 | |
Mark Fasheh | 30a4f5e | 2006-10-06 11:49:45 -0700 | [diff] [blame] | 948 | if (child_locked) |
| 949 | ocfs2_meta_unlock(inode, 1); |
| 950 | |
| 951 | ocfs2_meta_unlock(dir, 1); |
| 952 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 953 | if (orphan_dir) { |
| 954 | /* This was locked for us in ocfs2_prepare_orphan_dir() */ |
| 955 | ocfs2_meta_unlock(orphan_dir, 1); |
| 956 | mutex_unlock(&orphan_dir->i_mutex); |
| 957 | iput(orphan_dir); |
| 958 | } |
| 959 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 960 | if (fe_bh) |
| 961 | brelse(fe_bh); |
| 962 | |
| 963 | if (dirent_bh) |
| 964 | brelse(dirent_bh); |
| 965 | |
| 966 | if (parent_node_bh) |
| 967 | brelse(parent_node_bh); |
| 968 | |
| 969 | if (orphan_entry_bh) |
| 970 | brelse(orphan_entry_bh); |
| 971 | |
| 972 | mlog_exit(status); |
| 973 | |
| 974 | return status; |
| 975 | } |
| 976 | |
| 977 | /* |
| 978 | * The only place this should be used is rename! |
| 979 | * if they have the same id, then the 1st one is the only one locked. |
| 980 | */ |
| 981 | static int ocfs2_double_lock(struct ocfs2_super *osb, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 982 | struct buffer_head **bh1, |
| 983 | struct inode *inode1, |
| 984 | struct buffer_head **bh2, |
| 985 | struct inode *inode2) |
| 986 | { |
| 987 | int status; |
| 988 | struct ocfs2_inode_info *oi1 = OCFS2_I(inode1); |
| 989 | struct ocfs2_inode_info *oi2 = OCFS2_I(inode2); |
| 990 | struct buffer_head **tmpbh; |
| 991 | struct inode *tmpinode; |
| 992 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 993 | mlog_entry("(inode1 = %llu, inode2 = %llu)\n", |
| 994 | (unsigned long long)oi1->ip_blkno, |
| 995 | (unsigned long long)oi2->ip_blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 996 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 997 | if (*bh1) |
| 998 | *bh1 = NULL; |
| 999 | if (*bh2) |
| 1000 | *bh2 = NULL; |
| 1001 | |
| 1002 | /* we always want to lock the one with the lower lockid first. */ |
| 1003 | if (oi1->ip_blkno != oi2->ip_blkno) { |
| 1004 | if (oi1->ip_blkno < oi2->ip_blkno) { |
| 1005 | /* switch id1 and id2 around */ |
| 1006 | mlog(0, "switching them around...\n"); |
| 1007 | tmpbh = bh2; |
| 1008 | bh2 = bh1; |
| 1009 | bh1 = tmpbh; |
| 1010 | |
| 1011 | tmpinode = inode2; |
| 1012 | inode2 = inode1; |
| 1013 | inode1 = tmpinode; |
| 1014 | } |
| 1015 | /* lock id2 */ |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1016 | status = ocfs2_meta_lock(inode2, bh2, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1017 | if (status < 0) { |
| 1018 | if (status != -ENOENT) |
| 1019 | mlog_errno(status); |
| 1020 | goto bail; |
| 1021 | } |
| 1022 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1023 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1024 | /* lock id1 */ |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1025 | status = ocfs2_meta_lock(inode1, bh1, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1026 | if (status < 0) { |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1027 | /* |
| 1028 | * An error return must mean that no cluster locks |
| 1029 | * were held on function exit. |
| 1030 | */ |
| 1031 | if (oi1->ip_blkno != oi2->ip_blkno) |
| 1032 | ocfs2_meta_unlock(inode2, 1); |
| 1033 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1034 | if (status != -ENOENT) |
| 1035 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1036 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1037 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1038 | bail: |
| 1039 | mlog_exit(status); |
| 1040 | return status; |
| 1041 | } |
| 1042 | |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1043 | static void ocfs2_double_unlock(struct inode *inode1, struct inode *inode2) |
| 1044 | { |
| 1045 | ocfs2_meta_unlock(inode1, 1); |
| 1046 | |
| 1047 | if (inode1 != inode2) |
| 1048 | ocfs2_meta_unlock(inode2, 1); |
| 1049 | } |
| 1050 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1051 | #define PARENT_INO(buffer) \ |
| 1052 | ((struct ocfs2_dir_entry *) \ |
| 1053 | ((char *)buffer + \ |
| 1054 | le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode |
| 1055 | |
| 1056 | static int ocfs2_rename(struct inode *old_dir, |
| 1057 | struct dentry *old_dentry, |
| 1058 | struct inode *new_dir, |
| 1059 | struct dentry *new_dentry) |
| 1060 | { |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1061 | int status = 0, rename_lock = 0, parents_locked = 0; |
| 1062 | int old_child_locked = 0, new_child_locked = 0; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1063 | struct inode *old_inode = old_dentry->d_inode; |
| 1064 | struct inode *new_inode = new_dentry->d_inode; |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1065 | struct inode *orphan_dir = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1066 | struct ocfs2_dinode *newfe = NULL; |
| 1067 | char orphan_name[OCFS2_ORPHAN_NAMELEN + 1]; |
| 1068 | struct buffer_head *orphan_entry_bh = NULL; |
| 1069 | struct buffer_head *newfe_bh = NULL; |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1070 | struct buffer_head *old_inode_bh = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1071 | struct buffer_head *insert_entry_bh = NULL; |
| 1072 | struct ocfs2_super *osb = NULL; |
| 1073 | u64 newfe_blkno; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1074 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1075 | struct buffer_head *old_dir_bh = NULL; |
| 1076 | struct buffer_head *new_dir_bh = NULL; |
| 1077 | struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry |
| 1078 | // and new_dentry |
| 1079 | struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above |
| 1080 | struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir, |
| 1081 | // this is the 1st dirent bh |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1082 | nlink_t old_dir_nlink = old_dir->i_nlink; |
Sunil Mushran | 480214d | 2007-08-06 15:11:56 -0700 | [diff] [blame] | 1083 | struct ocfs2_dinode *old_di; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1084 | |
| 1085 | /* At some point it might be nice to break this function up a |
| 1086 | * bit. */ |
| 1087 | |
| 1088 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n", |
| 1089 | old_dir, old_dentry, new_dir, new_dentry, |
| 1090 | old_dentry->d_name.len, old_dentry->d_name.name, |
| 1091 | new_dentry->d_name.len, new_dentry->d_name.name); |
| 1092 | |
| 1093 | osb = OCFS2_SB(old_dir->i_sb); |
| 1094 | |
| 1095 | if (new_inode) { |
| 1096 | if (!igrab(new_inode)) |
| 1097 | BUG(); |
| 1098 | } |
| 1099 | |
Uwe Kleine-König | 1b3c371 | 2007-02-17 19:23:03 +0100 | [diff] [blame] | 1100 | /* Assume a directory hierarchy thusly: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1101 | * a/b/c |
| 1102 | * a/d |
| 1103 | * a,b,c, and d are all directories. |
| 1104 | * |
| 1105 | * from cwd of 'a' on both nodes: |
| 1106 | * node1: mv b/c d |
| 1107 | * node2: mv d b/c |
| 1108 | * |
| 1109 | * And that's why, just like the VFS, we need a file system |
| 1110 | * rename lock. */ |
| 1111 | if (old_dentry != new_dentry) { |
| 1112 | status = ocfs2_rename_lock(osb); |
| 1113 | if (status < 0) { |
| 1114 | mlog_errno(status); |
| 1115 | goto bail; |
| 1116 | } |
| 1117 | rename_lock = 1; |
| 1118 | } |
| 1119 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1120 | /* if old and new are the same, this'll just do one lock. */ |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1121 | status = ocfs2_double_lock(osb, &old_dir_bh, old_dir, |
| 1122 | &new_dir_bh, new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1123 | if (status < 0) { |
| 1124 | mlog_errno(status); |
| 1125 | goto bail; |
| 1126 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1127 | parents_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1128 | |
| 1129 | /* make sure both dirs have bhs |
| 1130 | * get an extra ref on old_dir_bh if old==new */ |
| 1131 | if (!new_dir_bh) { |
| 1132 | if (old_dir_bh) { |
| 1133 | new_dir_bh = old_dir_bh; |
| 1134 | get_bh(new_dir_bh); |
| 1135 | } else { |
| 1136 | mlog(ML_ERROR, "no old_dir_bh!\n"); |
| 1137 | status = -EIO; |
| 1138 | goto bail; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1142 | /* |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1143 | * Aside from allowing a meta data update, the locking here |
| 1144 | * also ensures that the vote thread on other nodes won't have |
| 1145 | * to concurrently downconvert the inode and the dentry locks. |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1146 | */ |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1147 | status = ocfs2_meta_lock(old_inode, &old_inode_bh, 1); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1148 | if (status < 0) { |
| 1149 | if (status != -ENOENT) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1150 | mlog_errno(status); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1151 | goto bail; |
| 1152 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1153 | old_child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1154 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1155 | status = ocfs2_remote_dentry_delete(old_dentry); |
| 1156 | if (status < 0) { |
| 1157 | mlog_errno(status); |
| 1158 | goto bail; |
| 1159 | } |
| 1160 | |
| 1161 | if (S_ISDIR(old_inode->i_mode)) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1162 | status = -EIO; |
| 1163 | old_inode_de_bh = ocfs2_bread(old_inode, 0, &status, 0); |
| 1164 | if (!old_inode_de_bh) |
| 1165 | goto bail; |
| 1166 | |
| 1167 | status = -EIO; |
| 1168 | if (le64_to_cpu(PARENT_INO(old_inode_de_bh->b_data)) != |
| 1169 | OCFS2_I(old_dir)->ip_blkno) |
| 1170 | goto bail; |
| 1171 | status = -EMLINK; |
| 1172 | if (!new_inode && new_dir!=old_dir && |
| 1173 | new_dir->i_nlink >= OCFS2_LINK_MAX) |
| 1174 | goto bail; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | status = -ENOENT; |
| 1178 | old_de_bh = ocfs2_find_entry(old_dentry->d_name.name, |
| 1179 | old_dentry->d_name.len, |
| 1180 | old_dir, &old_de); |
| 1181 | if (!old_de_bh) |
| 1182 | goto bail; |
| 1183 | |
| 1184 | /* |
| 1185 | * Check for inode number is _not_ due to possible IO errors. |
| 1186 | * We might rmdir the source, keep it as pwd of some process |
| 1187 | * and merrily kill the link to whatever was created under the |
| 1188 | * same name. Goodbye sticky bit ;-< |
| 1189 | */ |
| 1190 | if (le64_to_cpu(old_de->inode) != OCFS2_I(old_inode)->ip_blkno) |
| 1191 | goto bail; |
| 1192 | |
| 1193 | /* check if the target already exists (in which case we need |
| 1194 | * to delete it */ |
| 1195 | status = ocfs2_find_files_on_disk(new_dentry->d_name.name, |
| 1196 | new_dentry->d_name.len, |
| 1197 | &newfe_blkno, new_dir, &new_de_bh, |
| 1198 | &new_de); |
| 1199 | /* The only error we allow here is -ENOENT because the new |
| 1200 | * file not existing is perfectly valid. */ |
| 1201 | if ((status < 0) && (status != -ENOENT)) { |
| 1202 | /* If we cannot find the file specified we should just */ |
| 1203 | /* return the error... */ |
| 1204 | mlog_errno(status); |
| 1205 | goto bail; |
| 1206 | } |
| 1207 | |
| 1208 | if (!new_de && new_inode) |
| 1209 | mlog(ML_ERROR, "inode %lu does not exist in it's parent " |
| 1210 | "directory!", new_inode->i_ino); |
| 1211 | |
| 1212 | /* In case we need to overwrite an existing file, we blow it |
| 1213 | * away first */ |
| 1214 | if (new_de) { |
| 1215 | /* VFS didn't think there existed an inode here, but |
| 1216 | * someone else in the cluster must have raced our |
| 1217 | * rename to create one. Today we error cleanly, in |
| 1218 | * the future we should consider calling iget to build |
| 1219 | * a new struct inode for this entry. */ |
| 1220 | if (!new_inode) { |
| 1221 | status = -EACCES; |
| 1222 | |
| 1223 | mlog(0, "We found an inode for name %.*s but VFS " |
| 1224 | "didn't give us one.\n", new_dentry->d_name.len, |
| 1225 | new_dentry->d_name.name); |
| 1226 | goto bail; |
| 1227 | } |
| 1228 | |
| 1229 | if (OCFS2_I(new_inode)->ip_blkno != newfe_blkno) { |
| 1230 | status = -EACCES; |
| 1231 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1232 | mlog(0, "Inode %llu and dir %llu disagree. flags = %x\n", |
| 1233 | (unsigned long long)OCFS2_I(new_inode)->ip_blkno, |
| 1234 | (unsigned long long)newfe_blkno, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1235 | OCFS2_I(new_inode)->ip_flags); |
| 1236 | goto bail; |
| 1237 | } |
| 1238 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1239 | status = ocfs2_meta_lock(new_inode, &newfe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1240 | if (status < 0) { |
| 1241 | if (status != -ENOENT) |
| 1242 | mlog_errno(status); |
| 1243 | goto bail; |
| 1244 | } |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1245 | new_child_locked = 1; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1246 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1247 | status = ocfs2_remote_dentry_delete(new_dentry); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1248 | if (status < 0) { |
| 1249 | mlog_errno(status); |
| 1250 | goto bail; |
| 1251 | } |
| 1252 | |
| 1253 | newfe = (struct ocfs2_dinode *) newfe_bh->b_data; |
| 1254 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1255 | mlog(0, "aha rename over existing... new_de=%p new_blkno=%llu " |
| 1256 | "newfebh=%p bhblocknr=%llu\n", new_de, |
| 1257 | (unsigned long long)newfe_blkno, newfe_bh, newfe_bh ? |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1258 | (unsigned long long)newfe_bh->b_blocknr : 0ULL); |
| 1259 | |
| 1260 | if (S_ISDIR(new_inode->i_mode) || (new_inode->i_nlink == 1)) { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1261 | status = ocfs2_prepare_orphan_dir(osb, &orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1262 | new_inode, |
| 1263 | orphan_name, |
| 1264 | &orphan_entry_bh); |
| 1265 | if (status < 0) { |
| 1266 | mlog_errno(status); |
| 1267 | goto bail; |
| 1268 | } |
| 1269 | } |
| 1270 | } else { |
| 1271 | BUG_ON(new_dentry->d_parent->d_inode != new_dir); |
| 1272 | |
| 1273 | status = ocfs2_check_dir_for_entry(new_dir, |
| 1274 | new_dentry->d_name.name, |
| 1275 | new_dentry->d_name.len); |
| 1276 | if (status) |
| 1277 | goto bail; |
| 1278 | |
| 1279 | status = ocfs2_prepare_dir_for_insert(osb, new_dir, new_dir_bh, |
| 1280 | new_dentry->d_name.name, |
| 1281 | new_dentry->d_name.len, |
| 1282 | &insert_entry_bh); |
| 1283 | if (status < 0) { |
| 1284 | mlog_errno(status); |
| 1285 | goto bail; |
| 1286 | } |
| 1287 | } |
| 1288 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1289 | handle = ocfs2_start_trans(osb, OCFS2_RENAME_CREDITS); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1290 | if (IS_ERR(handle)) { |
| 1291 | status = PTR_ERR(handle); |
| 1292 | handle = NULL; |
| 1293 | mlog_errno(status); |
| 1294 | goto bail; |
| 1295 | } |
| 1296 | |
| 1297 | if (new_de) { |
| 1298 | if (S_ISDIR(new_inode->i_mode)) { |
| 1299 | if (!ocfs2_empty_dir(new_inode) || |
| 1300 | new_inode->i_nlink != 2) { |
| 1301 | status = -ENOTEMPTY; |
| 1302 | goto bail; |
| 1303 | } |
| 1304 | } |
| 1305 | status = ocfs2_journal_access(handle, new_inode, newfe_bh, |
| 1306 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1307 | if (status < 0) { |
| 1308 | mlog_errno(status); |
| 1309 | goto bail; |
| 1310 | } |
| 1311 | |
| 1312 | if (S_ISDIR(new_inode->i_mode) || |
| 1313 | (newfe->i_links_count == cpu_to_le16(1))){ |
| 1314 | status = ocfs2_orphan_add(osb, handle, new_inode, |
| 1315 | newfe, orphan_name, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1316 | orphan_entry_bh, orphan_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1317 | if (status < 0) { |
| 1318 | mlog_errno(status); |
| 1319 | goto bail; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /* change the dirent to point to the correct inode */ |
| 1324 | status = ocfs2_journal_access(handle, new_dir, new_de_bh, |
| 1325 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1326 | if (status < 0) { |
| 1327 | mlog_errno(status); |
| 1328 | goto bail; |
| 1329 | } |
| 1330 | new_de->inode = cpu_to_le64(OCFS2_I(old_inode)->ip_blkno); |
| 1331 | new_de->file_type = old_de->file_type; |
| 1332 | new_dir->i_version++; |
| 1333 | status = ocfs2_journal_dirty(handle, new_de_bh); |
| 1334 | if (status < 0) { |
| 1335 | mlog_errno(status); |
| 1336 | goto bail; |
| 1337 | } |
| 1338 | |
| 1339 | if (S_ISDIR(new_inode->i_mode)) |
| 1340 | newfe->i_links_count = 0; |
| 1341 | else |
| 1342 | le16_add_cpu(&newfe->i_links_count, -1); |
| 1343 | |
| 1344 | status = ocfs2_journal_dirty(handle, newfe_bh); |
| 1345 | if (status < 0) { |
| 1346 | mlog_errno(status); |
| 1347 | goto bail; |
| 1348 | } |
| 1349 | } else { |
| 1350 | /* if the name was not found in new_dir, add it now */ |
| 1351 | status = ocfs2_add_entry(handle, new_dentry, old_inode, |
| 1352 | OCFS2_I(old_inode)->ip_blkno, |
| 1353 | new_dir_bh, insert_entry_bh); |
| 1354 | } |
| 1355 | |
| 1356 | old_inode->i_ctime = CURRENT_TIME; |
| 1357 | mark_inode_dirty(old_inode); |
Sunil Mushran | 480214d | 2007-08-06 15:11:56 -0700 | [diff] [blame] | 1358 | |
| 1359 | status = ocfs2_journal_access(handle, old_inode, old_inode_bh, |
| 1360 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1361 | if (status >= 0) { |
| 1362 | old_di = (struct ocfs2_dinode *) old_inode_bh->b_data; |
| 1363 | |
| 1364 | old_di->i_ctime = cpu_to_le64(old_inode->i_ctime.tv_sec); |
| 1365 | old_di->i_ctime_nsec = cpu_to_le32(old_inode->i_ctime.tv_nsec); |
| 1366 | |
| 1367 | status = ocfs2_journal_dirty(handle, old_inode_bh); |
| 1368 | if (status < 0) |
| 1369 | mlog_errno(status); |
| 1370 | } else |
| 1371 | mlog_errno(status); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1372 | |
| 1373 | /* now that the name has been added to new_dir, remove the old name */ |
| 1374 | status = ocfs2_delete_entry(handle, old_dir, old_de, old_de_bh); |
| 1375 | if (status < 0) { |
| 1376 | mlog_errno(status); |
| 1377 | goto bail; |
| 1378 | } |
| 1379 | |
| 1380 | if (new_inode) { |
| 1381 | new_inode->i_nlink--; |
| 1382 | new_inode->i_ctime = CURRENT_TIME; |
| 1383 | } |
| 1384 | old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME; |
| 1385 | if (old_inode_de_bh) { |
| 1386 | status = ocfs2_journal_access(handle, old_inode, |
| 1387 | old_inode_de_bh, |
| 1388 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1389 | PARENT_INO(old_inode_de_bh->b_data) = |
| 1390 | cpu_to_le64(OCFS2_I(new_dir)->ip_blkno); |
| 1391 | status = ocfs2_journal_dirty(handle, old_inode_de_bh); |
| 1392 | old_dir->i_nlink--; |
| 1393 | if (new_inode) { |
| 1394 | new_inode->i_nlink--; |
| 1395 | } else { |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1396 | inc_nlink(new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1397 | mark_inode_dirty(new_dir); |
| 1398 | } |
| 1399 | } |
| 1400 | mark_inode_dirty(old_dir); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1401 | ocfs2_mark_inode_dirty(handle, old_dir, old_dir_bh); |
| 1402 | if (new_inode) { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1403 | mark_inode_dirty(new_inode); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1404 | ocfs2_mark_inode_dirty(handle, new_inode, newfe_bh); |
| 1405 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1406 | |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1407 | if (old_dir != new_dir) { |
| 1408 | /* Keep the same times on both directories.*/ |
| 1409 | new_dir->i_ctime = new_dir->i_mtime = old_dir->i_ctime; |
| 1410 | |
| 1411 | /* |
| 1412 | * This will also pick up the i_nlink change from the |
| 1413 | * block above. |
| 1414 | */ |
| 1415 | ocfs2_mark_inode_dirty(handle, new_dir, new_dir_bh); |
| 1416 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1417 | |
| 1418 | if (old_dir_nlink != old_dir->i_nlink) { |
| 1419 | if (!old_dir_bh) { |
| 1420 | mlog(ML_ERROR, "need to change nlink for old dir " |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1421 | "%llu from %d to %d but bh is NULL!\n", |
| 1422 | (unsigned long long)OCFS2_I(old_dir)->ip_blkno, |
| 1423 | (int)old_dir_nlink, old_dir->i_nlink); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1424 | } else { |
| 1425 | struct ocfs2_dinode *fe; |
| 1426 | status = ocfs2_journal_access(handle, old_dir, |
| 1427 | old_dir_bh, |
| 1428 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1429 | fe = (struct ocfs2_dinode *) old_dir_bh->b_data; |
| 1430 | fe->i_links_count = cpu_to_le16(old_dir->i_nlink); |
| 1431 | status = ocfs2_journal_dirty(handle, old_dir_bh); |
| 1432 | } |
| 1433 | } |
| 1434 | |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1435 | ocfs2_dentry_move(old_dentry, new_dentry, old_dir, new_dir); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1436 | status = 0; |
| 1437 | bail: |
| 1438 | if (rename_lock) |
| 1439 | ocfs2_rename_unlock(osb); |
| 1440 | |
| 1441 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1442 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1443 | |
Mark Fasheh | 8d5596c | 2006-10-06 15:04:51 -0700 | [diff] [blame] | 1444 | if (parents_locked) |
| 1445 | ocfs2_double_unlock(old_dir, new_dir); |
| 1446 | |
| 1447 | if (old_child_locked) |
| 1448 | ocfs2_meta_unlock(old_inode, 1); |
| 1449 | |
| 1450 | if (new_child_locked) |
| 1451 | ocfs2_meta_unlock(new_inode, 1); |
| 1452 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 1453 | if (orphan_dir) { |
| 1454 | /* This was locked for us in ocfs2_prepare_orphan_dir() */ |
| 1455 | ocfs2_meta_unlock(orphan_dir, 1); |
| 1456 | mutex_unlock(&orphan_dir->i_mutex); |
| 1457 | iput(orphan_dir); |
| 1458 | } |
| 1459 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1460 | if (new_inode) |
| 1461 | sync_mapping_buffers(old_inode->i_mapping); |
| 1462 | |
| 1463 | if (new_inode) |
| 1464 | iput(new_inode); |
| 1465 | if (newfe_bh) |
| 1466 | brelse(newfe_bh); |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1467 | if (old_inode_bh) |
| 1468 | brelse(old_inode_bh); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1469 | if (old_dir_bh) |
| 1470 | brelse(old_dir_bh); |
| 1471 | if (new_dir_bh) |
| 1472 | brelse(new_dir_bh); |
| 1473 | if (new_de_bh) |
| 1474 | brelse(new_de_bh); |
| 1475 | if (old_de_bh) |
| 1476 | brelse(old_de_bh); |
| 1477 | if (old_inode_de_bh) |
| 1478 | brelse(old_inode_de_bh); |
| 1479 | if (orphan_entry_bh) |
| 1480 | brelse(orphan_entry_bh); |
| 1481 | if (insert_entry_bh) |
| 1482 | brelse(insert_entry_bh); |
| 1483 | |
| 1484 | mlog_exit(status); |
| 1485 | |
| 1486 | return status; |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | * we expect i_size = strlen(symname). Copy symname into the file |
| 1491 | * data, including the null terminator. |
| 1492 | */ |
| 1493 | static int ocfs2_create_symlink_data(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1494 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1495 | struct inode *inode, |
| 1496 | const char *symname) |
| 1497 | { |
| 1498 | struct buffer_head **bhs = NULL; |
| 1499 | const char *c; |
| 1500 | struct super_block *sb = osb->sb; |
Mark Fasheh | 4f902c3 | 2007-03-09 16:26:50 -0800 | [diff] [blame] | 1501 | u64 p_blkno, p_blocks; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1502 | int virtual, blocks, status, i, bytes_left; |
| 1503 | |
| 1504 | bytes_left = i_size_read(inode) + 1; |
| 1505 | /* we can't trust i_blocks because we're actually going to |
| 1506 | * write i_size + 1 bytes. */ |
| 1507 | blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 1508 | |
Andrew Morton | 5515eff | 2006-03-26 01:37:53 -0800 | [diff] [blame] | 1509 | mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n", |
| 1510 | (unsigned long long)inode->i_blocks, |
| 1511 | i_size_read(inode), blocks); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1512 | |
| 1513 | /* Sanity check -- make sure we're going to fit. */ |
| 1514 | if (bytes_left > |
| 1515 | ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) { |
| 1516 | status = -EIO; |
| 1517 | mlog_errno(status); |
| 1518 | goto bail; |
| 1519 | } |
| 1520 | |
| 1521 | bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL); |
| 1522 | if (!bhs) { |
| 1523 | status = -ENOMEM; |
| 1524 | mlog_errno(status); |
| 1525 | goto bail; |
| 1526 | } |
| 1527 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 1528 | status = ocfs2_extent_map_get_blocks(inode, 0, &p_blkno, &p_blocks, |
| 1529 | NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1530 | if (status < 0) { |
| 1531 | mlog_errno(status); |
| 1532 | goto bail; |
| 1533 | } |
| 1534 | |
| 1535 | /* links can never be larger than one cluster so we know this |
| 1536 | * is all going to be contiguous, but do a sanity check |
| 1537 | * anyway. */ |
| 1538 | if ((p_blocks << sb->s_blocksize_bits) < bytes_left) { |
| 1539 | status = -EIO; |
| 1540 | mlog_errno(status); |
| 1541 | goto bail; |
| 1542 | } |
| 1543 | |
| 1544 | virtual = 0; |
| 1545 | while(bytes_left > 0) { |
| 1546 | c = &symname[virtual * sb->s_blocksize]; |
| 1547 | |
| 1548 | bhs[virtual] = sb_getblk(sb, p_blkno); |
| 1549 | if (!bhs[virtual]) { |
| 1550 | status = -ENOMEM; |
| 1551 | mlog_errno(status); |
| 1552 | goto bail; |
| 1553 | } |
| 1554 | ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]); |
| 1555 | |
| 1556 | status = ocfs2_journal_access(handle, inode, bhs[virtual], |
| 1557 | OCFS2_JOURNAL_ACCESS_CREATE); |
| 1558 | if (status < 0) { |
| 1559 | mlog_errno(status); |
| 1560 | goto bail; |
| 1561 | } |
| 1562 | |
| 1563 | memset(bhs[virtual]->b_data, 0, sb->s_blocksize); |
| 1564 | |
| 1565 | memcpy(bhs[virtual]->b_data, c, |
| 1566 | (bytes_left > sb->s_blocksize) ? sb->s_blocksize : |
| 1567 | bytes_left); |
| 1568 | |
| 1569 | status = ocfs2_journal_dirty(handle, bhs[virtual]); |
| 1570 | if (status < 0) { |
| 1571 | mlog_errno(status); |
| 1572 | goto bail; |
| 1573 | } |
| 1574 | |
| 1575 | virtual++; |
| 1576 | p_blkno++; |
| 1577 | bytes_left -= sb->s_blocksize; |
| 1578 | } |
| 1579 | |
| 1580 | status = 0; |
| 1581 | bail: |
| 1582 | |
| 1583 | if (bhs) { |
| 1584 | for(i = 0; i < blocks; i++) |
| 1585 | if (bhs[i]) |
| 1586 | brelse(bhs[i]); |
| 1587 | kfree(bhs); |
| 1588 | } |
| 1589 | |
| 1590 | mlog_exit(status); |
| 1591 | return status; |
| 1592 | } |
| 1593 | |
| 1594 | static int ocfs2_symlink(struct inode *dir, |
| 1595 | struct dentry *dentry, |
| 1596 | const char *symname) |
| 1597 | { |
| 1598 | int status, l, credits; |
| 1599 | u64 newsize; |
| 1600 | struct ocfs2_super *osb = NULL; |
| 1601 | struct inode *inode = NULL; |
| 1602 | struct super_block *sb; |
| 1603 | struct buffer_head *new_fe_bh = NULL; |
| 1604 | struct buffer_head *de_bh = NULL; |
| 1605 | struct buffer_head *parent_fe_bh = NULL; |
| 1606 | struct ocfs2_dinode *fe = NULL; |
| 1607 | struct ocfs2_dinode *dirfe; |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1608 | handle_t *handle = NULL; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1609 | struct ocfs2_alloc_context *inode_ac = NULL; |
| 1610 | struct ocfs2_alloc_context *data_ac = NULL; |
| 1611 | |
| 1612 | mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir, |
| 1613 | dentry, symname, dentry->d_name.len, dentry->d_name.name); |
| 1614 | |
| 1615 | sb = dir->i_sb; |
| 1616 | osb = OCFS2_SB(sb); |
| 1617 | |
| 1618 | l = strlen(symname) + 1; |
| 1619 | |
| 1620 | credits = ocfs2_calc_symlink_credits(sb); |
| 1621 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1622 | /* lock the parent directory */ |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 1623 | status = ocfs2_meta_lock(dir, &parent_fe_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1624 | if (status < 0) { |
| 1625 | if (status != -ENOENT) |
| 1626 | mlog_errno(status); |
Mark Fasheh | 6d8fc40 | 2006-10-06 11:54:33 -0700 | [diff] [blame] | 1627 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | dirfe = (struct ocfs2_dinode *) parent_fe_bh->b_data; |
| 1631 | if (!dirfe->i_links_count) { |
| 1632 | /* can't make a file in a deleted directory. */ |
| 1633 | status = -ENOENT; |
| 1634 | goto bail; |
| 1635 | } |
| 1636 | |
| 1637 | status = ocfs2_check_dir_for_entry(dir, dentry->d_name.name, |
| 1638 | dentry->d_name.len); |
| 1639 | if (status) |
| 1640 | goto bail; |
| 1641 | |
| 1642 | status = ocfs2_prepare_dir_for_insert(osb, dir, parent_fe_bh, |
| 1643 | dentry->d_name.name, |
| 1644 | dentry->d_name.len, &de_bh); |
| 1645 | if (status < 0) { |
| 1646 | mlog_errno(status); |
| 1647 | goto bail; |
| 1648 | } |
| 1649 | |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1650 | status = ocfs2_reserve_new_inode(osb, &inode_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1651 | if (status < 0) { |
| 1652 | if (status != -ENOSPC) |
| 1653 | mlog_errno(status); |
| 1654 | goto bail; |
| 1655 | } |
| 1656 | |
| 1657 | /* don't reserve bitmap space for fast symlinks. */ |
| 1658 | if (l > ocfs2_fast_symlink_chars(sb)) { |
Mark Fasheh | da5cbf2 | 2006-10-06 18:34:35 -0700 | [diff] [blame] | 1659 | status = ocfs2_reserve_clusters(osb, 1, &data_ac); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1660 | if (status < 0) { |
| 1661 | if (status != -ENOSPC) |
| 1662 | mlog_errno(status); |
| 1663 | goto bail; |
| 1664 | } |
| 1665 | } |
| 1666 | |
Mark Fasheh | 65eff9c | 2006-10-09 17:26:22 -0700 | [diff] [blame] | 1667 | handle = ocfs2_start_trans(osb, credits); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1668 | if (IS_ERR(handle)) { |
| 1669 | status = PTR_ERR(handle); |
| 1670 | handle = NULL; |
| 1671 | mlog_errno(status); |
| 1672 | goto bail; |
| 1673 | } |
| 1674 | |
| 1675 | status = ocfs2_mknod_locked(osb, dir, dentry, |
| 1676 | S_IFLNK | S_IRWXUGO, 0, |
| 1677 | &new_fe_bh, parent_fe_bh, handle, |
| 1678 | &inode, inode_ac); |
| 1679 | if (status < 0) { |
| 1680 | mlog_errno(status); |
| 1681 | goto bail; |
| 1682 | } |
| 1683 | |
| 1684 | fe = (struct ocfs2_dinode *) new_fe_bh->b_data; |
| 1685 | inode->i_rdev = 0; |
| 1686 | newsize = l - 1; |
| 1687 | if (l > ocfs2_fast_symlink_chars(sb)) { |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1688 | u32 offset = 0; |
| 1689 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1690 | inode->i_op = &ocfs2_symlink_inode_operations; |
Mark Fasheh | 2ae99a6 | 2007-03-09 16:43:28 -0800 | [diff] [blame] | 1691 | status = ocfs2_do_extend_allocation(osb, inode, &offset, 1, 0, |
Mark Fasheh | dcd0538 | 2007-01-16 11:32:23 -0800 | [diff] [blame] | 1692 | new_fe_bh, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1693 | handle, data_ac, NULL, |
| 1694 | NULL); |
| 1695 | if (status < 0) { |
| 1696 | if (status != -ENOSPC && status != -EINTR) { |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1697 | mlog(ML_ERROR, |
| 1698 | "Failed to extend file to %llu\n", |
| 1699 | (unsigned long long)newsize); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1700 | mlog_errno(status); |
| 1701 | status = -ENOSPC; |
| 1702 | } |
| 1703 | goto bail; |
| 1704 | } |
| 1705 | i_size_write(inode, newsize); |
Mark Fasheh | 8110b07 | 2007-03-22 16:53:23 -0700 | [diff] [blame] | 1706 | inode->i_blocks = ocfs2_inode_sector_count(inode); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1707 | } else { |
| 1708 | inode->i_op = &ocfs2_fast_symlink_inode_operations; |
| 1709 | memcpy((char *) fe->id2.i_symlink, symname, l); |
| 1710 | i_size_write(inode, newsize); |
| 1711 | inode->i_blocks = 0; |
| 1712 | } |
| 1713 | |
| 1714 | status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh); |
| 1715 | if (status < 0) { |
| 1716 | mlog_errno(status); |
| 1717 | goto bail; |
| 1718 | } |
| 1719 | |
| 1720 | if (!ocfs2_inode_is_fast_symlink(inode)) { |
| 1721 | status = ocfs2_create_symlink_data(osb, handle, inode, |
| 1722 | symname); |
| 1723 | if (status < 0) { |
| 1724 | mlog_errno(status); |
| 1725 | goto bail; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | status = ocfs2_add_entry(handle, dentry, inode, |
| 1730 | le64_to_cpu(fe->i_blkno), parent_fe_bh, |
| 1731 | de_bh); |
| 1732 | if (status < 0) { |
| 1733 | mlog_errno(status); |
| 1734 | goto bail; |
| 1735 | } |
| 1736 | |
Mark Fasheh | 0027dd5 | 2006-09-21 16:51:28 -0700 | [diff] [blame] | 1737 | status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno); |
Mark Fasheh | 379dfe9 | 2006-09-08 14:21:03 -0700 | [diff] [blame] | 1738 | if (status) { |
| 1739 | mlog_errno(status); |
| 1740 | goto bail; |
| 1741 | } |
| 1742 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1743 | insert_inode_hash(inode); |
| 1744 | dentry->d_op = &ocfs2_dentry_ops; |
| 1745 | d_instantiate(dentry, inode); |
| 1746 | bail: |
| 1747 | if (handle) |
Mark Fasheh | 02dc1af | 2006-10-09 16:48:10 -0700 | [diff] [blame] | 1748 | ocfs2_commit_trans(osb, handle); |
Mark Fasheh | 6d8fc40 | 2006-10-06 11:54:33 -0700 | [diff] [blame] | 1749 | |
| 1750 | ocfs2_meta_unlock(dir, 1); |
| 1751 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1752 | if (new_fe_bh) |
| 1753 | brelse(new_fe_bh); |
| 1754 | if (parent_fe_bh) |
| 1755 | brelse(parent_fe_bh); |
| 1756 | if (de_bh) |
| 1757 | brelse(de_bh); |
| 1758 | if (inode_ac) |
| 1759 | ocfs2_free_alloc_context(inode_ac); |
| 1760 | if (data_ac) |
| 1761 | ocfs2_free_alloc_context(data_ac); |
| 1762 | if ((status < 0) && inode) |
| 1763 | iput(inode); |
| 1764 | |
| 1765 | mlog_exit(status); |
| 1766 | |
| 1767 | return status; |
| 1768 | } |
| 1769 | |
| 1770 | int ocfs2_check_dir_entry(struct inode * dir, |
| 1771 | struct ocfs2_dir_entry * de, |
| 1772 | struct buffer_head * bh, |
| 1773 | unsigned long offset) |
| 1774 | { |
| 1775 | const char *error_msg = NULL; |
| 1776 | const int rlen = le16_to_cpu(de->rec_len); |
| 1777 | |
| 1778 | if (rlen < OCFS2_DIR_REC_LEN(1)) |
| 1779 | error_msg = "rec_len is smaller than minimal"; |
| 1780 | else if (rlen % 4 != 0) |
| 1781 | error_msg = "rec_len % 4 != 0"; |
| 1782 | else if (rlen < OCFS2_DIR_REC_LEN(de->name_len)) |
| 1783 | error_msg = "rec_len is too small for name_len"; |
| 1784 | else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize) |
| 1785 | error_msg = "directory entry across blocks"; |
| 1786 | |
| 1787 | if (error_msg != NULL) |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 1788 | mlog(ML_ERROR, "bad entry in directory #%llu: %s - " |
| 1789 | "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n", |
| 1790 | (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg, |
| 1791 | offset, (unsigned long long)le64_to_cpu(de->inode), rlen, |
| 1792 | de->name_len); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1793 | return error_msg == NULL ? 1 : 0; |
| 1794 | } |
| 1795 | |
| 1796 | /* we don't always have a dentry for what we want to add, so people |
| 1797 | * like orphan dir can call this instead. |
| 1798 | * |
| 1799 | * If you pass me insert_bh, I'll skip the search of the other dir |
| 1800 | * blocks and put the record in there. |
| 1801 | */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1802 | static int __ocfs2_add_entry(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1803 | struct inode *dir, |
| 1804 | const char *name, int namelen, |
| 1805 | struct inode *inode, u64 blkno, |
| 1806 | struct buffer_head *parent_fe_bh, |
| 1807 | struct buffer_head *insert_bh) |
| 1808 | { |
| 1809 | unsigned long offset; |
| 1810 | unsigned short rec_len; |
| 1811 | struct ocfs2_dir_entry *de, *de1; |
| 1812 | struct super_block *sb; |
| 1813 | int retval, status; |
| 1814 | |
| 1815 | mlog_entry_void(); |
| 1816 | |
| 1817 | sb = dir->i_sb; |
| 1818 | |
| 1819 | if (!namelen) |
| 1820 | return -EINVAL; |
| 1821 | |
| 1822 | rec_len = OCFS2_DIR_REC_LEN(namelen); |
| 1823 | offset = 0; |
| 1824 | de = (struct ocfs2_dir_entry *) insert_bh->b_data; |
| 1825 | while (1) { |
| 1826 | BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data); |
| 1827 | /* These checks should've already been passed by the |
| 1828 | * prepare function, but I guess we can leave them |
| 1829 | * here anyway. */ |
| 1830 | if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) { |
| 1831 | retval = -ENOENT; |
| 1832 | goto bail; |
| 1833 | } |
| 1834 | if (ocfs2_match(namelen, name, de)) { |
| 1835 | retval = -EEXIST; |
| 1836 | goto bail; |
| 1837 | } |
| 1838 | if (((le64_to_cpu(de->inode) == 0) && |
| 1839 | (le16_to_cpu(de->rec_len) >= rec_len)) || |
| 1840 | (le16_to_cpu(de->rec_len) >= |
| 1841 | (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) { |
Mark Fasheh | 592282c | 2007-01-02 17:59:40 -0800 | [diff] [blame] | 1842 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
| 1843 | retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh); |
| 1844 | if (retval < 0) { |
| 1845 | mlog_errno(retval); |
| 1846 | goto bail; |
| 1847 | } |
| 1848 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1849 | status = ocfs2_journal_access(handle, dir, insert_bh, |
| 1850 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1851 | /* By now the buffer is marked for journaling */ |
| 1852 | offset += le16_to_cpu(de->rec_len); |
| 1853 | if (le64_to_cpu(de->inode)) { |
| 1854 | de1 = (struct ocfs2_dir_entry *)((char *) de + |
| 1855 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 1856 | de1->rec_len = |
| 1857 | cpu_to_le16(le16_to_cpu(de->rec_len) - |
| 1858 | OCFS2_DIR_REC_LEN(de->name_len)); |
| 1859 | de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len)); |
| 1860 | de = de1; |
| 1861 | } |
| 1862 | de->file_type = OCFS2_FT_UNKNOWN; |
| 1863 | if (blkno) { |
| 1864 | de->inode = cpu_to_le64(blkno); |
| 1865 | ocfs2_set_de_type(de, inode->i_mode); |
| 1866 | } else |
| 1867 | de->inode = 0; |
| 1868 | de->name_len = namelen; |
| 1869 | memcpy(de->name, name, namelen); |
| 1870 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1871 | dir->i_version++; |
| 1872 | status = ocfs2_journal_dirty(handle, insert_bh); |
| 1873 | retval = 0; |
| 1874 | goto bail; |
| 1875 | } |
| 1876 | offset += le16_to_cpu(de->rec_len); |
| 1877 | de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len)); |
| 1878 | } |
| 1879 | |
| 1880 | /* when you think about it, the assert above should prevent us |
| 1881 | * from ever getting here. */ |
| 1882 | retval = -ENOSPC; |
| 1883 | bail: |
| 1884 | |
| 1885 | mlog_exit(retval); |
| 1886 | return retval; |
| 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | /* |
| 1891 | * ocfs2_delete_entry deletes a directory entry by merging it with the |
| 1892 | * previous entry |
| 1893 | */ |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 1894 | static int ocfs2_delete_entry(handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1895 | struct inode *dir, |
| 1896 | struct ocfs2_dir_entry *de_del, |
| 1897 | struct buffer_head *bh) |
| 1898 | { |
| 1899 | struct ocfs2_dir_entry *de, *pde; |
| 1900 | int i, status = -ENOENT; |
| 1901 | |
| 1902 | mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh); |
| 1903 | |
| 1904 | i = 0; |
| 1905 | pde = NULL; |
| 1906 | de = (struct ocfs2_dir_entry *) bh->b_data; |
| 1907 | while (i < bh->b_size) { |
| 1908 | if (!ocfs2_check_dir_entry(dir, de, bh, i)) { |
| 1909 | status = -EIO; |
| 1910 | mlog_errno(status); |
| 1911 | goto bail; |
| 1912 | } |
| 1913 | if (de == de_del) { |
| 1914 | status = ocfs2_journal_access(handle, dir, bh, |
| 1915 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 1916 | if (status < 0) { |
| 1917 | status = -EIO; |
| 1918 | mlog_errno(status); |
| 1919 | goto bail; |
| 1920 | } |
| 1921 | if (pde) |
| 1922 | pde->rec_len = |
| 1923 | cpu_to_le16(le16_to_cpu(pde->rec_len) + |
| 1924 | le16_to_cpu(de->rec_len)); |
| 1925 | else |
| 1926 | de->inode = 0; |
| 1927 | dir->i_version++; |
| 1928 | status = ocfs2_journal_dirty(handle, bh); |
| 1929 | goto bail; |
| 1930 | } |
| 1931 | i += le16_to_cpu(de->rec_len); |
| 1932 | pde = de; |
| 1933 | de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len)); |
| 1934 | } |
| 1935 | bail: |
| 1936 | mlog_exit(status); |
| 1937 | return status; |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 1942 | */ |
| 1943 | static int inline ocfs2_search_dirblock(struct buffer_head *bh, |
| 1944 | struct inode *dir, |
| 1945 | const char *name, int namelen, |
| 1946 | unsigned long offset, |
| 1947 | struct ocfs2_dir_entry **res_dir) |
| 1948 | { |
| 1949 | struct ocfs2_dir_entry *de; |
| 1950 | char *dlimit, *de_buf; |
| 1951 | int de_len; |
| 1952 | int ret = 0; |
| 1953 | |
| 1954 | mlog_entry_void(); |
| 1955 | |
| 1956 | de_buf = bh->b_data; |
| 1957 | dlimit = de_buf + dir->i_sb->s_blocksize; |
| 1958 | |
| 1959 | while (de_buf < dlimit) { |
| 1960 | /* this code is executed quadratically often */ |
| 1961 | /* do minimal checking `by hand' */ |
| 1962 | |
| 1963 | de = (struct ocfs2_dir_entry *) de_buf; |
| 1964 | |
| 1965 | if (de_buf + namelen <= dlimit && |
| 1966 | ocfs2_match(namelen, name, de)) { |
| 1967 | /* found a match - just to be sure, do a full check */ |
| 1968 | if (!ocfs2_check_dir_entry(dir, de, bh, offset)) { |
| 1969 | ret = -1; |
| 1970 | goto bail; |
| 1971 | } |
| 1972 | *res_dir = de; |
| 1973 | ret = 1; |
| 1974 | goto bail; |
| 1975 | } |
| 1976 | |
| 1977 | /* prevent looping on a bad block */ |
| 1978 | de_len = le16_to_cpu(de->rec_len); |
| 1979 | if (de_len <= 0) { |
| 1980 | ret = -1; |
| 1981 | goto bail; |
| 1982 | } |
| 1983 | |
| 1984 | de_buf += de_len; |
| 1985 | offset += de_len; |
| 1986 | } |
| 1987 | |
| 1988 | bail: |
| 1989 | mlog_exit(ret); |
| 1990 | return ret; |
| 1991 | } |
| 1992 | |
| 1993 | struct buffer_head *ocfs2_find_entry(const char *name, int namelen, |
| 1994 | struct inode *dir, |
| 1995 | struct ocfs2_dir_entry **res_dir) |
| 1996 | { |
| 1997 | struct super_block *sb; |
| 1998 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 1999 | struct buffer_head *bh, *ret = NULL; |
| 2000 | unsigned long start, block, b; |
| 2001 | int ra_max = 0; /* Number of bh's in the readahead |
| 2002 | buffer, bh_use[] */ |
| 2003 | int ra_ptr = 0; /* Current index into readahead |
| 2004 | buffer */ |
| 2005 | int num = 0; |
| 2006 | int nblocks, i, err; |
| 2007 | |
| 2008 | mlog_entry_void(); |
| 2009 | |
| 2010 | *res_dir = NULL; |
| 2011 | sb = dir->i_sb; |
| 2012 | |
| 2013 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 2014 | start = OCFS2_I(dir)->ip_dir_start_lookup; |
| 2015 | if (start >= nblocks) |
| 2016 | start = 0; |
| 2017 | block = start; |
| 2018 | |
| 2019 | restart: |
| 2020 | do { |
| 2021 | /* |
| 2022 | * We deal with the read-ahead logic here. |
| 2023 | */ |
| 2024 | if (ra_ptr >= ra_max) { |
| 2025 | /* Refill the readahead buffer */ |
| 2026 | ra_ptr = 0; |
| 2027 | b = block; |
| 2028 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 2029 | /* |
| 2030 | * Terminate if we reach the end of the |
| 2031 | * directory and must wrap, or if our |
| 2032 | * search has finished at this block. |
| 2033 | */ |
| 2034 | if (b >= nblocks || (num && block == start)) { |
| 2035 | bh_use[ra_max] = NULL; |
| 2036 | break; |
| 2037 | } |
| 2038 | num++; |
| 2039 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2040 | bh = ocfs2_bread(dir, b++, &err, 1); |
| 2041 | bh_use[ra_max] = bh; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2042 | } |
| 2043 | } |
| 2044 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 2045 | goto next; |
| 2046 | wait_on_buffer(bh); |
| 2047 | if (!buffer_uptodate(bh)) { |
| 2048 | /* read error, skip block & hope for the best */ |
Mark Fasheh | aa95887 | 2006-04-21 13:49:02 -0700 | [diff] [blame] | 2049 | ocfs2_error(dir->i_sb, "reading directory %llu, " |
| 2050 | "offset %lu\n", |
| 2051 | (unsigned long long)OCFS2_I(dir)->ip_blkno, |
| 2052 | block); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2053 | brelse(bh); |
| 2054 | goto next; |
| 2055 | } |
| 2056 | i = ocfs2_search_dirblock(bh, dir, name, namelen, |
| 2057 | block << sb->s_blocksize_bits, |
| 2058 | res_dir); |
| 2059 | if (i == 1) { |
| 2060 | OCFS2_I(dir)->ip_dir_start_lookup = block; |
| 2061 | ret = bh; |
| 2062 | goto cleanup_and_exit; |
| 2063 | } else { |
| 2064 | brelse(bh); |
| 2065 | if (i < 0) |
| 2066 | goto cleanup_and_exit; |
| 2067 | } |
| 2068 | next: |
| 2069 | if (++block >= nblocks) |
| 2070 | block = 0; |
| 2071 | } while (block != start); |
| 2072 | |
| 2073 | /* |
| 2074 | * If the directory has grown while we were searching, then |
| 2075 | * search the last part of the directory before giving up. |
| 2076 | */ |
| 2077 | block = nblocks; |
| 2078 | nblocks = i_size_read(dir) >> sb->s_blocksize_bits; |
| 2079 | if (block < nblocks) { |
| 2080 | start = 0; |
| 2081 | goto restart; |
| 2082 | } |
| 2083 | |
| 2084 | cleanup_and_exit: |
| 2085 | /* Clean up the read-ahead blocks */ |
| 2086 | for (; ra_ptr < ra_max; ra_ptr++) |
| 2087 | brelse(bh_use[ra_ptr]); |
| 2088 | |
| 2089 | mlog_exit_ptr(ret); |
| 2090 | return ret; |
| 2091 | } |
| 2092 | |
| 2093 | static int ocfs2_blkno_stringify(u64 blkno, char *name) |
| 2094 | { |
| 2095 | int status, namelen; |
| 2096 | |
| 2097 | mlog_entry_void(); |
| 2098 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2099 | namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx", |
| 2100 | (long long)blkno); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2101 | if (namelen <= 0) { |
| 2102 | if (namelen) |
| 2103 | status = namelen; |
| 2104 | else |
| 2105 | status = -EINVAL; |
| 2106 | mlog_errno(status); |
| 2107 | goto bail; |
| 2108 | } |
| 2109 | if (namelen != OCFS2_ORPHAN_NAMELEN) { |
| 2110 | status = -EINVAL; |
| 2111 | mlog_errno(status); |
| 2112 | goto bail; |
| 2113 | } |
| 2114 | |
| 2115 | mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name, |
| 2116 | namelen); |
| 2117 | |
| 2118 | status = 0; |
| 2119 | bail: |
| 2120 | mlog_exit(status); |
| 2121 | return status; |
| 2122 | } |
| 2123 | |
| 2124 | static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2125 | struct inode **ret_orphan_dir, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2126 | struct inode *inode, |
| 2127 | char *name, |
| 2128 | struct buffer_head **de_bh) |
| 2129 | { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2130 | struct inode *orphan_dir_inode; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2131 | struct buffer_head *orphan_dir_bh = NULL; |
| 2132 | int status = 0; |
| 2133 | |
| 2134 | status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name); |
| 2135 | if (status < 0) { |
| 2136 | mlog_errno(status); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2137 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | orphan_dir_inode = ocfs2_get_system_file_inode(osb, |
| 2141 | ORPHAN_DIR_SYSTEM_INODE, |
| 2142 | osb->slot_num); |
| 2143 | if (!orphan_dir_inode) { |
| 2144 | status = -ENOENT; |
| 2145 | mlog_errno(status); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2146 | return status; |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2147 | } |
| 2148 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2149 | mutex_lock(&orphan_dir_inode->i_mutex); |
| 2150 | |
Mark Fasheh | 4bcec18 | 2006-10-09 16:02:40 -0700 | [diff] [blame] | 2151 | status = ocfs2_meta_lock(orphan_dir_inode, &orphan_dir_bh, 1); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2152 | if (status < 0) { |
| 2153 | mlog_errno(status); |
| 2154 | goto leave; |
| 2155 | } |
| 2156 | |
| 2157 | status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode, |
| 2158 | orphan_dir_bh, name, |
| 2159 | OCFS2_ORPHAN_NAMELEN, de_bh); |
| 2160 | if (status < 0) { |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2161 | ocfs2_meta_unlock(orphan_dir_inode, 1); |
| 2162 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2163 | mlog_errno(status); |
| 2164 | goto leave; |
| 2165 | } |
| 2166 | |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2167 | *ret_orphan_dir = orphan_dir_inode; |
| 2168 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2169 | leave: |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2170 | if (status) { |
| 2171 | mutex_unlock(&orphan_dir_inode->i_mutex); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2172 | iput(orphan_dir_inode); |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2173 | } |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2174 | |
| 2175 | if (orphan_dir_bh) |
| 2176 | brelse(orphan_dir_bh); |
| 2177 | |
| 2178 | mlog_exit(status); |
| 2179 | return status; |
| 2180 | } |
| 2181 | |
| 2182 | static int ocfs2_orphan_add(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 2183 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2184 | struct inode *inode, |
| 2185 | struct ocfs2_dinode *fe, |
| 2186 | char *name, |
Mark Fasheh | 5098c27 | 2006-10-05 18:12:57 -0700 | [diff] [blame] | 2187 | struct buffer_head *de_bh, |
| 2188 | struct inode *orphan_dir_inode) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2189 | { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2190 | struct buffer_head *orphan_dir_bh = NULL; |
| 2191 | int status = 0; |
| 2192 | struct ocfs2_dinode *orphan_fe; |
| 2193 | |
| 2194 | mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino); |
| 2195 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2196 | status = ocfs2_read_block(osb, |
| 2197 | OCFS2_I(orphan_dir_inode)->ip_blkno, |
| 2198 | &orphan_dir_bh, OCFS2_BH_CACHED, |
| 2199 | orphan_dir_inode); |
| 2200 | if (status < 0) { |
| 2201 | mlog_errno(status); |
| 2202 | goto leave; |
| 2203 | } |
| 2204 | |
| 2205 | status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh, |
| 2206 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2207 | if (status < 0) { |
| 2208 | mlog_errno(status); |
| 2209 | goto leave; |
| 2210 | } |
| 2211 | |
| 2212 | /* we're a cluster, and nlink can change on disk from |
| 2213 | * underneath us... */ |
| 2214 | orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; |
| 2215 | if (S_ISDIR(inode->i_mode)) |
| 2216 | le16_add_cpu(&orphan_fe->i_links_count, 1); |
| 2217 | orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count); |
| 2218 | |
| 2219 | status = ocfs2_journal_dirty(handle, orphan_dir_bh); |
| 2220 | if (status < 0) { |
| 2221 | mlog_errno(status); |
| 2222 | goto leave; |
| 2223 | } |
| 2224 | |
| 2225 | status = __ocfs2_add_entry(handle, orphan_dir_inode, name, |
| 2226 | OCFS2_ORPHAN_NAMELEN, inode, |
| 2227 | OCFS2_I(inode)->ip_blkno, |
| 2228 | orphan_dir_bh, de_bh); |
| 2229 | if (status < 0) { |
| 2230 | mlog_errno(status); |
| 2231 | goto leave; |
| 2232 | } |
| 2233 | |
| 2234 | le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL); |
| 2235 | |
| 2236 | /* Record which orphan dir our inode now resides |
| 2237 | * in. delete_inode will use this to determine which orphan |
| 2238 | * dir to lock. */ |
Tiger Yang | 5000863 | 2007-03-20 16:01:38 -0700 | [diff] [blame] | 2239 | fe->i_orphaned_slot = cpu_to_le16(osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2240 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2241 | mlog(0, "Inode %llu orphaned in slot %d\n", |
| 2242 | (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2243 | |
| 2244 | leave: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2245 | if (orphan_dir_bh) |
| 2246 | brelse(orphan_dir_bh); |
| 2247 | |
| 2248 | mlog_exit(status); |
| 2249 | return status; |
| 2250 | } |
| 2251 | |
| 2252 | /* unlike orphan_add, we expect the orphan dir to already be locked here. */ |
| 2253 | int ocfs2_orphan_del(struct ocfs2_super *osb, |
Mark Fasheh | 1fabe14 | 2006-10-09 18:11:45 -0700 | [diff] [blame] | 2254 | handle_t *handle, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2255 | struct inode *orphan_dir_inode, |
| 2256 | struct inode *inode, |
| 2257 | struct buffer_head *orphan_dir_bh) |
| 2258 | { |
| 2259 | char name[OCFS2_ORPHAN_NAMELEN + 1]; |
| 2260 | struct ocfs2_dinode *orphan_fe; |
| 2261 | int status = 0; |
| 2262 | struct buffer_head *target_de_bh = NULL; |
| 2263 | struct ocfs2_dir_entry *target_de = NULL; |
| 2264 | |
| 2265 | mlog_entry_void(); |
| 2266 | |
| 2267 | status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name); |
| 2268 | if (status < 0) { |
| 2269 | mlog_errno(status); |
| 2270 | goto leave; |
| 2271 | } |
| 2272 | |
Mark Fasheh | b069705 | 2006-03-03 10:24:33 -0800 | [diff] [blame] | 2273 | mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n", |
| 2274 | name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno, |
| 2275 | OCFS2_ORPHAN_NAMELEN); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2276 | |
| 2277 | /* find it's spot in the orphan directory */ |
| 2278 | target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN, |
| 2279 | orphan_dir_inode, &target_de); |
| 2280 | if (!target_de_bh) { |
| 2281 | status = -ENOENT; |
| 2282 | mlog_errno(status); |
| 2283 | goto leave; |
| 2284 | } |
| 2285 | |
| 2286 | /* remove it from the orphan directory */ |
| 2287 | status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de, |
| 2288 | target_de_bh); |
| 2289 | if (status < 0) { |
| 2290 | mlog_errno(status); |
| 2291 | goto leave; |
| 2292 | } |
| 2293 | |
| 2294 | status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh, |
| 2295 | OCFS2_JOURNAL_ACCESS_WRITE); |
| 2296 | if (status < 0) { |
| 2297 | mlog_errno(status); |
| 2298 | goto leave; |
| 2299 | } |
| 2300 | |
| 2301 | /* do the i_nlink dance! :) */ |
| 2302 | orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data; |
| 2303 | if (S_ISDIR(inode->i_mode)) |
| 2304 | le16_add_cpu(&orphan_fe->i_links_count, -1); |
| 2305 | orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count); |
| 2306 | |
| 2307 | status = ocfs2_journal_dirty(handle, orphan_dir_bh); |
| 2308 | if (status < 0) { |
| 2309 | mlog_errno(status); |
| 2310 | goto leave; |
| 2311 | } |
| 2312 | |
| 2313 | leave: |
| 2314 | if (target_de_bh) |
| 2315 | brelse(target_de_bh); |
| 2316 | |
| 2317 | mlog_exit(status); |
| 2318 | return status; |
| 2319 | } |
| 2320 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2321 | const struct inode_operations ocfs2_dir_iops = { |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2322 | .create = ocfs2_create, |
| 2323 | .lookup = ocfs2_lookup, |
| 2324 | .link = ocfs2_link, |
| 2325 | .unlink = ocfs2_unlink, |
| 2326 | .rmdir = ocfs2_unlink, |
| 2327 | .symlink = ocfs2_symlink, |
| 2328 | .mkdir = ocfs2_mkdir, |
| 2329 | .mknod = ocfs2_mknod, |
| 2330 | .rename = ocfs2_rename, |
| 2331 | .setattr = ocfs2_setattr, |
| 2332 | .getattr = ocfs2_getattr, |
Tiger Yang | d38eb8d | 2006-11-27 09:59:21 +0800 | [diff] [blame] | 2333 | .permission = ocfs2_permission, |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 2334 | }; |