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