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