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