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