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