blob: 6fa978874c33cabf622e833b9ff33f9d5f3c0c00 [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,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700220 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700221 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,
Mark Fasheh0027dd52006-09-21 16:51:28 -0700444 OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700445 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 Fasheh0027dd52006-09-21 16:51:28 -0700757 err = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -0700758 if (err) {
759 mlog_errno(err);
760 goto bail;
761 }
762
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 atomic_inc(&inode->i_count);
764 dentry->d_op = &ocfs2_dentry_ops;
765 d_instantiate(dentry, inode);
766bail:
767 if (handle)
768 ocfs2_commit_trans(handle);
769 if (de_bh)
770 brelse(de_bh);
771 if (fe_bh)
772 brelse(fe_bh);
773 if (parent_fe_bh)
774 brelse(parent_fe_bh);
775
776 mlog_exit(err);
777
778 return err;
779}
780
Mark Fasheh379dfe92006-09-08 14:21:03 -0700781/*
782 * Takes and drops an exclusive lock on the given dentry. This will
783 * force other nodes to drop it.
784 */
785static int ocfs2_remote_dentry_delete(struct dentry *dentry)
786{
787 int ret;
788
789 ret = ocfs2_dentry_lock(dentry, 1);
790 if (ret)
791 mlog_errno(ret);
792 else
793 ocfs2_dentry_unlock(dentry, 1);
794
795 return ret;
796}
797
Mark Fashehccd979b2005-12-15 14:31:24 -0800798static int ocfs2_unlink(struct inode *dir,
799 struct dentry *dentry)
800{
801 int status;
802 unsigned int saved_nlink = 0;
803 struct inode *inode = dentry->d_inode;
804 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
805 u64 blkno;
806 struct ocfs2_dinode *fe = NULL;
807 struct buffer_head *fe_bh = NULL;
808 struct buffer_head *parent_node_bh = NULL;
809 struct ocfs2_journal_handle *handle = NULL;
810 struct ocfs2_dir_entry *dirent = NULL;
811 struct buffer_head *dirent_bh = NULL;
812 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
813 struct buffer_head *orphan_entry_bh = NULL;
814
815 mlog_entry("(0x%p, 0x%p, '%.*s')\n", dir, dentry,
816 dentry->d_name.len, dentry->d_name.name);
817
818 BUG_ON(dentry->d_parent->d_inode != dir);
819
Mark Fashehb06970532006-03-03 10:24:33 -0800820 mlog(0, "ino = %llu\n", (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -0800821
822 if (inode == osb->root_inode) {
823 mlog(0, "Cannot delete the root directory\n");
824 status = -EPERM;
825 goto leave;
826 }
827
828 handle = ocfs2_alloc_handle(osb);
829 if (handle == NULL) {
830 status = -ENOMEM;
831 mlog_errno(status);
832 goto leave;
833 }
834
835 status = ocfs2_meta_lock(dir, handle, &parent_node_bh, 1);
836 if (status < 0) {
837 if (status != -ENOENT)
838 mlog_errno(status);
839 goto leave;
840 }
841
842 status = ocfs2_find_files_on_disk(dentry->d_name.name,
843 dentry->d_name.len, &blkno,
844 dir, &dirent_bh, &dirent);
845 if (status < 0) {
846 if (status != -ENOENT)
847 mlog_errno(status);
848 goto leave;
849 }
850
851 if (OCFS2_I(inode)->ip_blkno != blkno) {
852 status = -ENOENT;
853
Mark Fashehb06970532006-03-03 10:24:33 -0800854 mlog(0, "ip_blkno %llu != dirent blkno %llu ip_flags = %x\n",
855 (unsigned long long)OCFS2_I(inode)->ip_blkno,
856 (unsigned long long)blkno, OCFS2_I(inode)->ip_flags);
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 goto leave;
858 }
859
860 status = ocfs2_meta_lock(inode, handle, &fe_bh, 1);
861 if (status < 0) {
862 if (status != -ENOENT)
863 mlog_errno(status);
864 goto leave;
865 }
866
867 if (S_ISDIR(inode->i_mode)) {
868 if (!ocfs2_empty_dir(inode)) {
869 status = -ENOTEMPTY;
870 goto leave;
871 } else if (inode->i_nlink != 2) {
872 status = -ENOTEMPTY;
873 goto leave;
874 }
875 }
876
877 /* There are still a few steps left until we can consider the
878 * unlink to have succeeded. Save off nlink here before
879 * modification so we can set it back in case we hit an issue
880 * before commit. */
881 saved_nlink = inode->i_nlink;
882 if (S_ISDIR(inode->i_mode))
883 inode->i_nlink = 0;
884 else
885 inode->i_nlink--;
886
Mark Fasheh379dfe92006-09-08 14:21:03 -0700887 status = ocfs2_remote_dentry_delete(dentry);
Mark Fashehccd979b2005-12-15 14:31:24 -0800888 if (status < 0) {
889 /* This vote should succeed under all normal
890 * circumstances. */
891 mlog_errno(status);
892 goto leave;
893 }
894
895 if (!inode->i_nlink) {
896 status = ocfs2_prepare_orphan_dir(osb, handle, inode,
897 orphan_name,
898 &orphan_entry_bh);
899 if (status < 0) {
900 mlog_errno(status);
901 goto leave;
902 }
903 }
904
905 handle = ocfs2_start_trans(osb, handle, OCFS2_UNLINK_CREDITS);
906 if (IS_ERR(handle)) {
907 status = PTR_ERR(handle);
908 handle = NULL;
909 mlog_errno(status);
910 goto leave;
911 }
912
913 status = ocfs2_journal_access(handle, inode, fe_bh,
914 OCFS2_JOURNAL_ACCESS_WRITE);
915 if (status < 0) {
916 mlog_errno(status);
917 goto leave;
918 }
919
920 fe = (struct ocfs2_dinode *) fe_bh->b_data;
921
922 if (!inode->i_nlink) {
923 status = ocfs2_orphan_add(osb, handle, inode, fe, orphan_name,
924 orphan_entry_bh);
925 if (status < 0) {
926 mlog_errno(status);
927 goto leave;
928 }
929 }
930
931 /* delete the name from the parent dir */
932 status = ocfs2_delete_entry(handle, dir, dirent, dirent_bh);
933 if (status < 0) {
934 mlog_errno(status);
935 goto leave;
936 }
937
938 /* We can set nlink on the dinode now. clear the saved version
939 * so that it doesn't get set later. */
940 fe->i_links_count = cpu_to_le16(inode->i_nlink);
941 saved_nlink = 0;
942
943 status = ocfs2_journal_dirty(handle, fe_bh);
944 if (status < 0) {
945 mlog_errno(status);
946 goto leave;
947 }
948
949 if (S_ISDIR(inode->i_mode)) {
950 dir->i_nlink--;
951 status = ocfs2_mark_inode_dirty(handle, dir,
952 parent_node_bh);
953 if (status < 0) {
954 mlog_errno(status);
955 dir->i_nlink++;
956 }
957 }
958
959leave:
960 if (status < 0 && saved_nlink)
961 inode->i_nlink = saved_nlink;
962
963 if (handle)
964 ocfs2_commit_trans(handle);
965
966 if (fe_bh)
967 brelse(fe_bh);
968
969 if (dirent_bh)
970 brelse(dirent_bh);
971
972 if (parent_node_bh)
973 brelse(parent_node_bh);
974
975 if (orphan_entry_bh)
976 brelse(orphan_entry_bh);
977
978 mlog_exit(status);
979
980 return status;
981}
982
983/*
984 * The only place this should be used is rename!
985 * if they have the same id, then the 1st one is the only one locked.
986 */
987static int ocfs2_double_lock(struct ocfs2_super *osb,
988 struct ocfs2_journal_handle *handle,
989 struct buffer_head **bh1,
990 struct inode *inode1,
991 struct buffer_head **bh2,
992 struct inode *inode2)
993{
994 int status;
995 struct ocfs2_inode_info *oi1 = OCFS2_I(inode1);
996 struct ocfs2_inode_info *oi2 = OCFS2_I(inode2);
997 struct buffer_head **tmpbh;
998 struct inode *tmpinode;
999
Mark Fashehb06970532006-03-03 10:24:33 -08001000 mlog_entry("(inode1 = %llu, inode2 = %llu)\n",
1001 (unsigned long long)oi1->ip_blkno,
1002 (unsigned long long)oi2->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001003
1004 BUG_ON(!handle);
1005
1006 if (*bh1)
1007 *bh1 = NULL;
1008 if (*bh2)
1009 *bh2 = NULL;
1010
1011 /* we always want to lock the one with the lower lockid first. */
1012 if (oi1->ip_blkno != oi2->ip_blkno) {
1013 if (oi1->ip_blkno < oi2->ip_blkno) {
1014 /* switch id1 and id2 around */
1015 mlog(0, "switching them around...\n");
1016 tmpbh = bh2;
1017 bh2 = bh1;
1018 bh1 = tmpbh;
1019
1020 tmpinode = inode2;
1021 inode2 = inode1;
1022 inode1 = tmpinode;
1023 }
1024 /* lock id2 */
1025 status = ocfs2_meta_lock(inode2, handle, bh2, 1);
1026 if (status < 0) {
1027 if (status != -ENOENT)
1028 mlog_errno(status);
1029 goto bail;
1030 }
1031 }
1032 /* lock id1 */
1033 status = ocfs2_meta_lock(inode1, handle, bh1, 1);
1034 if (status < 0) {
1035 if (status != -ENOENT)
1036 mlog_errno(status);
1037 goto bail;
1038 }
1039bail:
1040 mlog_exit(status);
1041 return status;
1042}
1043
1044#define PARENT_INO(buffer) \
1045 ((struct ocfs2_dir_entry *) \
1046 ((char *)buffer + \
1047 le16_to_cpu(((struct ocfs2_dir_entry *)buffer)->rec_len)))->inode
1048
1049static int ocfs2_rename(struct inode *old_dir,
1050 struct dentry *old_dentry,
1051 struct inode *new_dir,
1052 struct dentry *new_dentry)
1053{
1054 int status = 0, rename_lock = 0;
1055 struct inode *old_inode = old_dentry->d_inode;
1056 struct inode *new_inode = new_dentry->d_inode;
1057 struct ocfs2_dinode *newfe = NULL;
1058 char orphan_name[OCFS2_ORPHAN_NAMELEN + 1];
1059 struct buffer_head *orphan_entry_bh = NULL;
1060 struct buffer_head *newfe_bh = NULL;
1061 struct buffer_head *insert_entry_bh = NULL;
1062 struct ocfs2_super *osb = NULL;
1063 u64 newfe_blkno;
1064 struct ocfs2_journal_handle *handle = NULL;
1065 struct buffer_head *old_dir_bh = NULL;
1066 struct buffer_head *new_dir_bh = NULL;
1067 struct ocfs2_dir_entry *old_de = NULL, *new_de = NULL; // dirent for old_dentry
1068 // and new_dentry
1069 struct buffer_head *new_de_bh = NULL, *old_de_bh = NULL; // bhs for above
1070 struct buffer_head *old_inode_de_bh = NULL; // if old_dentry is a dir,
1071 // this is the 1st dirent bh
1072 nlink_t old_dir_nlink = old_dir->i_nlink, new_dir_nlink = new_dir->i_nlink;
Mark Fashehccd979b2005-12-15 14:31:24 -08001073
1074 /* At some point it might be nice to break this function up a
1075 * bit. */
1076
1077 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p, from='%.*s' to='%.*s')\n",
1078 old_dir, old_dentry, new_dir, new_dentry,
1079 old_dentry->d_name.len, old_dentry->d_name.name,
1080 new_dentry->d_name.len, new_dentry->d_name.name);
1081
1082 osb = OCFS2_SB(old_dir->i_sb);
1083
1084 if (new_inode) {
1085 if (!igrab(new_inode))
1086 BUG();
1087 }
1088
1089 if (atomic_read(&old_dentry->d_count) > 2) {
1090 shrink_dcache_parent(old_dentry);
1091 if (atomic_read(&old_dentry->d_count) > 2) {
1092 status = -EBUSY;
1093 goto bail;
1094 }
1095 }
1096
1097 /* 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)) {
1264 status = ocfs2_prepare_orphan_dir(osb, handle,
1265 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,
1319 orphan_entry_bh);
1320 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 {
1385 new_dir->i_nlink++;
1386 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
1438 if (new_inode)
1439 sync_mapping_buffers(old_inode->i_mapping);
1440
1441 if (new_inode)
1442 iput(new_inode);
1443 if (newfe_bh)
1444 brelse(newfe_bh);
1445 if (old_dir_bh)
1446 brelse(old_dir_bh);
1447 if (new_dir_bh)
1448 brelse(new_dir_bh);
1449 if (new_de_bh)
1450 brelse(new_de_bh);
1451 if (old_de_bh)
1452 brelse(old_de_bh);
1453 if (old_inode_de_bh)
1454 brelse(old_inode_de_bh);
1455 if (orphan_entry_bh)
1456 brelse(orphan_entry_bh);
1457 if (insert_entry_bh)
1458 brelse(insert_entry_bh);
1459
1460 mlog_exit(status);
1461
1462 return status;
1463}
1464
1465/*
1466 * we expect i_size = strlen(symname). Copy symname into the file
1467 * data, including the null terminator.
1468 */
1469static int ocfs2_create_symlink_data(struct ocfs2_super *osb,
1470 struct ocfs2_journal_handle *handle,
1471 struct inode *inode,
1472 const char *symname)
1473{
1474 struct buffer_head **bhs = NULL;
1475 const char *c;
1476 struct super_block *sb = osb->sb;
1477 u64 p_blkno;
1478 int p_blocks;
1479 int virtual, blocks, status, i, bytes_left;
1480
1481 bytes_left = i_size_read(inode) + 1;
1482 /* we can't trust i_blocks because we're actually going to
1483 * write i_size + 1 bytes. */
1484 blocks = (bytes_left + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
1485
Andrew Morton5515eff2006-03-26 01:37:53 -08001486 mlog_entry("i_blocks = %llu, i_size = %llu, blocks = %d\n",
1487 (unsigned long long)inode->i_blocks,
1488 i_size_read(inode), blocks);
Mark Fashehccd979b2005-12-15 14:31:24 -08001489
1490 /* Sanity check -- make sure we're going to fit. */
1491 if (bytes_left >
1492 ocfs2_clusters_to_bytes(sb, OCFS2_I(inode)->ip_clusters)) {
1493 status = -EIO;
1494 mlog_errno(status);
1495 goto bail;
1496 }
1497
1498 bhs = kcalloc(blocks, sizeof(struct buffer_head *), GFP_KERNEL);
1499 if (!bhs) {
1500 status = -ENOMEM;
1501 mlog_errno(status);
1502 goto bail;
1503 }
1504
1505 status = ocfs2_extent_map_get_blocks(inode, 0, 1, &p_blkno,
1506 &p_blocks);
1507 if (status < 0) {
1508 mlog_errno(status);
1509 goto bail;
1510 }
1511
1512 /* links can never be larger than one cluster so we know this
1513 * is all going to be contiguous, but do a sanity check
1514 * anyway. */
1515 if ((p_blocks << sb->s_blocksize_bits) < bytes_left) {
1516 status = -EIO;
1517 mlog_errno(status);
1518 goto bail;
1519 }
1520
1521 virtual = 0;
1522 while(bytes_left > 0) {
1523 c = &symname[virtual * sb->s_blocksize];
1524
1525 bhs[virtual] = sb_getblk(sb, p_blkno);
1526 if (!bhs[virtual]) {
1527 status = -ENOMEM;
1528 mlog_errno(status);
1529 goto bail;
1530 }
1531 ocfs2_set_new_buffer_uptodate(inode, bhs[virtual]);
1532
1533 status = ocfs2_journal_access(handle, inode, bhs[virtual],
1534 OCFS2_JOURNAL_ACCESS_CREATE);
1535 if (status < 0) {
1536 mlog_errno(status);
1537 goto bail;
1538 }
1539
1540 memset(bhs[virtual]->b_data, 0, sb->s_blocksize);
1541
1542 memcpy(bhs[virtual]->b_data, c,
1543 (bytes_left > sb->s_blocksize) ? sb->s_blocksize :
1544 bytes_left);
1545
1546 status = ocfs2_journal_dirty(handle, bhs[virtual]);
1547 if (status < 0) {
1548 mlog_errno(status);
1549 goto bail;
1550 }
1551
1552 virtual++;
1553 p_blkno++;
1554 bytes_left -= sb->s_blocksize;
1555 }
1556
1557 status = 0;
1558bail:
1559
1560 if (bhs) {
1561 for(i = 0; i < blocks; i++)
1562 if (bhs[i])
1563 brelse(bhs[i]);
1564 kfree(bhs);
1565 }
1566
1567 mlog_exit(status);
1568 return status;
1569}
1570
1571static int ocfs2_symlink(struct inode *dir,
1572 struct dentry *dentry,
1573 const char *symname)
1574{
1575 int status, l, credits;
1576 u64 newsize;
1577 struct ocfs2_super *osb = NULL;
1578 struct inode *inode = NULL;
1579 struct super_block *sb;
1580 struct buffer_head *new_fe_bh = NULL;
1581 struct buffer_head *de_bh = NULL;
1582 struct buffer_head *parent_fe_bh = NULL;
1583 struct ocfs2_dinode *fe = NULL;
1584 struct ocfs2_dinode *dirfe;
1585 struct ocfs2_journal_handle *handle = NULL;
1586 struct ocfs2_alloc_context *inode_ac = NULL;
1587 struct ocfs2_alloc_context *data_ac = NULL;
1588
1589 mlog_entry("(0x%p, 0x%p, symname='%s' actual='%.*s')\n", dir,
1590 dentry, symname, dentry->d_name.len, dentry->d_name.name);
1591
1592 sb = dir->i_sb;
1593 osb = OCFS2_SB(sb);
1594
1595 l = strlen(symname) + 1;
1596
1597 credits = ocfs2_calc_symlink_credits(sb);
1598
1599 handle = ocfs2_alloc_handle(osb);
1600 if (handle == NULL) {
1601 status = -ENOMEM;
1602 mlog_errno(status);
1603 goto bail;
1604 }
1605
1606 /* lock the parent directory */
1607 status = ocfs2_meta_lock(dir, handle, &parent_fe_bh, 1);
1608 if (status < 0) {
1609 if (status != -ENOENT)
1610 mlog_errno(status);
1611 goto bail;
1612 }
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
1634 status = ocfs2_reserve_new_inode(osb, handle, &inode_ac);
1635 if (status < 0) {
1636 if (status != -ENOSPC)
1637 mlog_errno(status);
1638 goto bail;
1639 }
1640
1641 /* don't reserve bitmap space for fast symlinks. */
1642 if (l > ocfs2_fast_symlink_chars(sb)) {
1643 status = ocfs2_reserve_clusters(osb, handle, 1, &data_ac);
1644 if (status < 0) {
1645 if (status != -ENOSPC)
1646 mlog_errno(status);
1647 goto bail;
1648 }
1649 }
1650
1651 handle = ocfs2_start_trans(osb, handle, credits);
1652 if (IS_ERR(handle)) {
1653 status = PTR_ERR(handle);
1654 handle = NULL;
1655 mlog_errno(status);
1656 goto bail;
1657 }
1658
1659 status = ocfs2_mknod_locked(osb, dir, dentry,
1660 S_IFLNK | S_IRWXUGO, 0,
1661 &new_fe_bh, parent_fe_bh, handle,
1662 &inode, inode_ac);
1663 if (status < 0) {
1664 mlog_errno(status);
1665 goto bail;
1666 }
1667
1668 fe = (struct ocfs2_dinode *) new_fe_bh->b_data;
1669 inode->i_rdev = 0;
1670 newsize = l - 1;
1671 if (l > ocfs2_fast_symlink_chars(sb)) {
1672 inode->i_op = &ocfs2_symlink_inode_operations;
1673 status = ocfs2_do_extend_allocation(osb, inode, 1, new_fe_bh,
1674 handle, data_ac, NULL,
1675 NULL);
1676 if (status < 0) {
1677 if (status != -ENOSPC && status != -EINTR) {
Mark Fashehb06970532006-03-03 10:24:33 -08001678 mlog(ML_ERROR,
1679 "Failed to extend file to %llu\n",
1680 (unsigned long long)newsize);
Mark Fashehccd979b2005-12-15 14:31:24 -08001681 mlog_errno(status);
1682 status = -ENOSPC;
1683 }
1684 goto bail;
1685 }
1686 i_size_write(inode, newsize);
1687 inode->i_blocks = ocfs2_align_bytes_to_sectors(newsize);
1688 } else {
1689 inode->i_op = &ocfs2_fast_symlink_inode_operations;
1690 memcpy((char *) fe->id2.i_symlink, symname, l);
1691 i_size_write(inode, newsize);
1692 inode->i_blocks = 0;
1693 }
1694
1695 status = ocfs2_mark_inode_dirty(handle, inode, new_fe_bh);
1696 if (status < 0) {
1697 mlog_errno(status);
1698 goto bail;
1699 }
1700
1701 if (!ocfs2_inode_is_fast_symlink(inode)) {
1702 status = ocfs2_create_symlink_data(osb, handle, inode,
1703 symname);
1704 if (status < 0) {
1705 mlog_errno(status);
1706 goto bail;
1707 }
1708 }
1709
1710 status = ocfs2_add_entry(handle, dentry, inode,
1711 le64_to_cpu(fe->i_blkno), parent_fe_bh,
1712 de_bh);
1713 if (status < 0) {
1714 mlog_errno(status);
1715 goto bail;
1716 }
1717
Mark Fasheh0027dd52006-09-21 16:51:28 -07001718 status = ocfs2_dentry_attach_lock(dentry, inode, OCFS2_I(dir)->ip_blkno);
Mark Fasheh379dfe92006-09-08 14:21:03 -07001719 if (status) {
1720 mlog_errno(status);
1721 goto bail;
1722 }
1723
Mark Fashehccd979b2005-12-15 14:31:24 -08001724 insert_inode_hash(inode);
1725 dentry->d_op = &ocfs2_dentry_ops;
1726 d_instantiate(dentry, inode);
1727bail:
1728 if (handle)
1729 ocfs2_commit_trans(handle);
1730 if (new_fe_bh)
1731 brelse(new_fe_bh);
1732 if (parent_fe_bh)
1733 brelse(parent_fe_bh);
1734 if (de_bh)
1735 brelse(de_bh);
1736 if (inode_ac)
1737 ocfs2_free_alloc_context(inode_ac);
1738 if (data_ac)
1739 ocfs2_free_alloc_context(data_ac);
1740 if ((status < 0) && inode)
1741 iput(inode);
1742
1743 mlog_exit(status);
1744
1745 return status;
1746}
1747
1748int ocfs2_check_dir_entry(struct inode * dir,
1749 struct ocfs2_dir_entry * de,
1750 struct buffer_head * bh,
1751 unsigned long offset)
1752{
1753 const char *error_msg = NULL;
1754 const int rlen = le16_to_cpu(de->rec_len);
1755
1756 if (rlen < OCFS2_DIR_REC_LEN(1))
1757 error_msg = "rec_len is smaller than minimal";
1758 else if (rlen % 4 != 0)
1759 error_msg = "rec_len % 4 != 0";
1760 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
1761 error_msg = "rec_len is too small for name_len";
1762 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
1763 error_msg = "directory entry across blocks";
1764
1765 if (error_msg != NULL)
Mark Fashehb06970532006-03-03 10:24:33 -08001766 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
1767 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
1768 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
1769 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
1770 de->name_len);
Mark Fashehccd979b2005-12-15 14:31:24 -08001771 return error_msg == NULL ? 1 : 0;
1772}
1773
1774/* we don't always have a dentry for what we want to add, so people
1775 * like orphan dir can call this instead.
1776 *
1777 * If you pass me insert_bh, I'll skip the search of the other dir
1778 * blocks and put the record in there.
1779 */
1780static int __ocfs2_add_entry(struct ocfs2_journal_handle *handle,
1781 struct inode *dir,
1782 const char *name, int namelen,
1783 struct inode *inode, u64 blkno,
1784 struct buffer_head *parent_fe_bh,
1785 struct buffer_head *insert_bh)
1786{
1787 unsigned long offset;
1788 unsigned short rec_len;
1789 struct ocfs2_dir_entry *de, *de1;
1790 struct super_block *sb;
1791 int retval, status;
1792
1793 mlog_entry_void();
1794
1795 sb = dir->i_sb;
1796
1797 if (!namelen)
1798 return -EINVAL;
1799
1800 rec_len = OCFS2_DIR_REC_LEN(namelen);
1801 offset = 0;
1802 de = (struct ocfs2_dir_entry *) insert_bh->b_data;
1803 while (1) {
1804 BUG_ON((char *)de >= sb->s_blocksize + insert_bh->b_data);
1805 /* These checks should've already been passed by the
1806 * prepare function, but I guess we can leave them
1807 * here anyway. */
1808 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1809 retval = -ENOENT;
1810 goto bail;
1811 }
1812 if (ocfs2_match(namelen, name, de)) {
1813 retval = -EEXIST;
1814 goto bail;
1815 }
1816 if (((le64_to_cpu(de->inode) == 0) &&
1817 (le16_to_cpu(de->rec_len) >= rec_len)) ||
1818 (le16_to_cpu(de->rec_len) >=
1819 (OCFS2_DIR_REC_LEN(de->name_len) + rec_len))) {
1820 status = ocfs2_journal_access(handle, dir, insert_bh,
1821 OCFS2_JOURNAL_ACCESS_WRITE);
1822 /* By now the buffer is marked for journaling */
1823 offset += le16_to_cpu(de->rec_len);
1824 if (le64_to_cpu(de->inode)) {
1825 de1 = (struct ocfs2_dir_entry *)((char *) de +
1826 OCFS2_DIR_REC_LEN(de->name_len));
1827 de1->rec_len =
1828 cpu_to_le16(le16_to_cpu(de->rec_len) -
1829 OCFS2_DIR_REC_LEN(de->name_len));
1830 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1831 de = de1;
1832 }
1833 de->file_type = OCFS2_FT_UNKNOWN;
1834 if (blkno) {
1835 de->inode = cpu_to_le64(blkno);
1836 ocfs2_set_de_type(de, inode->i_mode);
1837 } else
1838 de->inode = 0;
1839 de->name_len = namelen;
1840 memcpy(de->name, name, namelen);
1841
1842 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1843 dir->i_version++;
1844 status = ocfs2_journal_dirty(handle, insert_bh);
1845 retval = 0;
1846 goto bail;
1847 }
1848 offset += le16_to_cpu(de->rec_len);
1849 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1850 }
1851
1852 /* when you think about it, the assert above should prevent us
1853 * from ever getting here. */
1854 retval = -ENOSPC;
1855bail:
1856
1857 mlog_exit(retval);
1858 return retval;
1859}
1860
1861
1862/*
1863 * ocfs2_delete_entry deletes a directory entry by merging it with the
1864 * previous entry
1865 */
1866static int ocfs2_delete_entry(struct ocfs2_journal_handle *handle,
1867 struct inode *dir,
1868 struct ocfs2_dir_entry *de_del,
1869 struct buffer_head *bh)
1870{
1871 struct ocfs2_dir_entry *de, *pde;
1872 int i, status = -ENOENT;
1873
1874 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1875
1876 i = 0;
1877 pde = NULL;
1878 de = (struct ocfs2_dir_entry *) bh->b_data;
1879 while (i < bh->b_size) {
1880 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1881 status = -EIO;
1882 mlog_errno(status);
1883 goto bail;
1884 }
1885 if (de == de_del) {
1886 status = ocfs2_journal_access(handle, dir, bh,
1887 OCFS2_JOURNAL_ACCESS_WRITE);
1888 if (status < 0) {
1889 status = -EIO;
1890 mlog_errno(status);
1891 goto bail;
1892 }
1893 if (pde)
1894 pde->rec_len =
1895 cpu_to_le16(le16_to_cpu(pde->rec_len) +
1896 le16_to_cpu(de->rec_len));
1897 else
1898 de->inode = 0;
1899 dir->i_version++;
1900 status = ocfs2_journal_dirty(handle, bh);
1901 goto bail;
1902 }
1903 i += le16_to_cpu(de->rec_len);
1904 pde = de;
1905 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1906 }
1907bail:
1908 mlog_exit(status);
1909 return status;
1910}
1911
1912/*
1913 * Returns 0 if not found, -1 on failure, and 1 on success
1914 */
1915static int inline ocfs2_search_dirblock(struct buffer_head *bh,
1916 struct inode *dir,
1917 const char *name, int namelen,
1918 unsigned long offset,
1919 struct ocfs2_dir_entry **res_dir)
1920{
1921 struct ocfs2_dir_entry *de;
1922 char *dlimit, *de_buf;
1923 int de_len;
1924 int ret = 0;
1925
1926 mlog_entry_void();
1927
1928 de_buf = bh->b_data;
1929 dlimit = de_buf + dir->i_sb->s_blocksize;
1930
1931 while (de_buf < dlimit) {
1932 /* this code is executed quadratically often */
1933 /* do minimal checking `by hand' */
1934
1935 de = (struct ocfs2_dir_entry *) de_buf;
1936
1937 if (de_buf + namelen <= dlimit &&
1938 ocfs2_match(namelen, name, de)) {
1939 /* found a match - just to be sure, do a full check */
1940 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
1941 ret = -1;
1942 goto bail;
1943 }
1944 *res_dir = de;
1945 ret = 1;
1946 goto bail;
1947 }
1948
1949 /* prevent looping on a bad block */
1950 de_len = le16_to_cpu(de->rec_len);
1951 if (de_len <= 0) {
1952 ret = -1;
1953 goto bail;
1954 }
1955
1956 de_buf += de_len;
1957 offset += de_len;
1958 }
1959
1960bail:
1961 mlog_exit(ret);
1962 return ret;
1963}
1964
1965struct buffer_head *ocfs2_find_entry(const char *name, int namelen,
1966 struct inode *dir,
1967 struct ocfs2_dir_entry **res_dir)
1968{
1969 struct super_block *sb;
1970 struct buffer_head *bh_use[NAMEI_RA_SIZE];
1971 struct buffer_head *bh, *ret = NULL;
1972 unsigned long start, block, b;
1973 int ra_max = 0; /* Number of bh's in the readahead
1974 buffer, bh_use[] */
1975 int ra_ptr = 0; /* Current index into readahead
1976 buffer */
1977 int num = 0;
1978 int nblocks, i, err;
1979
1980 mlog_entry_void();
1981
1982 *res_dir = NULL;
1983 sb = dir->i_sb;
1984
1985 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
1986 start = OCFS2_I(dir)->ip_dir_start_lookup;
1987 if (start >= nblocks)
1988 start = 0;
1989 block = start;
1990
1991restart:
1992 do {
1993 /*
1994 * We deal with the read-ahead logic here.
1995 */
1996 if (ra_ptr >= ra_max) {
1997 /* Refill the readahead buffer */
1998 ra_ptr = 0;
1999 b = block;
2000 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
2001 /*
2002 * Terminate if we reach the end of the
2003 * directory and must wrap, or if our
2004 * search has finished at this block.
2005 */
2006 if (b >= nblocks || (num && block == start)) {
2007 bh_use[ra_max] = NULL;
2008 break;
2009 }
2010 num++;
2011
Mark Fashehccd979b2005-12-15 14:31:24 -08002012 bh = ocfs2_bread(dir, b++, &err, 1);
2013 bh_use[ra_max] = bh;
Mark Fashehccd979b2005-12-15 14:31:24 -08002014 }
2015 }
2016 if ((bh = bh_use[ra_ptr++]) == NULL)
2017 goto next;
2018 wait_on_buffer(bh);
2019 if (!buffer_uptodate(bh)) {
2020 /* read error, skip block & hope for the best */
Mark Fashehaa958872006-04-21 13:49:02 -07002021 ocfs2_error(dir->i_sb, "reading directory %llu, "
2022 "offset %lu\n",
2023 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2024 block);
Mark Fashehccd979b2005-12-15 14:31:24 -08002025 brelse(bh);
2026 goto next;
2027 }
2028 i = ocfs2_search_dirblock(bh, dir, name, namelen,
2029 block << sb->s_blocksize_bits,
2030 res_dir);
2031 if (i == 1) {
2032 OCFS2_I(dir)->ip_dir_start_lookup = block;
2033 ret = bh;
2034 goto cleanup_and_exit;
2035 } else {
2036 brelse(bh);
2037 if (i < 0)
2038 goto cleanup_and_exit;
2039 }
2040 next:
2041 if (++block >= nblocks)
2042 block = 0;
2043 } while (block != start);
2044
2045 /*
2046 * If the directory has grown while we were searching, then
2047 * search the last part of the directory before giving up.
2048 */
2049 block = nblocks;
2050 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
2051 if (block < nblocks) {
2052 start = 0;
2053 goto restart;
2054 }
2055
2056cleanup_and_exit:
2057 /* Clean up the read-ahead blocks */
2058 for (; ra_ptr < ra_max; ra_ptr++)
2059 brelse(bh_use[ra_ptr]);
2060
2061 mlog_exit_ptr(ret);
2062 return ret;
2063}
2064
2065static int ocfs2_blkno_stringify(u64 blkno, char *name)
2066{
2067 int status, namelen;
2068
2069 mlog_entry_void();
2070
Mark Fashehb06970532006-03-03 10:24:33 -08002071 namelen = snprintf(name, OCFS2_ORPHAN_NAMELEN + 1, "%016llx",
2072 (long long)blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002073 if (namelen <= 0) {
2074 if (namelen)
2075 status = namelen;
2076 else
2077 status = -EINVAL;
2078 mlog_errno(status);
2079 goto bail;
2080 }
2081 if (namelen != OCFS2_ORPHAN_NAMELEN) {
2082 status = -EINVAL;
2083 mlog_errno(status);
2084 goto bail;
2085 }
2086
2087 mlog(0, "built filename '%s' for orphan dir (len=%d)\n", name,
2088 namelen);
2089
2090 status = 0;
2091bail:
2092 mlog_exit(status);
2093 return status;
2094}
2095
2096static int ocfs2_prepare_orphan_dir(struct ocfs2_super *osb,
2097 struct ocfs2_journal_handle *handle,
2098 struct inode *inode,
2099 char *name,
2100 struct buffer_head **de_bh)
2101{
2102 struct inode *orphan_dir_inode = NULL;
2103 struct buffer_head *orphan_dir_bh = NULL;
2104 int status = 0;
2105
2106 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2107 if (status < 0) {
2108 mlog_errno(status);
2109 goto leave;
2110 }
2111
2112 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2113 ORPHAN_DIR_SYSTEM_INODE,
2114 osb->slot_num);
2115 if (!orphan_dir_inode) {
2116 status = -ENOENT;
2117 mlog_errno(status);
2118 goto leave;
2119 }
2120
2121 ocfs2_handle_add_inode(handle, orphan_dir_inode);
2122 status = ocfs2_meta_lock(orphan_dir_inode, handle, &orphan_dir_bh, 1);
2123 if (status < 0) {
2124 mlog_errno(status);
2125 goto leave;
2126 }
2127
2128 status = ocfs2_prepare_dir_for_insert(osb, orphan_dir_inode,
2129 orphan_dir_bh, name,
2130 OCFS2_ORPHAN_NAMELEN, de_bh);
2131 if (status < 0) {
2132 mlog_errno(status);
2133 goto leave;
2134 }
2135
2136leave:
2137 if (orphan_dir_inode)
2138 iput(orphan_dir_inode);
2139
2140 if (orphan_dir_bh)
2141 brelse(orphan_dir_bh);
2142
2143 mlog_exit(status);
2144 return status;
2145}
2146
2147static int ocfs2_orphan_add(struct ocfs2_super *osb,
2148 struct ocfs2_journal_handle *handle,
2149 struct inode *inode,
2150 struct ocfs2_dinode *fe,
2151 char *name,
2152 struct buffer_head *de_bh)
2153{
2154 struct inode *orphan_dir_inode = NULL;
2155 struct buffer_head *orphan_dir_bh = NULL;
2156 int status = 0;
2157 struct ocfs2_dinode *orphan_fe;
2158
2159 mlog_entry("(inode->i_ino = %lu)\n", inode->i_ino);
2160
2161 orphan_dir_inode = ocfs2_get_system_file_inode(osb,
2162 ORPHAN_DIR_SYSTEM_INODE,
2163 osb->slot_num);
2164 if (!orphan_dir_inode) {
2165 status = -ENOENT;
2166 mlog_errno(status);
2167 goto leave;
2168 }
2169
2170 status = ocfs2_read_block(osb,
2171 OCFS2_I(orphan_dir_inode)->ip_blkno,
2172 &orphan_dir_bh, OCFS2_BH_CACHED,
2173 orphan_dir_inode);
2174 if (status < 0) {
2175 mlog_errno(status);
2176 goto leave;
2177 }
2178
2179 status = ocfs2_journal_access(handle, orphan_dir_inode, orphan_dir_bh,
2180 OCFS2_JOURNAL_ACCESS_WRITE);
2181 if (status < 0) {
2182 mlog_errno(status);
2183 goto leave;
2184 }
2185
2186 /* we're a cluster, and nlink can change on disk from
2187 * underneath us... */
2188 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2189 if (S_ISDIR(inode->i_mode))
2190 le16_add_cpu(&orphan_fe->i_links_count, 1);
2191 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2192
2193 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2194 if (status < 0) {
2195 mlog_errno(status);
2196 goto leave;
2197 }
2198
2199 status = __ocfs2_add_entry(handle, orphan_dir_inode, name,
2200 OCFS2_ORPHAN_NAMELEN, inode,
2201 OCFS2_I(inode)->ip_blkno,
2202 orphan_dir_bh, de_bh);
2203 if (status < 0) {
2204 mlog_errno(status);
2205 goto leave;
2206 }
2207
2208 le32_add_cpu(&fe->i_flags, OCFS2_ORPHANED_FL);
2209
2210 /* Record which orphan dir our inode now resides
2211 * in. delete_inode will use this to determine which orphan
2212 * dir to lock. */
2213 spin_lock(&OCFS2_I(inode)->ip_lock);
2214 OCFS2_I(inode)->ip_orphaned_slot = osb->slot_num;
2215 spin_unlock(&OCFS2_I(inode)->ip_lock);
2216
Mark Fashehb06970532006-03-03 10:24:33 -08002217 mlog(0, "Inode %llu orphaned in slot %d\n",
2218 (unsigned long long)OCFS2_I(inode)->ip_blkno, osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08002219
2220leave:
2221 if (orphan_dir_inode)
2222 iput(orphan_dir_inode);
2223
2224 if (orphan_dir_bh)
2225 brelse(orphan_dir_bh);
2226
2227 mlog_exit(status);
2228 return status;
2229}
2230
2231/* unlike orphan_add, we expect the orphan dir to already be locked here. */
2232int ocfs2_orphan_del(struct ocfs2_super *osb,
2233 struct ocfs2_journal_handle *handle,
2234 struct inode *orphan_dir_inode,
2235 struct inode *inode,
2236 struct buffer_head *orphan_dir_bh)
2237{
2238 char name[OCFS2_ORPHAN_NAMELEN + 1];
2239 struct ocfs2_dinode *orphan_fe;
2240 int status = 0;
2241 struct buffer_head *target_de_bh = NULL;
2242 struct ocfs2_dir_entry *target_de = NULL;
2243
2244 mlog_entry_void();
2245
2246 status = ocfs2_blkno_stringify(OCFS2_I(inode)->ip_blkno, name);
2247 if (status < 0) {
2248 mlog_errno(status);
2249 goto leave;
2250 }
2251
Mark Fashehb06970532006-03-03 10:24:33 -08002252 mlog(0, "removing '%s' from orphan dir %llu (namelen=%d)\n",
2253 name, (unsigned long long)OCFS2_I(orphan_dir_inode)->ip_blkno,
2254 OCFS2_ORPHAN_NAMELEN);
Mark Fashehccd979b2005-12-15 14:31:24 -08002255
2256 /* find it's spot in the orphan directory */
2257 target_de_bh = ocfs2_find_entry(name, OCFS2_ORPHAN_NAMELEN,
2258 orphan_dir_inode, &target_de);
2259 if (!target_de_bh) {
2260 status = -ENOENT;
2261 mlog_errno(status);
2262 goto leave;
2263 }
2264
2265 /* remove it from the orphan directory */
2266 status = ocfs2_delete_entry(handle, orphan_dir_inode, target_de,
2267 target_de_bh);
2268 if (status < 0) {
2269 mlog_errno(status);
2270 goto leave;
2271 }
2272
2273 status = ocfs2_journal_access(handle,orphan_dir_inode, orphan_dir_bh,
2274 OCFS2_JOURNAL_ACCESS_WRITE);
2275 if (status < 0) {
2276 mlog_errno(status);
2277 goto leave;
2278 }
2279
2280 /* do the i_nlink dance! :) */
2281 orphan_fe = (struct ocfs2_dinode *) orphan_dir_bh->b_data;
2282 if (S_ISDIR(inode->i_mode))
2283 le16_add_cpu(&orphan_fe->i_links_count, -1);
2284 orphan_dir_inode->i_nlink = le16_to_cpu(orphan_fe->i_links_count);
2285
2286 status = ocfs2_journal_dirty(handle, orphan_dir_bh);
2287 if (status < 0) {
2288 mlog_errno(status);
2289 goto leave;
2290 }
2291
2292leave:
2293 if (target_de_bh)
2294 brelse(target_de_bh);
2295
2296 mlog_exit(status);
2297 return status;
2298}
2299
2300struct inode_operations ocfs2_dir_iops = {
2301 .create = ocfs2_create,
2302 .lookup = ocfs2_lookup,
2303 .link = ocfs2_link,
2304 .unlink = ocfs2_unlink,
2305 .rmdir = ocfs2_unlink,
2306 .symlink = ocfs2_symlink,
2307 .mkdir = ocfs2_mkdir,
2308 .mknod = ocfs2_mknod,
2309 .rename = ocfs2_rename,
2310 .setattr = ocfs2_setattr,
2311 .getattr = ocfs2_getattr,
2312};