blob: 47de64988b1182f2d57ff48ee906dcf629b2b65c [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 * dir.c
5 *
6 * Creates, reads, walks and deletes directory-nodes
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>
Jan Karaa90714c2008-10-09 19:38:40 +020043#include <linux/quotaops.h>
Mark Fasheh9b7895e2008-11-12 16:27:44 -080044#include <linux/sort.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080045
46#define MLOG_MASK_PREFIX ML_NAMEI
47#include <cluster/masklog.h>
48
49#include "ocfs2.h"
50
51#include "alloc.h"
Joel Beckerc175a512008-12-10 17:58:22 -080052#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053#include "dir.h"
54#include "dlmglue.h"
55#include "extent_map.h"
56#include "file.h"
57#include "inode.h"
58#include "journal.h"
59#include "namei.h"
60#include "suballoc.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070061#include "super.h"
Mark Fasheh9b7895e2008-11-12 16:27:44 -080062#include "sysfile.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080063#include "uptodate.h"
64
65#include "buffer_head_io.h"
66
Mark Fasheh316f4b92007-09-07 18:21:26 -070067#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
Mark Fashehccd979b2005-12-15 14:31:24 -080072static unsigned char ocfs2_filetype_table[] = {
73 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
74};
75
Mark Fasheh316f4b92007-09-07 18:21:26 -070076static int ocfs2_do_extend_dir(struct super_block *sb,
77 handle_t *handle,
78 struct inode *dir,
79 struct buffer_head *parent_fe_bh,
80 struct ocfs2_alloc_context *data_ac,
81 struct ocfs2_alloc_context *meta_ac,
82 struct buffer_head **new_bh);
83
Mark Fasheh23193e52007-09-12 13:01:18 -070084/*
Mark Fasheh87d35a72008-12-10 17:36:25 -080085 * These are distinct checks because future versions of the file system will
86 * want to have a trailing dirent structure independent of indexing.
87 */
88static int ocfs2_dir_has_trailer(struct inode *dir)
89{
90 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
91 return 0;
92
93 return ocfs2_meta_ecc(OCFS2_SB(dir->i_sb));
94}
95
96static int ocfs2_supports_dir_trailer(struct ocfs2_super *osb)
97{
98 return ocfs2_meta_ecc(osb);
99}
100
101static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
102{
103 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
104}
105
106#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
107
Joel Beckerc175a512008-12-10 17:58:22 -0800108/* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
109 * them more consistent? */
110struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
111 void *data)
112{
113 char *p = data;
114
115 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
116 return (struct ocfs2_dir_block_trailer *)p;
117}
118
Mark Fasheh87d35a72008-12-10 17:36:25 -0800119/*
120 * XXX: This is executed once on every dirent. We should consider optimizing
121 * it.
122 */
123static int ocfs2_skip_dir_trailer(struct inode *dir,
124 struct ocfs2_dir_entry *de,
125 unsigned long offset,
126 unsigned long blklen)
127{
128 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
129
130 if (!ocfs2_dir_has_trailer(dir))
131 return 0;
132
133 if (offset != toff)
134 return 0;
135
136 return 1;
137}
138
139static void ocfs2_init_dir_trailer(struct inode *inode,
140 struct buffer_head *bh)
141{
142 struct ocfs2_dir_block_trailer *trailer;
143
144 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
145 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
146 trailer->db_compat_rec_len =
147 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
148 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
149 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
150}
151
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800152void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
153{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800154 brelse(res->dl_dx_root_bh);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800155 brelse(res->dl_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800156 brelse(res->dl_dx_leaf_bh);
157}
158
159static int ocfs2_dir_indexed(struct inode *inode)
160{
161 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
162 return 1;
163 return 0;
164}
165
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800166static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
167{
168 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
169}
170
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800171/*
172 * Hashing code adapted from ext3
173 */
174#define DELTA 0x9E3779B9
175
176static void TEA_transform(__u32 buf[4], __u32 const in[])
177{
178 __u32 sum = 0;
179 __u32 b0 = buf[0], b1 = buf[1];
180 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
181 int n = 16;
182
183 do {
184 sum += DELTA;
185 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
186 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
187 } while (--n);
188
189 buf[0] += b0;
190 buf[1] += b1;
191}
192
193static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
194{
195 __u32 pad, val;
196 int i;
197
198 pad = (__u32)len | ((__u32)len << 8);
199 pad |= pad << 16;
200
201 val = pad;
202 if (len > num*4)
203 len = num * 4;
204 for (i = 0; i < len; i++) {
205 if ((i % 4) == 0)
206 val = pad;
207 val = msg[i] + (val << 8);
208 if ((i % 4) == 3) {
209 *buf++ = val;
210 val = pad;
211 num--;
212 }
213 }
214 if (--num >= 0)
215 *buf++ = val;
216 while (--num >= 0)
217 *buf++ = pad;
218}
219
220static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
221 struct ocfs2_dx_hinfo *hinfo)
222{
223 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
224 const char *p;
225 __u32 in[8], buf[4];
226
227 /*
228 * XXX: Is this really necessary, if the index is never looked
229 * at by readdir? Is a hash value of '0' a bad idea?
230 */
231 if ((len == 1 && !strncmp(".", name, 1)) ||
232 (len == 2 && !strncmp("..", name, 2))) {
233 buf[0] = buf[1] = 0;
234 goto out;
235 }
236
237#ifdef OCFS2_DEBUG_DX_DIRS
238 /*
239 * This makes it very easy to debug indexing problems. We
240 * should never allow this to be selected without hand editing
241 * this file though.
242 */
243 buf[0] = buf[1] = len;
244 goto out;
245#endif
246
247 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
248
249 p = name;
250 while (len > 0) {
251 str2hashbuf(p, len, in, 4);
252 TEA_transform(buf, in);
253 len -= 16;
254 p += 16;
255 }
256
257out:
258 hinfo->major_hash = buf[0];
259 hinfo->minor_hash = buf[1];
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800260}
261
Mark Fasheh87d35a72008-12-10 17:36:25 -0800262/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700263 * bh passed here can be an inode block or a dir data block, depending
264 * on the inode inline data flag.
265 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700266static int ocfs2_check_dir_entry(struct inode * dir,
267 struct ocfs2_dir_entry * de,
268 struct buffer_head * bh,
269 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700270{
271 const char *error_msg = NULL;
272 const int rlen = le16_to_cpu(de->rec_len);
273
274 if (rlen < OCFS2_DIR_REC_LEN(1))
275 error_msg = "rec_len is smaller than minimal";
276 else if (rlen % 4 != 0)
277 error_msg = "rec_len % 4 != 0";
278 else if (rlen < OCFS2_DIR_REC_LEN(de->name_len))
279 error_msg = "rec_len is too small for name_len";
280 else if (((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize)
281 error_msg = "directory entry across blocks";
282
283 if (error_msg != NULL)
284 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
285 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
286 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
287 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
288 de->name_len);
289 return error_msg == NULL ? 1 : 0;
290}
291
292static inline int ocfs2_match(int len,
293 const char * const name,
294 struct ocfs2_dir_entry *de)
295{
296 if (len != de->name_len)
297 return 0;
298 if (!de->inode)
299 return 0;
300 return !memcmp(name, de->name, len);
301}
302
303/*
304 * Returns 0 if not found, -1 on failure, and 1 on success
305 */
306static int inline ocfs2_search_dirblock(struct buffer_head *bh,
307 struct inode *dir,
308 const char *name, int namelen,
309 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700310 char *first_de,
311 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700312 struct ocfs2_dir_entry **res_dir)
313{
314 struct ocfs2_dir_entry *de;
315 char *dlimit, *de_buf;
316 int de_len;
317 int ret = 0;
318
319 mlog_entry_void();
320
Mark Fasheh23193e52007-09-12 13:01:18 -0700321 de_buf = first_de;
322 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700323
324 while (de_buf < dlimit) {
325 /* this code is executed quadratically often */
326 /* do minimal checking `by hand' */
327
328 de = (struct ocfs2_dir_entry *) de_buf;
329
330 if (de_buf + namelen <= dlimit &&
331 ocfs2_match(namelen, name, de)) {
332 /* found a match - just to be sure, do a full check */
333 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
334 ret = -1;
335 goto bail;
336 }
337 *res_dir = de;
338 ret = 1;
339 goto bail;
340 }
341
342 /* prevent looping on a bad block */
343 de_len = le16_to_cpu(de->rec_len);
344 if (de_len <= 0) {
345 ret = -1;
346 goto bail;
347 }
348
349 de_buf += de_len;
350 offset += de_len;
351 }
352
353bail:
354 mlog_exit(ret);
355 return ret;
356}
357
Mark Fasheh23193e52007-09-12 13:01:18 -0700358static struct buffer_head *ocfs2_find_entry_id(const char *name,
359 int namelen,
360 struct inode *dir,
361 struct ocfs2_dir_entry **res_dir)
362{
363 int ret, found;
364 struct buffer_head *di_bh = NULL;
365 struct ocfs2_dinode *di;
366 struct ocfs2_inline_data *data;
367
Joel Beckerb657c952008-11-13 14:49:11 -0800368 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700369 if (ret) {
370 mlog_errno(ret);
371 goto out;
372 }
373
374 di = (struct ocfs2_dinode *)di_bh->b_data;
375 data = &di->id2.i_data;
376
377 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
378 data->id_data, i_size_read(dir), res_dir);
379 if (found == 1)
380 return di_bh;
381
382 brelse(di_bh);
383out:
384 return NULL;
385}
386
Joel Beckera22305c2008-11-13 14:49:17 -0800387static int ocfs2_validate_dir_block(struct super_block *sb,
388 struct buffer_head *bh)
389{
Joel Beckerc175a512008-12-10 17:58:22 -0800390 int rc;
391 struct ocfs2_dir_block_trailer *trailer =
392 ocfs2_trailer_from_bh(bh, sb);
393
394
Joel Beckera22305c2008-11-13 14:49:17 -0800395 /*
Joel Beckerc175a512008-12-10 17:58:22 -0800396 * We don't validate dirents here, that's handled
Joel Beckera22305c2008-11-13 14:49:17 -0800397 * in-place when the code walks them.
398 */
Joel Becker970e4932008-11-13 14:49:19 -0800399 mlog(0, "Validating dirblock %llu\n",
400 (unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800401
Joel Beckerc175a512008-12-10 17:58:22 -0800402 BUG_ON(!buffer_uptodate(bh));
403
404 /*
405 * If the ecc fails, we return the error but otherwise
406 * leave the filesystem running. We know any error is
407 * local to this block.
408 *
409 * Note that we are safe to call this even if the directory
410 * doesn't have a trailer. Filesystems without metaecc will do
411 * nothing, and filesystems with it will have one.
412 */
413 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
414 if (rc)
415 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
416 (unsigned long long)bh->b_blocknr);
417
418 return rc;
Joel Beckera22305c2008-11-13 14:49:17 -0800419}
420
421/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800422 * Validate a directory trailer.
423 *
424 * We check the trailer here rather than in ocfs2_validate_dir_block()
425 * because that function doesn't have the inode to test.
426 */
427static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
428{
429 int rc = 0;
430 struct ocfs2_dir_block_trailer *trailer;
431
432 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
433 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
434 rc = -EINVAL;
435 ocfs2_error(dir->i_sb,
436 "Invalid dirblock #%llu: "
437 "signature = %.*s\n",
438 (unsigned long long)bh->b_blocknr, 7,
439 trailer->db_signature);
440 goto out;
441 }
442 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
443 rc = -EINVAL;
444 ocfs2_error(dir->i_sb,
445 "Directory block #%llu has an invalid "
446 "db_blkno of %llu",
447 (unsigned long long)bh->b_blocknr,
448 (unsigned long long)le64_to_cpu(trailer->db_blkno));
449 goto out;
450 }
451 if (le64_to_cpu(trailer->db_parent_dinode) !=
452 OCFS2_I(dir)->ip_blkno) {
453 rc = -EINVAL;
454 ocfs2_error(dir->i_sb,
455 "Directory block #%llu on dinode "
456 "#%llu has an invalid parent_dinode "
457 "of %llu",
458 (unsigned long long)bh->b_blocknr,
459 (unsigned long long)OCFS2_I(dir)->ip_blkno,
460 (unsigned long long)le64_to_cpu(trailer->db_blkno));
461 goto out;
462 }
463out:
464 return rc;
465}
466
467/*
Joel Beckera22305c2008-11-13 14:49:17 -0800468 * This function forces all errors to -EIO for consistency with its
469 * predecessor, ocfs2_bread(). We haven't audited what returning the
470 * real error codes would do to callers. We log the real codes with
471 * mlog_errno() before we squash them.
472 */
473static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
474 struct buffer_head **bh, int flags)
475{
476 int rc = 0;
477 struct buffer_head *tmp = *bh;
Joel Beckera22305c2008-11-13 14:49:17 -0800478
Joel Becker511308d2008-11-13 14:49:21 -0800479 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
480 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800481 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800482 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800483 goto out;
484 }
485
Mark Fasheh87d35a72008-12-10 17:36:25 -0800486 if (!(flags & OCFS2_BH_READAHEAD) &&
487 ocfs2_dir_has_trailer(inode)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800488 rc = ocfs2_check_dir_trailer(inode, tmp);
489 if (rc) {
490 if (!*bh)
491 brelse(tmp);
492 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800493 goto out;
494 }
495 }
Joel Beckera22305c2008-11-13 14:49:17 -0800496
Joel Becker511308d2008-11-13 14:49:21 -0800497 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800498 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800499 *bh = tmp;
500
Mark Fasheh87d35a72008-12-10 17:36:25 -0800501out:
Joel Beckera22305c2008-11-13 14:49:17 -0800502 return rc ? -EIO : 0;
503}
504
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800505/*
506 * Read the block at 'phys' which belongs to this directory
507 * inode. This function does no virtual->physical block translation -
508 * what's passed in is assumed to be a valid directory block.
509 */
510static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
511 struct buffer_head **bh)
512{
513 int ret;
514 struct buffer_head *tmp = *bh;
515
516 ret = ocfs2_read_block(dir, phys, &tmp, ocfs2_validate_dir_block);
517 if (ret) {
518 mlog_errno(ret);
519 goto out;
520 }
521
522 if (ocfs2_supports_dir_trailer(dir)) {
523 ret = ocfs2_check_dir_trailer(dir, tmp);
524 if (ret) {
525 if (!*bh)
526 brelse(tmp);
527 mlog_errno(ret);
528 goto out;
529 }
530 }
531
532 if (!ret && !*bh)
533 *bh = tmp;
534out:
535 return ret;
536}
537
538static int ocfs2_validate_dx_root(struct super_block *sb,
539 struct buffer_head *bh)
540{
541 int ret;
542 struct ocfs2_dx_root_block *dx_root;
543
544 BUG_ON(!buffer_uptodate(bh));
545
546 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
547
548 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
549 if (ret) {
550 mlog(ML_ERROR,
551 "Checksum failed for dir index root block %llu\n",
552 (unsigned long long)bh->b_blocknr);
553 return ret;
554 }
555
556 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
557 ocfs2_error(sb,
558 "Dir Index Root # %llu has bad signature %.*s",
559 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
560 7, dx_root->dr_signature);
561 return -EINVAL;
562 }
563
564 return 0;
565}
566
567static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
568 struct buffer_head **dx_root_bh)
569{
570 int ret;
571 u64 blkno = le64_to_cpu(di->i_dx_root);
572 struct buffer_head *tmp = *dx_root_bh;
573
574 ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_root);
575
576 /* If ocfs2_read_block() got us a new bh, pass it up. */
577 if (!ret && !*dx_root_bh)
578 *dx_root_bh = tmp;
579
580 return ret;
581}
582
583static int ocfs2_validate_dx_leaf(struct super_block *sb,
584 struct buffer_head *bh)
585{
586 int ret;
587 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
588
589 BUG_ON(!buffer_uptodate(bh));
590
591 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
592 if (ret) {
593 mlog(ML_ERROR,
594 "Checksum failed for dir index leaf block %llu\n",
595 (unsigned long long)bh->b_blocknr);
596 return ret;
597 }
598
599 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
600 ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
601 7, dx_leaf->dl_signature);
602 return -EROFS;
603 }
604
605 return 0;
606}
607
608static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
609 struct buffer_head **dx_leaf_bh)
610{
611 int ret;
612 struct buffer_head *tmp = *dx_leaf_bh;
613
614 ret = ocfs2_read_block(dir, blkno, &tmp, ocfs2_validate_dx_leaf);
615
616 /* If ocfs2_read_block() got us a new bh, pass it up. */
617 if (!ret && !*dx_leaf_bh)
618 *dx_leaf_bh = tmp;
619
620 return ret;
621}
622
623/*
624 * Read a series of dx_leaf blocks. This expects all buffer_head
625 * pointers to be NULL on function entry.
626 */
627static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
628 struct buffer_head **dx_leaf_bhs)
629{
630 int ret;
631
632 ret = ocfs2_read_blocks(dir, start, num, dx_leaf_bhs, 0,
633 ocfs2_validate_dx_leaf);
634 if (ret)
635 mlog_errno(ret);
636
637 return ret;
638}
639
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200640static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
641 struct inode *dir,
642 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700643{
644 struct super_block *sb;
645 struct buffer_head *bh_use[NAMEI_RA_SIZE];
646 struct buffer_head *bh, *ret = NULL;
647 unsigned long start, block, b;
648 int ra_max = 0; /* Number of bh's in the readahead
649 buffer, bh_use[] */
650 int ra_ptr = 0; /* Current index into readahead
651 buffer */
652 int num = 0;
653 int nblocks, i, err;
654
655 mlog_entry_void();
656
Mark Fasheh316f4b92007-09-07 18:21:26 -0700657 sb = dir->i_sb;
658
659 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
660 start = OCFS2_I(dir)->ip_dir_start_lookup;
661 if (start >= nblocks)
662 start = 0;
663 block = start;
664
665restart:
666 do {
667 /*
668 * We deal with the read-ahead logic here.
669 */
670 if (ra_ptr >= ra_max) {
671 /* Refill the readahead buffer */
672 ra_ptr = 0;
673 b = block;
674 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
675 /*
676 * Terminate if we reach the end of the
677 * directory and must wrap, or if our
678 * search has finished at this block.
679 */
680 if (b >= nblocks || (num && block == start)) {
681 bh_use[ra_max] = NULL;
682 break;
683 }
684 num++;
685
Joel Beckera22305c2008-11-13 14:49:17 -0800686 bh = NULL;
687 err = ocfs2_read_dir_block(dir, b++, &bh,
688 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700689 bh_use[ra_max] = bh;
690 }
691 }
692 if ((bh = bh_use[ra_ptr++]) == NULL)
693 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800694 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700695 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800696 * ocfs2_read_dir_block() has released the bh. */
Mark Fasheh316f4b92007-09-07 18:21:26 -0700697 ocfs2_error(dir->i_sb, "reading directory %llu, "
698 "offset %lu\n",
699 (unsigned long long)OCFS2_I(dir)->ip_blkno,
700 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700701 goto next;
702 }
703 i = ocfs2_search_dirblock(bh, dir, name, namelen,
704 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700705 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700706 res_dir);
707 if (i == 1) {
708 OCFS2_I(dir)->ip_dir_start_lookup = block;
709 ret = bh;
710 goto cleanup_and_exit;
711 } else {
712 brelse(bh);
713 if (i < 0)
714 goto cleanup_and_exit;
715 }
716 next:
717 if (++block >= nblocks)
718 block = 0;
719 } while (block != start);
720
721 /*
722 * If the directory has grown while we were searching, then
723 * search the last part of the directory before giving up.
724 */
725 block = nblocks;
726 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
727 if (block < nblocks) {
728 start = 0;
729 goto restart;
730 }
731
732cleanup_and_exit:
733 /* Clean up the read-ahead blocks */
734 for (; ra_ptr < ra_max; ra_ptr++)
735 brelse(bh_use[ra_ptr]);
736
737 mlog_exit_ptr(ret);
738 return ret;
739}
740
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800741static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
742 struct ocfs2_extent_list *el,
743 u32 major_hash,
744 u32 *ret_cpos,
745 u64 *ret_phys_blkno,
746 unsigned int *ret_clen)
747{
748 int ret = 0, i, found;
749 struct buffer_head *eb_bh = NULL;
750 struct ocfs2_extent_block *eb;
751 struct ocfs2_extent_rec *rec = NULL;
752
753 if (el->l_tree_depth) {
754 ret = ocfs2_find_leaf(inode, el, major_hash, &eb_bh);
755 if (ret) {
756 mlog_errno(ret);
757 goto out;
758 }
759
760 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
761 el = &eb->h_list;
762
763 if (el->l_tree_depth) {
764 ocfs2_error(inode->i_sb,
765 "Inode %lu has non zero tree depth in "
766 "btree tree block %llu\n", inode->i_ino,
767 (unsigned long long)eb_bh->b_blocknr);
768 ret = -EROFS;
769 goto out;
770 }
771 }
772
773 found = 0;
774 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
775 rec = &el->l_recs[i];
776
777 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
778 found = 1;
779 break;
780 }
781 }
782
783 if (!found) {
784 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
785 "record (%u, %u, 0) in btree", inode->i_ino,
786 le32_to_cpu(rec->e_cpos),
787 ocfs2_rec_clusters(el, rec));
788 ret = -EROFS;
789 goto out;
790 }
791
792 if (ret_phys_blkno)
793 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
794 if (ret_cpos)
795 *ret_cpos = le32_to_cpu(rec->e_cpos);
796 if (ret_clen)
797 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
798
799out:
800 brelse(eb_bh);
801 return ret;
802}
803
804/*
805 * Returns the block index, from the start of the cluster which this
806 * hash belongs too.
807 */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800808static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
809 u32 minor_hash)
810{
811 return minor_hash & osb->osb_dx_mask;
812}
813
814static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800815 struct ocfs2_dx_hinfo *hinfo)
816{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800817 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800818}
819
820static int ocfs2_dx_dir_lookup(struct inode *inode,
821 struct ocfs2_extent_list *el,
822 struct ocfs2_dx_hinfo *hinfo,
823 u32 *ret_cpos,
824 u64 *ret_phys_blkno)
825{
826 int ret = 0;
827 unsigned int cend, uninitialized_var(clen);
828 u32 uninitialized_var(cpos);
829 u64 uninitialized_var(blkno);
830 u32 name_hash = hinfo->major_hash;
831
832 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
833 &clen);
834 if (ret) {
835 mlog_errno(ret);
836 goto out;
837 }
838
839 cend = cpos + clen;
840 if (name_hash >= cend) {
841 /* We want the last cluster */
842 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
843 cpos += clen - 1;
844 } else {
845 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
846 name_hash - cpos);
847 cpos = name_hash;
848 }
849
850 /*
851 * We now have the cluster which should hold our entry. To
852 * find the exact block from the start of the cluster to
853 * search, we take the lower bits of the hash.
854 */
855 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
856
857 if (ret_phys_blkno)
858 *ret_phys_blkno = blkno;
859 if (ret_cpos)
860 *ret_cpos = cpos;
861
862out:
863
864 return ret;
865}
866
867static int ocfs2_dx_dir_search(const char *name, int namelen,
868 struct inode *dir,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800869 struct ocfs2_dx_root_block *dx_root,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800870 struct ocfs2_dir_lookup_result *res)
871{
872 int ret, i, found;
873 u64 uninitialized_var(phys);
874 struct buffer_head *dx_leaf_bh = NULL;
875 struct ocfs2_dx_leaf *dx_leaf;
876 struct ocfs2_dx_entry *dx_entry = NULL;
877 struct buffer_head *dir_ent_bh = NULL;
878 struct ocfs2_dir_entry *dir_ent = NULL;
879 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800880 struct ocfs2_extent_list *dr_el;
881 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800882
883 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
884
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800885 if (ocfs2_dx_root_inline(dx_root)) {
886 entry_list = &dx_root->dr_entries;
887 goto search;
888 }
889
890 dr_el = &dx_root->dr_list;
891
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800892 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
893 if (ret) {
894 mlog_errno(ret);
895 goto out;
896 }
897
898 mlog(0, "Dir %llu: name: \"%.*s\", lookup of hash: %u.0x%x "
899 "returns: %llu\n",
900 (unsigned long long)OCFS2_I(dir)->ip_blkno,
901 namelen, name, hinfo->major_hash, hinfo->minor_hash,
902 (unsigned long long)phys);
903
904 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
905 if (ret) {
906 mlog_errno(ret);
907 goto out;
908 }
909
910 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
911
912 mlog(0, "leaf info: num_used: %d, count: %d\n",
913 le16_to_cpu(dx_leaf->dl_list.de_num_used),
914 le16_to_cpu(dx_leaf->dl_list.de_count));
915
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800916 entry_list = &dx_leaf->dl_list;
917
918search:
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800919 /*
920 * Empty leaf is legal, so no need to check for that.
921 */
922 found = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800923 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
924 dx_entry = &entry_list->de_entries[i];
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800925
926 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
927 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
928 continue;
929
930 /*
931 * Search unindexed leaf block now. We're not
932 * guaranteed to find anything.
933 */
934 ret = ocfs2_read_dir_block_direct(dir,
935 le64_to_cpu(dx_entry->dx_dirent_blk),
936 &dir_ent_bh);
937 if (ret) {
938 mlog_errno(ret);
939 goto out;
940 }
941
942 /*
943 * XXX: We should check the unindexed block here,
944 * before using it.
945 */
946
947 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
948 0, dir_ent_bh->b_data,
949 dir->i_sb->s_blocksize, &dir_ent);
950 if (found == 1)
951 break;
952
953 if (found == -1) {
954 /* This means we found a bad directory entry. */
955 ret = -EIO;
956 mlog_errno(ret);
957 goto out;
958 }
959
960 brelse(dir_ent_bh);
961 dir_ent_bh = NULL;
962 }
963
964 if (found <= 0) {
965 ret = -ENOENT;
966 goto out;
967 }
968
969 res->dl_leaf_bh = dir_ent_bh;
970 res->dl_entry = dir_ent;
971 res->dl_dx_leaf_bh = dx_leaf_bh;
972 res->dl_dx_entry = dx_entry;
973
974 ret = 0;
975out:
976 if (ret) {
977 brelse(dx_leaf_bh);
978 brelse(dir_ent_bh);
979 }
980 return ret;
981}
982
983static int ocfs2_find_entry_dx(const char *name, int namelen,
984 struct inode *dir,
985 struct ocfs2_dir_lookup_result *lookup)
986{
987 int ret;
988 struct buffer_head *di_bh = NULL;
989 struct ocfs2_dinode *di;
990 struct buffer_head *dx_root_bh = NULL;
991 struct ocfs2_dx_root_block *dx_root;
992
993 ret = ocfs2_read_inode_block(dir, &di_bh);
994 if (ret) {
995 mlog_errno(ret);
996 goto out;
997 }
998
999 di = (struct ocfs2_dinode *)di_bh->b_data;
1000
1001 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1002 if (ret) {
1003 mlog_errno(ret);
1004 goto out;
1005 }
1006 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1007
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001008 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001009 if (ret) {
1010 if (ret != -ENOENT)
1011 mlog_errno(ret);
1012 goto out;
1013 }
1014
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001015 lookup->dl_dx_root_bh = dx_root_bh;
1016 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001017out:
1018 brelse(di_bh);
1019 brelse(dx_root_bh);
1020 return ret;
1021}
1022
Mark Fasheh23193e52007-09-12 13:01:18 -07001023/*
1024 * Try to find an entry of the provided name within 'dir'.
1025 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001026 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1027 * returned and the struct 'res' will contain information useful to
1028 * other directory manipulation functions.
Mark Fasheh23193e52007-09-12 13:01:18 -07001029 *
1030 * Caller can NOT assume anything about the contents of the
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001031 * buffer_heads - they are passed back only so that it can be passed
1032 * into any one of the manipulation functions (add entry, delete
1033 * entry, etc). As an example, bh in the extent directory case is a
1034 * data block, in the inline-data case it actually points to an inode,
1035 * in the indexed directory case, multiple buffers are involved.
Mark Fasheh23193e52007-09-12 13:01:18 -07001036 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001037int ocfs2_find_entry(const char *name, int namelen,
1038 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh23193e52007-09-12 13:01:18 -07001039{
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001040 struct buffer_head *bh;
1041 struct ocfs2_dir_entry *res_dir = NULL;
Mark Fasheh23193e52007-09-12 13:01:18 -07001042
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001043 if (ocfs2_dir_indexed(dir))
1044 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1045
1046 /*
1047 * The unindexed dir code only uses part of the lookup
1048 * structure, so there's no reason to push it down further
1049 * than this.
1050 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001051 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001052 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1053 else
1054 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
Mark Fasheh23193e52007-09-12 13:01:18 -07001055
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001056 if (bh == NULL)
1057 return -ENOENT;
1058
1059 lookup->dl_leaf_bh = bh;
1060 lookup->dl_entry = res_dir;
1061 return 0;
Mark Fasheh23193e52007-09-12 13:01:18 -07001062}
1063
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001064/*
1065 * Update inode number and type of a previously found directory entry.
1066 */
Mark Fasheh38760e22007-09-11 17:21:56 -07001067int ocfs2_update_entry(struct inode *dir, handle_t *handle,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001068 struct ocfs2_dir_lookup_result *res,
Mark Fasheh38760e22007-09-11 17:21:56 -07001069 struct inode *new_entry_inode)
1070{
1071 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07001072 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001073 struct ocfs2_dir_entry *de = res->dl_entry;
1074 struct buffer_head *de_bh = res->dl_leaf_bh;
Mark Fasheh38760e22007-09-11 17:21:56 -07001075
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001076 /*
1077 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -07001078 * based directories, so no need to split this up. The only
1079 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001080 */
1081
Joel Becker13723d02008-10-17 19:25:01 -07001082 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1083 access = ocfs2_journal_access_di;
1084
1085 ret = access(handle, dir, de_bh, OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -07001086 if (ret) {
1087 mlog_errno(ret);
1088 goto out;
1089 }
1090
1091 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1092 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1093
1094 ocfs2_journal_dirty(handle, de_bh);
1095
1096out:
1097 return ret;
1098}
1099
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001100/*
1101 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1102 * previous entry
1103 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001104static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1105 struct ocfs2_dir_entry *de_del,
1106 struct buffer_head *bh, char *first_de,
1107 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001108{
1109 struct ocfs2_dir_entry *de, *pde;
1110 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -07001111 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001112
1113 mlog_entry("(0x%p, 0x%p, 0x%p, 0x%p)\n", handle, dir, de_del, bh);
1114
Joel Becker13723d02008-10-17 19:25:01 -07001115 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1116 access = ocfs2_journal_access_di;
1117
Mark Fasheh316f4b92007-09-07 18:21:26 -07001118 i = 0;
1119 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001120 de = (struct ocfs2_dir_entry *) first_de;
1121 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001122 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1123 status = -EIO;
1124 mlog_errno(status);
1125 goto bail;
1126 }
1127 if (de == de_del) {
Joel Becker13723d02008-10-17 19:25:01 -07001128 status = access(handle, dir, bh,
1129 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001130 if (status < 0) {
1131 status = -EIO;
1132 mlog_errno(status);
1133 goto bail;
1134 }
1135 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +01001136 le16_add_cpu(&pde->rec_len,
1137 le16_to_cpu(de->rec_len));
Mark Fasheh316f4b92007-09-07 18:21:26 -07001138 else
1139 de->inode = 0;
1140 dir->i_version++;
1141 status = ocfs2_journal_dirty(handle, bh);
1142 goto bail;
1143 }
1144 i += le16_to_cpu(de->rec_len);
1145 pde = de;
1146 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1147 }
1148bail:
1149 mlog_exit(status);
1150 return status;
1151}
1152
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001153static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1154 int index)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001155{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001156 int num_used = le16_to_cpu(entry_list->de_num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001157
1158 if (num_used == 1 || index == (num_used - 1))
1159 goto clear;
1160
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001161 memmove(&entry_list->de_entries[index],
1162 &entry_list->de_entries[index + 1],
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001163 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1164clear:
1165 num_used--;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001166 memset(&entry_list->de_entries[num_used], 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001167 sizeof(struct ocfs2_dx_entry));
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001168 entry_list->de_num_used = cpu_to_le16(num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001169}
1170
1171static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1172 struct ocfs2_dir_lookup_result *lookup)
1173{
1174 int ret, index;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001175 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001176 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1177 struct ocfs2_dx_leaf *dx_leaf;
1178 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001179 struct ocfs2_dx_root_block *dx_root;
1180 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001181
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001182 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1183 if (ocfs2_dx_root_inline(dx_root)) {
1184 entry_list = &dx_root->dr_entries;
1185 } else {
1186 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1187 entry_list = &dx_leaf->dl_list;
1188 }
1189
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001190 /* Neither of these are a disk corruption - that should have
1191 * been caught by lookup, before we got here. */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001192 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1193 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001194
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001195 index = (char *)dx_entry - (char *)entry_list->de_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001196 index /= sizeof(*dx_entry);
1197
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001198 if (index >= le16_to_cpu(entry_list->de_num_used)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001199 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001200 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1201 entry_list, dx_entry);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001202 return -EIO;
1203 }
1204
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001205 /*
1206 * Add the block holding our index into the journal before
1207 * removing the unindexed entry. If we get an error return
1208 * from __ocfs2_delete_entry(), then it hasn't removed the
1209 * entry yet. Likewise, successful return means we *must*
1210 * remove the indexed entry.
1211 *
1212 * We're also careful to journal the root tree block here if
1213 * we're going to be adding to the start of the free list.
1214 */
1215 if (ocfs2_dx_root_inline(dx_root)) {
1216 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1217 OCFS2_JOURNAL_ACCESS_WRITE);
1218 if (ret) {
1219 mlog_errno(ret);
1220 goto out;
1221 }
1222 } else {
1223 ret = ocfs2_journal_access_dl(handle, dir,
1224 lookup->dl_dx_leaf_bh,
1225 OCFS2_JOURNAL_ACCESS_WRITE);
1226 if (ret) {
1227 mlog_errno(ret);
1228 goto out;
1229 }
1230 }
1231
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001232 mlog(0, "Dir %llu: delete entry at index: %d\n",
1233 (unsigned long long)OCFS2_I(dir)->ip_blkno, index);
1234
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001235 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1236 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1237 if (ret) {
1238 mlog_errno(ret);
1239 goto out;
1240 }
1241
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001242 ocfs2_dx_list_remove_entry(entry_list, index);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001243
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001244 if (ocfs2_dx_root_inline(dx_root))
1245 ocfs2_journal_dirty(handle, dx_root_bh);
1246 else
1247 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001248
1249out:
1250 return ret;
1251}
1252
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001253static inline int ocfs2_delete_entry_id(handle_t *handle,
1254 struct inode *dir,
1255 struct ocfs2_dir_entry *de_del,
1256 struct buffer_head *bh)
1257{
1258 int ret;
1259 struct buffer_head *di_bh = NULL;
1260 struct ocfs2_dinode *di;
1261 struct ocfs2_inline_data *data;
1262
Joel Beckerb657c952008-11-13 14:49:11 -08001263 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001264 if (ret) {
1265 mlog_errno(ret);
1266 goto out;
1267 }
1268
1269 di = (struct ocfs2_dinode *)di_bh->b_data;
1270 data = &di->id2.i_data;
1271
1272 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1273 i_size_read(dir));
1274
1275 brelse(di_bh);
1276out:
1277 return ret;
1278}
1279
1280static inline int ocfs2_delete_entry_el(handle_t *handle,
1281 struct inode *dir,
1282 struct ocfs2_dir_entry *de_del,
1283 struct buffer_head *bh)
1284{
1285 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1286 bh->b_size);
1287}
1288
1289/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001290 * Delete a directory entry. Hide the details of directory
1291 * implementation from the caller.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001292 */
1293int ocfs2_delete_entry(handle_t *handle,
1294 struct inode *dir,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001295 struct ocfs2_dir_lookup_result *res)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001296{
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001297 if (ocfs2_dir_indexed(dir))
1298 return ocfs2_delete_entry_dx(handle, dir, res);
1299
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001300 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001301 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1302 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001303
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001304 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1305 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001306}
1307
Mark Fasheh8553cf42007-09-13 16:29:01 -07001308/*
1309 * Check whether 'de' has enough room to hold an entry of
1310 * 'new_rec_len' bytes.
1311 */
1312static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1313 unsigned int new_rec_len)
1314{
1315 unsigned int de_really_used;
1316
1317 /* Check whether this is an empty record with enough space */
1318 if (le64_to_cpu(de->inode) == 0 &&
1319 le16_to_cpu(de->rec_len) >= new_rec_len)
1320 return 1;
1321
1322 /*
1323 * Record might have free space at the end which we can
1324 * use.
1325 */
1326 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1327 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1328 return 1;
1329
1330 return 0;
1331}
1332
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001333static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1334 struct ocfs2_dx_entry *dx_new_entry)
1335{
1336 int i;
1337
1338 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1339 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1340
1341 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1342}
1343
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001344static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1345 struct ocfs2_dx_hinfo *hinfo,
1346 u64 dirent_blk)
1347{
1348 int i;
1349 struct ocfs2_dx_entry *dx_entry;
1350
1351 i = le16_to_cpu(entry_list->de_num_used);
1352 dx_entry = &entry_list->de_entries[i];
1353
1354 memset(dx_entry, 0, sizeof(*dx_entry));
1355 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1356 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1357 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1358
1359 le16_add_cpu(&entry_list->de_num_used, 1);
1360}
1361
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001362static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1363 struct ocfs2_dx_hinfo *hinfo,
1364 u64 dirent_blk,
1365 struct buffer_head *dx_leaf_bh)
1366{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001367 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001368 struct ocfs2_dx_leaf *dx_leaf;
1369
1370 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
1371 OCFS2_JOURNAL_ACCESS_WRITE);
1372 if (ret) {
1373 mlog_errno(ret);
1374 goto out;
1375 }
1376
1377 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001378 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001379 ocfs2_journal_dirty(handle, dx_leaf_bh);
1380
1381out:
1382 return ret;
1383}
1384
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001385static int ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1386 struct ocfs2_dx_hinfo *hinfo,
1387 u64 dirent_blk,
1388 struct buffer_head *dx_root_bh)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001389{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001390 int ret;
1391 struct ocfs2_dx_root_block *dx_root;
1392
1393 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
1394 OCFS2_JOURNAL_ACCESS_WRITE);
1395 if (ret) {
1396 mlog_errno(ret);
1397 goto out;
1398 }
1399
1400 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1401 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1402 ocfs2_journal_dirty(handle, dx_root_bh);
1403
1404out:
1405 return ret;
1406}
1407
1408static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1409 struct ocfs2_dir_lookup_result *lookup)
1410{
1411 struct ocfs2_dx_root_block *dx_root;
1412
1413 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1414 if (ocfs2_dx_root_inline(dx_root))
1415 return ocfs2_dx_inline_root_insert(dir, handle,
1416 &lookup->dl_hinfo,
1417 lookup->dl_leaf_bh->b_blocknr,
1418 lookup->dl_dx_root_bh);
1419
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001420 return __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1421 lookup->dl_leaf_bh->b_blocknr,
1422 lookup->dl_dx_leaf_bh);
1423}
1424
Mark Fasheh316f4b92007-09-07 18:21:26 -07001425/* we don't always have a dentry for what we want to add, so people
1426 * like orphan dir can call this instead.
1427 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001428 * The lookup context must have been filled from
1429 * ocfs2_prepare_dir_for_insert.
Mark Fasheh316f4b92007-09-07 18:21:26 -07001430 */
1431int __ocfs2_add_entry(handle_t *handle,
1432 struct inode *dir,
1433 const char *name, int namelen,
1434 struct inode *inode, u64 blkno,
1435 struct buffer_head *parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001436 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001437{
1438 unsigned long offset;
1439 unsigned short rec_len;
1440 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001441 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1442 struct super_block *sb = dir->i_sb;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001443 int retval, status;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001444 unsigned int size = sb->s_blocksize;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001445 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001446 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001447
1448 mlog_entry_void();
1449
Mark Fasheh316f4b92007-09-07 18:21:26 -07001450 if (!namelen)
1451 return -EINVAL;
1452
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001453 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1454 data_start = di->id2.i_data.id_data;
1455 size = i_size_read(dir);
1456
1457 BUG_ON(insert_bh != parent_fe_bh);
1458 }
1459
Mark Fasheh316f4b92007-09-07 18:21:26 -07001460 rec_len = OCFS2_DIR_REC_LEN(namelen);
1461 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001462 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001463 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001464 BUG_ON((char *)de >= (size + data_start));
1465
Mark Fasheh316f4b92007-09-07 18:21:26 -07001466 /* These checks should've already been passed by the
1467 * prepare function, but I guess we can leave them
1468 * here anyway. */
1469 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1470 retval = -ENOENT;
1471 goto bail;
1472 }
1473 if (ocfs2_match(namelen, name, de)) {
1474 retval = -EEXIST;
1475 goto bail;
1476 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001477
Mark Fasheh87d35a72008-12-10 17:36:25 -08001478 /* We're guaranteed that we should have space, so we
1479 * can't possibly have hit the trailer...right? */
1480 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1481 "Hit dir trailer trying to insert %.*s "
1482 "(namelen %d) into directory %llu. "
1483 "offset is %lu, trailer offset is %d\n",
1484 namelen, name, namelen,
1485 (unsigned long long)parent_fe_bh->b_blocknr,
1486 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1487
Mark Fasheh8553cf42007-09-13 16:29:01 -07001488 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001489 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1490 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1491 if (retval < 0) {
1492 mlog_errno(retval);
1493 goto bail;
1494 }
1495
Joel Becker13723d02008-10-17 19:25:01 -07001496 if (insert_bh == parent_fe_bh)
1497 status = ocfs2_journal_access_di(handle, dir,
1498 insert_bh,
1499 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001500 else {
Joel Becker13723d02008-10-17 19:25:01 -07001501 status = ocfs2_journal_access_db(handle, dir,
1502 insert_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001503 OCFS2_JOURNAL_ACCESS_WRITE);
1504
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001505 if (ocfs2_dir_indexed(dir)) {
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001506 status = ocfs2_dx_dir_insert(dir,
1507 handle,
1508 lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001509 if (status) {
1510 mlog_errno(status);
1511 goto bail;
1512 }
1513 }
1514 }
1515
Mark Fasheh316f4b92007-09-07 18:21:26 -07001516 /* By now the buffer is marked for journaling */
1517 offset += le16_to_cpu(de->rec_len);
1518 if (le64_to_cpu(de->inode)) {
1519 de1 = (struct ocfs2_dir_entry *)((char *) de +
1520 OCFS2_DIR_REC_LEN(de->name_len));
1521 de1->rec_len =
1522 cpu_to_le16(le16_to_cpu(de->rec_len) -
1523 OCFS2_DIR_REC_LEN(de->name_len));
1524 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1525 de = de1;
1526 }
1527 de->file_type = OCFS2_FT_UNKNOWN;
1528 if (blkno) {
1529 de->inode = cpu_to_le64(blkno);
1530 ocfs2_set_de_type(de, inode->i_mode);
1531 } else
1532 de->inode = 0;
1533 de->name_len = namelen;
1534 memcpy(de->name, name, namelen);
1535
1536 dir->i_version++;
1537 status = ocfs2_journal_dirty(handle, insert_bh);
1538 retval = 0;
1539 goto bail;
1540 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001541
Mark Fasheh316f4b92007-09-07 18:21:26 -07001542 offset += le16_to_cpu(de->rec_len);
1543 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1544 }
1545
1546 /* when you think about it, the assert above should prevent us
1547 * from ever getting here. */
1548 retval = -ENOSPC;
1549bail:
1550
1551 mlog_exit(retval);
1552 return retval;
1553}
1554
Mark Fasheh23193e52007-09-12 13:01:18 -07001555static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001556 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -07001557 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001558 filldir_t filldir, int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -07001559{
1560 int ret, i, filldir_ret;
1561 unsigned long offset = *f_pos;
1562 struct buffer_head *di_bh = NULL;
1563 struct ocfs2_dinode *di;
1564 struct ocfs2_inline_data *data;
1565 struct ocfs2_dir_entry *de;
1566
Joel Beckerb657c952008-11-13 14:49:11 -08001567 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -07001568 if (ret) {
1569 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1570 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1571 goto out;
1572 }
1573
1574 di = (struct ocfs2_dinode *)di_bh->b_data;
1575 data = &di->id2.i_data;
1576
1577 while (*f_pos < i_size_read(inode)) {
1578revalidate:
1579 /* If the dir block has changed since the last call to
1580 * readdir(2), then we might be pointing to an invalid
1581 * dirent right now. Scan from the start of the block
1582 * to make sure. */
1583 if (*f_version != inode->i_version) {
1584 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1585 de = (struct ocfs2_dir_entry *)
1586 (data->id_data + i);
1587 /* It's too expensive to do a full
1588 * dirent test each time round this
1589 * loop, but we do have to test at
1590 * least that it is non-zero. A
1591 * failure will be detected in the
1592 * dirent test below. */
1593 if (le16_to_cpu(de->rec_len) <
1594 OCFS2_DIR_REC_LEN(1))
1595 break;
1596 i += le16_to_cpu(de->rec_len);
1597 }
1598 *f_pos = offset = i;
1599 *f_version = inode->i_version;
1600 }
1601
1602 de = (struct ocfs2_dir_entry *) (data->id_data + *f_pos);
1603 if (!ocfs2_check_dir_entry(inode, de, di_bh, *f_pos)) {
1604 /* On error, skip the f_pos to the end. */
1605 *f_pos = i_size_read(inode);
1606 goto out;
1607 }
1608 offset += le16_to_cpu(de->rec_len);
1609 if (le64_to_cpu(de->inode)) {
1610 /* We might block in the next section
1611 * if the data destination is
1612 * currently swapped out. So, use a
1613 * version stamp to detect whether or
1614 * not the directory has been modified
1615 * during the copy operation.
1616 */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001617 u64 version = *f_version;
Mark Fasheh23193e52007-09-12 13:01:18 -07001618 unsigned char d_type = DT_UNKNOWN;
1619
1620 if (de->file_type < OCFS2_FT_MAX)
1621 d_type = ocfs2_filetype_table[de->file_type];
1622
1623 filldir_ret = filldir(priv, de->name,
1624 de->name_len,
1625 *f_pos,
1626 le64_to_cpu(de->inode),
1627 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -07001628 if (filldir_ret) {
1629 if (filldir_err)
1630 *filldir_err = filldir_ret;
Mark Fasheh23193e52007-09-12 13:01:18 -07001631 break;
Mark Fashehe7b34012007-09-24 14:25:27 -07001632 }
Mark Fasheh23193e52007-09-12 13:01:18 -07001633 if (version != *f_version)
1634 goto revalidate;
1635 }
1636 *f_pos += le16_to_cpu(de->rec_len);
1637 }
1638
1639out:
1640 brelse(di_bh);
1641
1642 return 0;
1643}
1644
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001645/*
1646 * NOTE: This function can be called against unindexed directories,
1647 * and indexed ones.
1648 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001649static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001650 u64 *f_version,
Mark Fasheh23193e52007-09-12 13:01:18 -07001651 loff_t *f_pos, void *priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001652 filldir_t filldir, int *filldir_err)
Mark Fashehccd979b2005-12-15 14:31:24 -08001653{
1654 int error = 0;
Mark Fashehaa958872006-04-21 13:49:02 -07001655 unsigned long offset, blk, last_ra_blk = 0;
1656 int i, stored;
Mark Fashehccd979b2005-12-15 14:31:24 -08001657 struct buffer_head * bh, * tmp;
1658 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -08001659 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -07001660 unsigned int ra_sectors = 16;
Mark Fashehccd979b2005-12-15 14:31:24 -08001661
1662 stored = 0;
1663 bh = NULL;
1664
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001665 offset = (*f_pos) & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001666
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001667 while (!error && !stored && *f_pos < i_size_read(inode)) {
1668 blk = (*f_pos) >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -08001669 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1670 /* Skip the corrupt dirblock and keep trying */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001671 *f_pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -08001672 continue;
1673 }
1674
Mark Fashehaa958872006-04-21 13:49:02 -07001675 /* The idea here is to begin with 8k read-ahead and to stay
1676 * 4k ahead of our current position.
1677 *
1678 * TODO: Use the pagecache for this. We just need to
1679 * make sure it's cluster-safe... */
1680 if (!last_ra_blk
1681 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1682 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -08001683 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -08001684 tmp = NULL;
1685 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1686 OCFS2_BH_READAHEAD))
1687 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -08001688 }
Mark Fashehaa958872006-04-21 13:49:02 -07001689 last_ra_blk = blk;
1690 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08001691 }
1692
1693revalidate:
1694 /* If the dir block has changed since the last call to
1695 * readdir(2), then we might be pointing to an invalid
1696 * dirent right now. Scan from the start of the block
1697 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001698 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001699 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1700 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1701 /* It's too expensive to do a full
1702 * dirent test each time round this
1703 * loop, but we do have to test at
1704 * least that it is non-zero. A
1705 * failure will be detected in the
1706 * dirent test below. */
1707 if (le16_to_cpu(de->rec_len) <
1708 OCFS2_DIR_REC_LEN(1))
1709 break;
1710 i += le16_to_cpu(de->rec_len);
1711 }
1712 offset = i;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001713 *f_pos = ((*f_pos) & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -08001714 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001715 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -08001716 }
1717
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001718 while (!error && *f_pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001719 && offset < sb->s_blocksize) {
1720 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1721 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1722 /* On error, skip the f_pos to the
1723 next block. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001724 *f_pos = ((*f_pos) | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001725 brelse(bh);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001726 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08001727 }
1728 offset += le16_to_cpu(de->rec_len);
1729 if (le64_to_cpu(de->inode)) {
1730 /* We might block in the next section
1731 * if the data destination is
1732 * currently swapped out. So, use a
1733 * version stamp to detect whether or
1734 * not the directory has been modified
1735 * during the copy operation.
1736 */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001737 unsigned long version = *f_version;
Mark Fashehccd979b2005-12-15 14:31:24 -08001738 unsigned char d_type = DT_UNKNOWN;
1739
1740 if (de->file_type < OCFS2_FT_MAX)
1741 d_type = ocfs2_filetype_table[de->file_type];
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001742 error = filldir(priv, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -08001743 de->name_len,
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001744 *f_pos,
Mark Fasheh7e853672007-09-10 17:30:26 -07001745 le64_to_cpu(de->inode),
Mark Fashehccd979b2005-12-15 14:31:24 -08001746 d_type);
Mark Fashehe7b34012007-09-24 14:25:27 -07001747 if (error) {
1748 if (filldir_err)
1749 *filldir_err = error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001750 break;
Mark Fashehe7b34012007-09-24 14:25:27 -07001751 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001752 if (version != *f_version)
Mark Fashehccd979b2005-12-15 14:31:24 -08001753 goto revalidate;
1754 stored ++;
1755 }
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001756 *f_pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -08001757 }
1758 offset = 0;
1759 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -08001760 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001761 }
1762
1763 stored = 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001764out:
1765 return stored;
1766}
1767
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001768static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Mark Fashehe7b34012007-09-24 14:25:27 -07001769 loff_t *f_pos, void *priv, filldir_t filldir,
1770 int *filldir_err)
Mark Fasheh23193e52007-09-12 13:01:18 -07001771{
1772 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1773 return ocfs2_dir_foreach_blk_id(inode, f_version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001774 filldir, filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001775
Mark Fashehe7b34012007-09-24 14:25:27 -07001776 return ocfs2_dir_foreach_blk_el(inode, f_version, f_pos, priv, filldir,
1777 filldir_err);
Mark Fasheh23193e52007-09-12 13:01:18 -07001778}
1779
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001780/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001781 * This is intended to be called from inside other kernel functions,
1782 * so we fake some arguments.
1783 */
1784int ocfs2_dir_foreach(struct inode *inode, loff_t *f_pos, void *priv,
1785 filldir_t filldir)
1786{
Mark Fashehe7b34012007-09-24 14:25:27 -07001787 int ret = 0, filldir_err = 0;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001788 u64 version = inode->i_version;
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001789
1790 while (*f_pos < i_size_read(inode)) {
1791 ret = ocfs2_dir_foreach_blk(inode, &version, f_pos, priv,
Mark Fashehe7b34012007-09-24 14:25:27 -07001792 filldir, &filldir_err);
1793 if (ret || filldir_err)
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001794 break;
1795 }
1796
Mark Fashehe7b34012007-09-24 14:25:27 -07001797 if (ret > 0)
1798 ret = -EIO;
1799
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001800 return 0;
1801}
1802
1803/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001804 * ocfs2_readdir()
1805 *
1806 */
1807int ocfs2_readdir(struct file * filp, void * dirent, filldir_t filldir)
1808{
1809 int error = 0;
1810 struct inode *inode = filp->f_path.dentry->d_inode;
1811 int lock_level = 0;
1812
1813 mlog_entry("dirino=%llu\n",
1814 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1815
Mark Fashehe63aecb62007-10-18 15:30:42 -07001816 error = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001817 if (lock_level && error >= 0) {
1818 /* We release EX lock which used to update atime
1819 * and get PR lock again to reduce contention
1820 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001821 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001822 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001823 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001824 }
1825 if (error < 0) {
1826 if (error != -ENOENT)
1827 mlog_errno(error);
1828 /* we haven't got any yet, so propagate the error. */
1829 goto bail_nolock;
1830 }
1831
1832 error = ocfs2_dir_foreach_blk(inode, &filp->f_version, &filp->f_pos,
Mark Fashehe7b34012007-09-24 14:25:27 -07001833 dirent, filldir, NULL);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001834
Mark Fashehe63aecb62007-10-18 15:30:42 -07001835 ocfs2_inode_unlock(inode, lock_level);
Mark Fashehccd979b2005-12-15 14:31:24 -08001836
Mark Fashehaa958872006-04-21 13:49:02 -07001837bail_nolock:
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001838 mlog_exit(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08001839
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001840 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001841}
1842
1843/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001844 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08001845 */
1846int ocfs2_find_files_on_disk(const char *name,
1847 int namelen,
1848 u64 *blkno,
1849 struct inode *inode,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001850 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08001851{
1852 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08001853
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001854 mlog(0, "name=%.*s, blkno=%p, inode=%llu\n", namelen, name, blkno,
1855 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001856
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001857 status = ocfs2_find_entry(name, namelen, inode, lookup);
1858 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08001859 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08001860
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001861 *blkno = le64_to_cpu(lookup->dl_entry->inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08001862
1863 status = 0;
1864leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08001865
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 return status;
1867}
1868
Mark Fashehbe94d112007-09-11 15:22:06 -07001869/*
1870 * Convenience function for callers which just want the block number
1871 * mapped to a name and don't require the full dirent info, etc.
1872 */
1873int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
1874 int namelen, u64 *blkno)
1875{
1876 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001877 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehbe94d112007-09-11 15:22:06 -07001878
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001879 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
1880 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehbe94d112007-09-11 15:22:06 -07001881
1882 return ret;
1883}
1884
Mark Fashehccd979b2005-12-15 14:31:24 -08001885/* Check for a name within a directory.
1886 *
1887 * Return 0 if the name does not exist
1888 * Return -EEXIST if the directory contains the name
1889 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001890 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 */
1892int ocfs2_check_dir_for_entry(struct inode *dir,
1893 const char *name,
1894 int namelen)
1895{
1896 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001897 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08001898
Mark Fashehb06970532006-03-03 10:24:33 -08001899 mlog_entry("dir %llu, name '%.*s'\n",
1900 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08001901
1902 ret = -EEXIST;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001903 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 goto bail;
1905
1906 ret = 0;
1907bail:
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001908 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08001909
1910 mlog_exit(ret);
1911 return ret;
1912}
1913
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001914struct ocfs2_empty_dir_priv {
1915 unsigned seen_dot;
1916 unsigned seen_dot_dot;
1917 unsigned seen_other;
1918};
1919static int ocfs2_empty_dir_filldir(void *priv, const char *name, int name_len,
1920 loff_t pos, u64 ino, unsigned type)
1921{
1922 struct ocfs2_empty_dir_priv *p = priv;
1923
1924 /*
1925 * Check the positions of "." and ".." records to be sure
1926 * they're in the correct place.
1927 */
1928 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
1929 p->seen_dot = 1;
1930 return 0;
1931 }
1932
1933 if (name_len == 2 && !strncmp("..", name, 2) &&
1934 pos == OCFS2_DIR_REC_LEN(1)) {
1935 p->seen_dot_dot = 1;
1936 return 0;
1937 }
1938
1939 p->seen_other = 1;
1940 return 1;
1941}
Mark Fashehccd979b2005-12-15 14:31:24 -08001942/*
1943 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001944 *
1945 * Returns 1 if dir is empty, zero otherwise.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001946 *
1947 * XXX: This is a performance problem
Mark Fashehccd979b2005-12-15 14:31:24 -08001948 */
1949int ocfs2_empty_dir(struct inode *inode)
1950{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001951 int ret;
1952 loff_t start = 0;
1953 struct ocfs2_empty_dir_priv priv;
Mark Fashehccd979b2005-12-15 14:31:24 -08001954
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001955 memset(&priv, 0, sizeof(priv));
1956
1957 ret = ocfs2_dir_foreach(inode, &start, &priv, ocfs2_empty_dir_filldir);
1958 if (ret)
1959 mlog_errno(ret);
1960
1961 if (!priv.seen_dot || !priv.seen_dot_dot) {
1962 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -08001963 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001964 /*
1965 * XXX: Is it really safe to allow an unlink to continue?
1966 */
Mark Fashehccd979b2005-12-15 14:31:24 -08001967 return 1;
1968 }
1969
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07001970 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08001971}
1972
Mark Fasheh87d35a72008-12-10 17:36:25 -08001973/*
1974 * Fills "." and ".." dirents in a new directory block. Returns dirent for
1975 * "..", which might be used during creation of a directory with a trailing
1976 * header. It is otherwise safe to ignore the return code.
1977 */
1978static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
1979 struct inode *parent,
1980 char *start,
1981 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001982{
1983 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
1984
1985 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
1986 de->name_len = 1;
1987 de->rec_len =
1988 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1989 strcpy(de->name, ".");
1990 ocfs2_set_de_type(de, S_IFDIR);
1991
1992 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
1993 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
1994 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
1995 de->name_len = 2;
1996 strcpy(de->name, "..");
1997 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08001998
1999 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002000}
2001
2002/*
2003 * This works together with code in ocfs2_mknod_locked() which sets
2004 * the inline-data flag and initializes the inline-data section.
2005 */
2006static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2007 handle_t *handle,
2008 struct inode *parent,
2009 struct inode *inode,
2010 struct buffer_head *di_bh)
2011{
2012 int ret;
2013 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2014 struct ocfs2_inline_data *data = &di->id2.i_data;
2015 unsigned int size = le16_to_cpu(data->id_count);
2016
Joel Becker13723d02008-10-17 19:25:01 -07002017 ret = ocfs2_journal_access_di(handle, inode, di_bh,
2018 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002019 if (ret) {
2020 mlog_errno(ret);
2021 goto out;
2022 }
2023
2024 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2025
2026 ocfs2_journal_dirty(handle, di_bh);
2027 if (ret) {
2028 mlog_errno(ret);
2029 goto out;
2030 }
2031
2032 i_size_write(inode, size);
2033 inode->i_nlink = 2;
2034 inode->i_blocks = ocfs2_inode_sector_count(inode);
2035
2036 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2037 if (ret < 0)
2038 mlog_errno(ret);
2039
2040out:
2041 return ret;
2042}
2043
2044static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2045 handle_t *handle,
2046 struct inode *parent,
2047 struct inode *inode,
2048 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002049 struct ocfs2_alloc_context *data_ac,
2050 struct buffer_head **ret_new_bh)
Mark Fasheh316f4b92007-09-07 18:21:26 -07002051{
2052 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002053 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002054 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002055 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002056
2057 mlog_entry_void();
2058
Mark Fasheh87d35a72008-12-10 17:36:25 -08002059 if (ocfs2_supports_dir_trailer(osb))
2060 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2061
Mark Fasheh316f4b92007-09-07 18:21:26 -07002062 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2063 data_ac, NULL, &new_bh);
2064 if (status < 0) {
2065 mlog_errno(status);
2066 goto bail;
2067 }
2068
2069 ocfs2_set_new_buffer_uptodate(inode, new_bh);
2070
Joel Becker13723d02008-10-17 19:25:01 -07002071 status = ocfs2_journal_access_db(handle, inode, new_bh,
2072 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002073 if (status < 0) {
2074 mlog_errno(status);
2075 goto bail;
2076 }
2077 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2078
Mark Fasheh87d35a72008-12-10 17:36:25 -08002079 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2080 if (ocfs2_supports_dir_trailer(osb))
2081 ocfs2_init_dir_trailer(inode, new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002082
2083 status = ocfs2_journal_dirty(handle, new_bh);
2084 if (status < 0) {
2085 mlog_errno(status);
2086 goto bail;
2087 }
2088
2089 i_size_write(inode, inode->i_sb->s_blocksize);
2090 inode->i_nlink = 2;
2091 inode->i_blocks = ocfs2_inode_sector_count(inode);
2092 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2093 if (status < 0) {
2094 mlog_errno(status);
2095 goto bail;
2096 }
2097
2098 status = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002099 if (ret_new_bh) {
2100 *ret_new_bh = new_bh;
2101 new_bh = NULL;
2102 }
Mark Fasheh316f4b92007-09-07 18:21:26 -07002103bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002104 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002105
2106 mlog_exit(status);
2107 return status;
2108}
2109
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002110static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2111 handle_t *handle, struct inode *dir,
2112 struct buffer_head *di_bh,
2113 struct ocfs2_alloc_context *meta_ac,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002114 int dx_inline,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002115 struct buffer_head **ret_dx_root_bh)
2116{
2117 int ret;
2118 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2119 u16 dr_suballoc_bit;
2120 u64 dr_blkno;
2121 unsigned int num_bits;
2122 struct buffer_head *dx_root_bh = NULL;
2123 struct ocfs2_dx_root_block *dx_root;
2124
2125 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, &dr_suballoc_bit,
2126 &num_bits, &dr_blkno);
2127 if (ret) {
2128 mlog_errno(ret);
2129 goto out;
2130 }
2131
2132 mlog(0, "Dir %llu, attach new index block: %llu\n",
2133 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2134 (unsigned long long)dr_blkno);
2135
2136 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2137 if (dx_root_bh == NULL) {
2138 ret = -EIO;
2139 goto out;
2140 }
2141 ocfs2_set_new_buffer_uptodate(dir, dx_root_bh);
2142
2143 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
2144 OCFS2_JOURNAL_ACCESS_CREATE);
2145 if (ret < 0) {
2146 mlog_errno(ret);
2147 goto out;
2148 }
2149
2150 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2151 memset(dx_root, 0, osb->sb->s_blocksize);
2152 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2153 dx_root->dr_suballoc_slot = cpu_to_le16(osb->slot_num);
2154 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2155 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2156 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2157 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002158
2159 if (dx_inline) {
2160 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2161 dx_root->dr_entries.de_count =
2162 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2163 } else {
2164 dx_root->dr_list.l_count =
2165 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2166 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002167
2168 ret = ocfs2_journal_dirty(handle, dx_root_bh);
2169 if (ret)
2170 mlog_errno(ret);
2171
2172 ret = ocfs2_journal_access_di(handle, dir, di_bh,
2173 OCFS2_JOURNAL_ACCESS_CREATE);
2174 if (ret) {
2175 mlog_errno(ret);
2176 goto out;
2177 }
2178
2179 di->i_dx_root = cpu_to_le64(dr_blkno);
2180
2181 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2182 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2183
2184 ret = ocfs2_journal_dirty(handle, di_bh);
2185 if (ret)
2186 mlog_errno(ret);
2187
2188 *ret_dx_root_bh = dx_root_bh;
2189 dx_root_bh = NULL;
2190
2191out:
2192 brelse(dx_root_bh);
2193 return ret;
2194}
2195
2196static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2197 handle_t *handle, struct inode *dir,
2198 struct buffer_head **dx_leaves,
2199 int num_dx_leaves, u64 start_blk)
2200{
2201 int ret, i;
2202 struct ocfs2_dx_leaf *dx_leaf;
2203 struct buffer_head *bh;
2204
2205 for (i = 0; i < num_dx_leaves; i++) {
2206 bh = sb_getblk(osb->sb, start_blk + i);
2207 if (bh == NULL) {
2208 ret = -EIO;
2209 goto out;
2210 }
2211 dx_leaves[i] = bh;
2212
2213 ocfs2_set_new_buffer_uptodate(dir, bh);
2214
2215 ret = ocfs2_journal_access_dl(handle, dir, bh,
2216 OCFS2_JOURNAL_ACCESS_CREATE);
2217 if (ret < 0) {
2218 mlog_errno(ret);
2219 goto out;
2220 }
2221
2222 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2223
2224 memset(dx_leaf, 0, osb->sb->s_blocksize);
2225 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2226 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2227 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2228 dx_leaf->dl_list.de_count =
2229 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2230
2231 mlog(0,
2232 "Dir %llu, format dx_leaf: %llu, entry count: %u\n",
2233 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2234 (unsigned long long)bh->b_blocknr,
2235 le16_to_cpu(dx_leaf->dl_list.de_count));
2236
2237 ocfs2_journal_dirty(handle, bh);
2238 }
2239
2240 ret = 0;
2241out:
2242 return ret;
2243}
2244
2245/*
2246 * Allocates and formats a new cluster for use in an indexed dir
2247 * leaf. This version will not do the extent insert, so that it can be
2248 * used by operations which need careful ordering.
2249 */
2250static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2251 u32 cpos, handle_t *handle,
2252 struct ocfs2_alloc_context *data_ac,
2253 struct buffer_head **dx_leaves,
2254 int num_dx_leaves, u64 *ret_phys_blkno)
2255{
2256 int ret;
2257 u32 phys, num;
2258 u64 phys_blkno;
2259 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2260
2261 /*
2262 * XXX: For create, this should claim cluster for the index
2263 * *before* the unindexed insert so that we have a better
2264 * chance of contiguousness as the directory grows in number
2265 * of entries.
2266 */
2267 ret = __ocfs2_claim_clusters(osb, handle, data_ac, 1, 1, &phys, &num);
2268 if (ret) {
2269 mlog_errno(ret);
2270 goto out;
2271 }
2272
2273 /*
2274 * Format the new cluster first. That way, we're inserting
2275 * valid data.
2276 */
2277 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2278 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2279 num_dx_leaves, phys_blkno);
2280 if (ret) {
2281 mlog_errno(ret);
2282 goto out;
2283 }
2284
2285 *ret_phys_blkno = phys_blkno;
2286out:
2287 return ret;
2288}
2289
2290static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2291 struct ocfs2_extent_tree *et,
2292 u32 cpos, handle_t *handle,
2293 struct ocfs2_alloc_context *data_ac,
2294 struct ocfs2_alloc_context *meta_ac,
2295 struct buffer_head **dx_leaves,
2296 int num_dx_leaves)
2297{
2298 int ret;
2299 u64 phys_blkno;
2300 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2301
2302 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2303 num_dx_leaves, &phys_blkno);
2304 if (ret) {
2305 mlog_errno(ret);
2306 goto out;
2307 }
2308
2309 ret = ocfs2_insert_extent(osb, handle, dir, et, cpos, phys_blkno, 1, 0,
2310 meta_ac);
2311 if (ret)
2312 mlog_errno(ret);
2313out:
2314 return ret;
2315}
2316
2317static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2318 int *ret_num_leaves)
2319{
2320 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2321 struct buffer_head **dx_leaves;
2322
2323 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2324 GFP_NOFS);
2325 if (dx_leaves && ret_num_leaves)
2326 *ret_num_leaves = num_dx_leaves;
2327
2328 return dx_leaves;
2329}
2330
2331static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2332 handle_t *handle,
2333 struct inode *parent,
2334 struct inode *inode,
2335 struct buffer_head *di_bh,
2336 struct ocfs2_alloc_context *data_ac,
2337 struct ocfs2_alloc_context *meta_ac)
2338{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002339 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002340 struct buffer_head *leaf_bh = NULL;
2341 struct buffer_head *dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002342 struct ocfs2_dx_hinfo hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002343 struct ocfs2_dx_root_block *dx_root;
2344 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002345
2346 /*
2347 * Our strategy is to create the directory as though it were
2348 * unindexed, then add the index block. This works with very
2349 * little complication since the state of a new directory is a
2350 * very well known quantity.
2351 *
2352 * Essentially, we have two dirents ("." and ".."), in the 1st
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002353 * block which need indexing. These are easily inserted into
2354 * the index block.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002355 */
2356
2357 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2358 data_ac, &leaf_bh);
2359 if (ret) {
2360 mlog_errno(ret);
2361 goto out;
2362 }
2363
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002364 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh,
2365 meta_ac, 1, &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002366 if (ret) {
2367 mlog_errno(ret);
2368 goto out;
2369 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002370 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2371 entry_list = &dx_root->dr_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002372
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002373 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2374 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002375
2376 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002377 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002378
2379out:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002380 brelse(dx_root_bh);
2381 brelse(leaf_bh);
2382 return ret;
2383}
2384
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002385int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2386 handle_t *handle,
2387 struct inode *parent,
2388 struct inode *inode,
2389 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002390 struct ocfs2_alloc_context *data_ac,
2391 struct ocfs2_alloc_context *meta_ac)
2392
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002393{
2394 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2395
2396 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2397 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2398
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002399 if (ocfs2_supports_indexed_dirs(osb))
2400 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2401 data_ac, meta_ac);
2402
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002403 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002404 data_ac, NULL);
2405}
2406
2407static int ocfs2_dx_dir_index_block(struct inode *dir,
2408 handle_t *handle,
2409 struct buffer_head **dx_leaves,
2410 int num_dx_leaves,
2411 struct buffer_head *dirent_bh)
2412{
2413 int ret, namelen, i;
2414 char *de_buf, *limit;
2415 struct ocfs2_dir_entry *de;
2416 struct buffer_head *dx_leaf_bh;
2417 struct ocfs2_dx_hinfo hinfo;
2418 u64 dirent_blk = dirent_bh->b_blocknr;
2419
2420 de_buf = dirent_bh->b_data;
2421 limit = de_buf + dir->i_sb->s_blocksize;
2422
2423 while (de_buf < limit) {
2424 de = (struct ocfs2_dir_entry *)de_buf;
2425
2426 namelen = de->name_len;
2427 if (!namelen || !de->inode)
2428 goto inc;
2429
2430 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2431
2432 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2433 dx_leaf_bh = dx_leaves[i];
2434
2435 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2436 dirent_blk, dx_leaf_bh);
2437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2440 }
2441
2442inc:
2443 de_buf += le16_to_cpu(de->rec_len);
2444 }
2445
2446out:
2447 return ret;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002448}
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002449 /*
2450 * XXX: This expects dx_root_bh to already be part of the transaction.
2451 */
2452static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2453 struct buffer_head *dx_root_bh,
2454 struct buffer_head *dirent_bh)
2455{
2456 char *de_buf, *limit;
2457 struct ocfs2_dx_root_block *dx_root;
2458 struct ocfs2_dir_entry *de;
2459 struct ocfs2_dx_hinfo hinfo;
2460 u64 dirent_blk = dirent_bh->b_blocknr;
2461
2462 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2463
2464 de_buf = dirent_bh->b_data;
2465 limit = de_buf + dir->i_sb->s_blocksize;
2466
2467 while (de_buf < limit) {
2468 de = (struct ocfs2_dir_entry *)de_buf;
2469
2470 if (!de->name_len || !de->inode)
2471 goto inc;
2472
2473 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2474
2475 mlog(0,
2476 "dir: %llu, major: 0x%x minor: 0x%x, index: %u, name: %.*s\n",
2477 (unsigned long long)dir->i_ino, hinfo.major_hash,
2478 hinfo.minor_hash,
2479 le16_to_cpu(dx_root->dr_entries.de_num_used),
2480 de->name_len, de->name);
2481
2482 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2483 dirent_blk);
2484inc:
2485 de_buf += le16_to_cpu(de->rec_len);
2486 }
2487}
2488
2489/*
2490 * Count the number of inline directory entries in di_bh and compare
2491 * them against the number of entries we can hold in an inline dx root
2492 * block.
2493 */
2494static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2495 struct buffer_head *di_bh)
2496{
2497 int dirent_count = 0;
2498 char *de_buf, *limit;
2499 struct ocfs2_dir_entry *de;
2500 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2501
2502 de_buf = di->id2.i_data.id_data;
2503 limit = de_buf + i_size_read(dir);
2504
2505 while (de_buf < limit) {
2506 de = (struct ocfs2_dir_entry *)de_buf;
2507
2508 if (de->name_len && de->inode)
2509 dirent_count++;
2510
2511 de_buf += le16_to_cpu(de->rec_len);
2512 }
2513
2514 /* We are careful to leave room for one extra record. */
2515 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2516}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002517
Mark Fasheh87d35a72008-12-10 17:36:25 -08002518/*
2519 * Expand rec_len of the rightmost dirent in a directory block so that it
2520 * contains the end of our valid space for dirents. We do this during
2521 * expansion from an inline directory to one with extents. The first dir block
2522 * in that case is taken from the inline data portion of the inode block.
2523 *
2524 * We add the dir trailer if this filesystem wants it.
2525 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002526static void ocfs2_expand_last_dirent(char *start, unsigned int old_size,
Mark Fasheh87d35a72008-12-10 17:36:25 -08002527 struct super_block *sb)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002528{
2529 struct ocfs2_dir_entry *de;
2530 struct ocfs2_dir_entry *prev_de;
2531 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002532 unsigned int new_size = sb->s_blocksize;
2533 unsigned int bytes;
2534
2535 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
2536 new_size = ocfs2_dir_trailer_blk_off(sb);
2537
2538 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002539
2540 limit = start + old_size;
2541 de_buf = start;
2542 de = (struct ocfs2_dir_entry *)de_buf;
2543 do {
2544 prev_de = de;
2545 de_buf += le16_to_cpu(de->rec_len);
2546 de = (struct ocfs2_dir_entry *)de_buf;
2547 } while (de_buf < limit);
2548
2549 le16_add_cpu(&prev_de->rec_len, bytes);
2550}
2551
2552/*
2553 * We allocate enough clusters to fulfill "blocks_wanted", but set
2554 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2555 * rest automatically for us.
2556 *
2557 * *first_block_bh is a pointer to the 1st data block allocated to the
2558 * directory.
2559 */
2560static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2561 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002562 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002563 struct buffer_head **first_block_bh)
2564{
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002565 u32 alloc, dx_alloc, bit_off, len;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002566 struct super_block *sb = dir->i_sb;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002567 int ret, i, num_dx_leaves = 0, dx_inline = 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002568 credits = ocfs2_inline_to_extents_credits(sb);
2569 u64 dx_insert_blkno, blkno,
2570 bytes = blocks_wanted << sb->s_blocksize_bits;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002571 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2572 struct ocfs2_inode_info *oi = OCFS2_I(dir);
2573 struct ocfs2_alloc_context *data_ac;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002574 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002575 struct buffer_head *dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002576 struct buffer_head *dx_root_bh = NULL;
2577 struct buffer_head **dx_leaves = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002578 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2579 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002580 struct ocfs2_extent_tree et;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002581 struct ocfs2_extent_tree dx_et;
2582 int did_quota = 0, bytes_allocated = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002583
Joel Becker8d6220d2008-08-22 12:46:09 -07002584 ocfs2_init_dinode_extent_tree(&et, dir, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002585
2586 alloc = ocfs2_clusters_for_bytes(sb, bytes);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002587 dx_alloc = 0;
2588
2589 if (ocfs2_supports_indexed_dirs(osb)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002590 credits += ocfs2_add_dir_index_credits(sb);
2591
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002592 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2593 if (!dx_inline) {
2594 /* Add one more cluster for an index leaf */
2595 dx_alloc++;
2596 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2597 &num_dx_leaves);
2598 if (!dx_leaves) {
2599 ret = -ENOMEM;
2600 mlog_errno(ret);
2601 goto out;
2602 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002603 }
2604
2605 /* This gets us the dx_root */
2606 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2607 if (ret) {
2608 mlog_errno(ret);
2609 goto out;
2610 }
2611 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002612
2613 /*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002614 * We should never need more than 2 clusters for the unindexed
2615 * tree - maximum dirent size is far less than one block. In
2616 * fact, the only time we'd need more than one cluster is if
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002617 * blocksize == clustersize and the dirent won't fit in the
2618 * extra space that the expansion to a single block gives. As
2619 * of today, that only happens on 4k/4k file systems.
2620 */
2621 BUG_ON(alloc > 2);
2622
2623 ret = ocfs2_reserve_clusters(osb, alloc, &data_ac);
2624 if (ret) {
2625 mlog_errno(ret);
2626 goto out;
2627 }
2628
2629 down_write(&oi->ip_alloc_sem);
2630
2631 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02002632 * Prepare for worst case allocation scenario of two separate
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002633 * extents in the unindexed tree.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002634 */
2635 if (alloc == 2)
2636 credits += OCFS2_SUBALLOC_ALLOC;
2637
2638 handle = ocfs2_start_trans(osb, credits);
2639 if (IS_ERR(handle)) {
2640 ret = PTR_ERR(handle);
2641 mlog_errno(ret);
2642 goto out_sem;
2643 }
2644
Jan Karaa90714c2008-10-09 19:38:40 +02002645 if (vfs_dq_alloc_space_nodirty(dir,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002646 ocfs2_clusters_to_bytes(osb->sb,
2647 alloc + dx_alloc))) {
Jan Karaa90714c2008-10-09 19:38:40 +02002648 ret = -EDQUOT;
2649 goto out_commit;
2650 }
2651 did_quota = 1;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002652
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002653 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002654 /*
2655 * Allocate our index cluster first, to maximize the
2656 * possibility that unindexed leaves grow
2657 * contiguously.
2658 */
2659 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2660 dx_leaves, num_dx_leaves,
2661 &dx_insert_blkno);
2662 if (ret) {
2663 mlog_errno(ret);
2664 goto out_commit;
2665 }
2666 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2667 }
2668
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002669 /*
2670 * Try to claim as many clusters as the bitmap can give though
2671 * if we only get one now, that's enough to continue. The rest
2672 * will be claimed after the conversion to extents.
2673 */
2674 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off, &len);
2675 if (ret) {
2676 mlog_errno(ret);
2677 goto out_commit;
2678 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002679 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002680
2681 /*
2682 * Operations are carefully ordered so that we set up the new
2683 * data block first. The conversion from inline data to
2684 * extents follows.
2685 */
2686 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2687 dirdata_bh = sb_getblk(sb, blkno);
2688 if (!dirdata_bh) {
2689 ret = -EIO;
2690 mlog_errno(ret);
2691 goto out_commit;
2692 }
2693
2694 ocfs2_set_new_buffer_uptodate(dir, dirdata_bh);
2695
Joel Becker13723d02008-10-17 19:25:01 -07002696 ret = ocfs2_journal_access_db(handle, dir, dirdata_bh,
2697 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002698 if (ret) {
2699 mlog_errno(ret);
2700 goto out_commit;
2701 }
2702
2703 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2704 memset(dirdata_bh->b_data + i_size_read(dir), 0,
2705 sb->s_blocksize - i_size_read(dir));
Mark Fasheh87d35a72008-12-10 17:36:25 -08002706 ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), sb);
2707 if (ocfs2_supports_dir_trailer(osb))
2708 ocfs2_init_dir_trailer(dir, dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002709
2710 ret = ocfs2_journal_dirty(handle, dirdata_bh);
2711 if (ret) {
2712 mlog_errno(ret);
2713 goto out_commit;
2714 }
2715
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002716 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2717 /*
2718 * Dx dirs with an external cluster need to do this up
2719 * front. Inline dx root's get handled later, after
2720 * we've allocated our root block.
2721 */
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002722 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
2723 num_dx_leaves, dirdata_bh);
2724 if (ret) {
2725 mlog_errno(ret);
2726 goto out_commit;
2727 }
2728 }
2729
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002730 /*
2731 * Set extent, i_size, etc on the directory. After this, the
2732 * inode should contain the same exact dirents as before and
2733 * be fully accessible from system calls.
2734 *
2735 * We let the later dirent insert modify c/mtime - to the user
2736 * the data hasn't changed.
2737 */
Joel Becker13723d02008-10-17 19:25:01 -07002738 ret = ocfs2_journal_access_di(handle, dir, di_bh,
2739 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002740 if (ret) {
2741 mlog_errno(ret);
2742 goto out_commit;
2743 }
2744
2745 spin_lock(&oi->ip_lock);
2746 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
2747 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2748 spin_unlock(&oi->ip_lock);
2749
2750 ocfs2_dinode_new_extent_list(dir, di);
2751
2752 i_size_write(dir, sb->s_blocksize);
2753 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2754
2755 di->i_size = cpu_to_le64(sb->s_blocksize);
2756 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
2757 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002758
2759 /*
2760 * This should never fail as our extent list is empty and all
2761 * related blocks have been journaled already.
2762 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07002763 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0, blkno, len,
2764 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002765 if (ret) {
2766 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08002767 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002768 }
2769
Mark Fasheh9780eb62008-08-05 11:32:46 -07002770 /*
2771 * Set i_blocks after the extent insert for the most up to
2772 * date ip_clusters value.
2773 */
2774 dir->i_blocks = ocfs2_inode_sector_count(dir);
2775
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002776 ret = ocfs2_journal_dirty(handle, di_bh);
2777 if (ret) {
2778 mlog_errno(ret);
2779 goto out_commit;
2780 }
2781
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002782 if (ocfs2_supports_indexed_dirs(osb)) {
2783 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002784 meta_ac, dx_inline,
2785 &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002786 if (ret) {
2787 mlog_errno(ret);
2788 goto out_commit;
2789 }
2790
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002791 if (dx_inline) {
2792 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
2793 dirdata_bh);
2794 } else {
2795 ocfs2_init_dx_root_extent_tree(&dx_et, dir, dx_root_bh);
2796 ret = ocfs2_insert_extent(osb, handle, dir, &dx_et, 0,
2797 dx_insert_blkno, 1, 0, NULL);
2798 if (ret)
2799 mlog_errno(ret);
2800 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002801 }
2802
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002803 /*
2804 * We asked for two clusters, but only got one in the 1st
2805 * pass. Claim the 2nd cluster as a separate extent.
2806 */
2807 if (alloc > len) {
2808 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
2809 &len);
2810 if (ret) {
2811 mlog_errno(ret);
2812 goto out_commit;
2813 }
2814 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2815
Joel Beckerf99b9b72008-08-20 19:36:33 -07002816 ret = ocfs2_insert_extent(osb, handle, dir, &et, 1,
2817 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002818 if (ret) {
2819 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08002820 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002821 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002822 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002823 }
2824
2825 *first_block_bh = dirdata_bh;
2826 dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002827 if (ocfs2_supports_indexed_dirs(osb)) {
2828 unsigned int off;
2829
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002830 if (!dx_inline) {
2831 /*
2832 * We need to return the correct block within the
2833 * cluster which should hold our entry.
2834 */
2835 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
2836 &lookup->dl_hinfo);
2837 get_bh(dx_leaves[off]);
2838 lookup->dl_dx_leaf_bh = dx_leaves[off];
2839 }
2840 lookup->dl_dx_root_bh = dx_root_bh;
2841 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002842 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002843
2844out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02002845 if (ret < 0 && did_quota)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002846 vfs_dq_free_space_nodirty(dir, bytes_allocated);
2847
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002848 ocfs2_commit_trans(osb, handle);
2849
2850out_sem:
2851 up_write(&oi->ip_alloc_sem);
2852
2853out:
2854 if (data_ac)
2855 ocfs2_free_alloc_context(data_ac);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002856 if (meta_ac)
2857 ocfs2_free_alloc_context(meta_ac);
2858
2859 if (dx_leaves) {
2860 for (i = 0; i < num_dx_leaves; i++)
2861 brelse(dx_leaves[i]);
2862 kfree(dx_leaves);
2863 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002864
2865 brelse(dirdata_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002866 brelse(dx_root_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002867
2868 return ret;
2869}
2870
Mark Fashehccd979b2005-12-15 14:31:24 -08002871/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07002872static int ocfs2_do_extend_dir(struct super_block *sb,
2873 handle_t *handle,
2874 struct inode *dir,
2875 struct buffer_head *parent_fe_bh,
2876 struct ocfs2_alloc_context *data_ac,
2877 struct ocfs2_alloc_context *meta_ac,
2878 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08002879{
2880 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02002881 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07002882 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08002883
2884 spin_lock(&OCFS2_I(dir)->ip_lock);
2885 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
2886 spin_unlock(&OCFS2_I(dir)->ip_lock);
2887
2888 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002889 u32 offset = OCFS2_I(dir)->ip_clusters;
2890
Jan Karaa90714c2008-10-09 19:38:40 +02002891 if (vfs_dq_alloc_space_nodirty(dir,
2892 ocfs2_clusters_to_bytes(sb, 1))) {
2893 status = -EDQUOT;
2894 goto bail;
2895 }
2896 did_quota = 1;
2897
Tao Ma0eb8d47e2008-08-18 17:38:45 +08002898 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
2899 1, 0, parent_fe_bh, handle,
2900 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002901 BUG_ON(status == -EAGAIN);
2902 if (status < 0) {
2903 mlog_errno(status);
2904 goto bail;
2905 }
2906 }
2907
Mark Fasheh8110b072007-03-22 16:53:23 -07002908 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
2909 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08002910 if (status < 0) {
2911 mlog_errno(status);
2912 goto bail;
2913 }
2914
2915 *new_bh = sb_getblk(sb, p_blkno);
2916 if (!*new_bh) {
2917 status = -EIO;
2918 mlog_errno(status);
2919 goto bail;
2920 }
2921 status = 0;
2922bail:
Jan Karaa90714c2008-10-09 19:38:40 +02002923 if (did_quota && status < 0)
2924 vfs_dq_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08002925 mlog_exit(status);
2926 return status;
2927}
2928
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002929/*
2930 * Assumes you already have a cluster lock on the directory.
2931 *
2932 * 'blocks_wanted' is only used if we have an inline directory which
2933 * is to be turned into an extent based one. The size of the dirent to
2934 * insert might be larger than the space gained by growing to just one
2935 * block, so we may have to grow the inode by two blocks in that case.
2936 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002937static int ocfs2_extend_dir(struct ocfs2_super *osb,
2938 struct inode *dir,
2939 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002940 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002941 struct ocfs2_dir_lookup_result *lookup,
Mark Fashehccd979b2005-12-15 14:31:24 -08002942 struct buffer_head **new_de_bh)
2943{
2944 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07002945 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08002946 loff_t dir_i_size;
2947 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08002948 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08002949 struct ocfs2_alloc_context *data_ac = NULL;
2950 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07002951 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08002952 struct buffer_head *new_bh = NULL;
2953 struct ocfs2_dir_entry * de;
2954 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002955 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -08002956
2957 mlog_entry_void();
2958
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002959 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2960 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002961 blocks_wanted, lookup,
2962 &new_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002963 if (status) {
2964 mlog_errno(status);
2965 goto bail;
2966 }
2967
2968 if (blocks_wanted == 1) {
2969 /*
2970 * If the new dirent will fit inside the space
2971 * created by pushing out to one block, then
2972 * we can complete the operation
2973 * here. Otherwise we have to expand i_size
2974 * and format the 2nd block below.
2975 */
2976 BUG_ON(new_bh == NULL);
2977 goto bail_bh;
2978 }
2979
2980 /*
2981 * Get rid of 'new_bh' - we want to format the 2nd
2982 * data block and return that instead.
2983 */
2984 brelse(new_bh);
2985 new_bh = NULL;
2986
2987 dir_i_size = i_size_read(dir);
2988 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
2989 goto do_extend;
2990 }
2991
Mark Fashehccd979b2005-12-15 14:31:24 -08002992 dir_i_size = i_size_read(dir);
Mark Fashehb06970532006-03-03 10:24:33 -08002993 mlog(0, "extending dir %llu (i_size = %lld)\n",
2994 (unsigned long long)OCFS2_I(dir)->ip_blkno, dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08002995
Mark Fashehccd979b2005-12-15 14:31:24 -08002996 /* dir->i_size is always block aligned. */
2997 spin_lock(&OCFS2_I(dir)->ip_lock);
2998 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
2999 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker8d6220d2008-08-22 12:46:09 -07003000 ocfs2_init_dinode_extent_tree(&et, dir, parent_fe_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07003001 num_free_extents = ocfs2_num_free_extents(osb, dir, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08003002 if (num_free_extents < 0) {
3003 status = num_free_extents;
3004 mlog_errno(status);
3005 goto bail;
3006 }
3007
3008 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08003009 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003010 if (status < 0) {
3011 if (status != -ENOSPC)
3012 mlog_errno(status);
3013 goto bail;
3014 }
3015 }
3016
Mark Fashehda5cbf22006-10-06 18:34:35 -07003017 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003018 if (status < 0) {
3019 if (status != -ENOSPC)
3020 mlog_errno(status);
3021 goto bail;
3022 }
3023
Tao Ma811f9332008-08-18 17:38:43 +08003024 credits = ocfs2_calc_extend_credits(sb, el, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08003025 } else {
3026 spin_unlock(&OCFS2_I(dir)->ip_lock);
3027 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3028 }
3029
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003030do_extend:
Joel Beckeree19a772007-03-28 18:27:07 -07003031 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3032 drop_alloc_sem = 1;
3033
Mark Fasheh65eff9c2006-10-09 17:26:22 -07003034 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08003035 if (IS_ERR(handle)) {
3036 status = PTR_ERR(handle);
3037 handle = NULL;
3038 mlog_errno(status);
3039 goto bail;
3040 }
3041
3042 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3043 data_ac, meta_ac, &new_bh);
3044 if (status < 0) {
3045 mlog_errno(status);
3046 goto bail;
3047 }
3048
3049 ocfs2_set_new_buffer_uptodate(dir, new_bh);
3050
Joel Becker13723d02008-10-17 19:25:01 -07003051 status = ocfs2_journal_access_db(handle, dir, new_bh,
3052 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08003053 if (status < 0) {
3054 mlog_errno(status);
3055 goto bail;
3056 }
3057 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08003058
Mark Fashehccd979b2005-12-15 14:31:24 -08003059 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3060 de->inode = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003061 if (ocfs2_dir_has_trailer(dir)) {
3062 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
3063 ocfs2_init_dir_trailer(dir, new_bh);
3064 } else {
3065 de->rec_len = cpu_to_le16(sb->s_blocksize);
3066 }
Mark Fashehccd979b2005-12-15 14:31:24 -08003067 status = ocfs2_journal_dirty(handle, new_bh);
3068 if (status < 0) {
3069 mlog_errno(status);
3070 goto bail;
3071 }
3072
3073 dir_i_size += dir->i_sb->s_blocksize;
3074 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07003075 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08003076 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3077 if (status < 0) {
3078 mlog_errno(status);
3079 goto bail;
3080 }
3081
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003082bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08003083 *new_de_bh = new_bh;
3084 get_bh(*new_de_bh);
3085bail:
Joel Beckeree19a772007-03-28 18:27:07 -07003086 if (drop_alloc_sem)
3087 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08003088 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07003089 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08003090
3091 if (data_ac)
3092 ocfs2_free_alloc_context(data_ac);
3093 if (meta_ac)
3094 ocfs2_free_alloc_context(meta_ac);
3095
Mark Fasheha81cb882008-10-07 14:25:16 -07003096 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003097
3098 mlog_exit(status);
3099 return status;
3100}
3101
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003102static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3103 const char *name, int namelen,
3104 struct buffer_head **ret_de_bh,
3105 unsigned int *blocks_wanted)
3106{
3107 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003108 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003109 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3110 struct ocfs2_dir_entry *de, *last_de = NULL;
3111 char *de_buf, *limit;
3112 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003113 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3114
3115 /*
3116 * This calculates how many free bytes we'd have in block zero, should
3117 * this function force expansion to an extent tree.
3118 */
3119 if (ocfs2_supports_dir_trailer(OCFS2_SB(sb)))
3120 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3121 else
3122 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003123
3124 de_buf = di->id2.i_data.id_data;
3125 limit = de_buf + i_size_read(dir);
3126 rec_len = OCFS2_DIR_REC_LEN(namelen);
3127
3128 while (de_buf < limit) {
3129 de = (struct ocfs2_dir_entry *)de_buf;
3130
3131 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3132 ret = -ENOENT;
3133 goto out;
3134 }
3135 if (ocfs2_match(namelen, name, de)) {
3136 ret = -EEXIST;
3137 goto out;
3138 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003139 /*
3140 * No need to check for a trailing dirent record here as
3141 * they're not used for inline dirs.
3142 */
3143
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003144 if (ocfs2_dirent_would_fit(de, rec_len)) {
3145 /* Ok, we found a spot. Return this bh and let
3146 * the caller actually fill it in. */
3147 *ret_de_bh = di_bh;
3148 get_bh(*ret_de_bh);
3149 ret = 0;
3150 goto out;
3151 }
3152
3153 last_de = de;
3154 de_buf += le16_to_cpu(de->rec_len);
3155 offset += le16_to_cpu(de->rec_len);
3156 }
3157
3158 /*
3159 * We're going to require expansion of the directory - figure
3160 * out how many blocks we'll need so that a place for the
3161 * dirent can be found.
3162 */
3163 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003164 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003165 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3166 *blocks_wanted = 2;
3167
3168 ret = -ENOSPC;
3169out:
3170 return ret;
3171}
3172
3173static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3174 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08003175{
3176 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003177 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003178 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08003179 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003180 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08003181 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003182 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08003183
Joel Beckera22305c2008-11-13 14:49:17 -08003184 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3185 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003186 mlog_errno(status);
3187 goto bail;
3188 }
3189
3190 rec_len = OCFS2_DIR_REC_LEN(namelen);
3191 offset = 0;
3192 de = (struct ocfs2_dir_entry *) bh->b_data;
3193 while (1) {
3194 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3195 brelse(bh);
3196 bh = NULL;
3197
3198 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003199 /*
3200 * Caller will have to expand this
3201 * directory.
3202 */
3203 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08003204 goto bail;
3205 }
Joel Beckera22305c2008-11-13 14:49:17 -08003206 status = ocfs2_read_dir_block(dir,
3207 offset >> sb->s_blocksize_bits,
3208 &bh, 0);
3209 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003210 mlog_errno(status);
3211 goto bail;
3212 }
3213 /* move to next block */
3214 de = (struct ocfs2_dir_entry *) bh->b_data;
3215 }
3216 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3217 status = -ENOENT;
3218 goto bail;
3219 }
3220 if (ocfs2_match(namelen, name, de)) {
3221 status = -EEXIST;
3222 goto bail;
3223 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003224
3225 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3226 blocksize))
3227 goto next;
3228
Mark Fasheh8553cf42007-09-13 16:29:01 -07003229 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003230 /* Ok, we found a spot. Return this bh and let
3231 * the caller actually fill it in. */
3232 *ret_de_bh = bh;
3233 get_bh(*ret_de_bh);
3234 status = 0;
3235 goto bail;
3236 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003237next:
Mark Fashehccd979b2005-12-15 14:31:24 -08003238 offset += le16_to_cpu(de->rec_len);
3239 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3240 }
3241
3242 status = 0;
3243bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07003244 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003245
3246 mlog_exit(status);
3247 return status;
3248}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003249
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003250static int dx_leaf_sort_cmp(const void *a, const void *b)
3251{
3252 const struct ocfs2_dx_entry *entry1 = a;
3253 const struct ocfs2_dx_entry *entry2 = b;
3254 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3255 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3256 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3257 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3258
3259 if (major_hash1 > major_hash2)
3260 return 1;
3261 if (major_hash1 < major_hash2)
3262 return -1;
3263
3264 /*
3265 * It is not strictly necessary to sort by minor
3266 */
3267 if (minor_hash1 > minor_hash2)
3268 return 1;
3269 if (minor_hash1 < minor_hash2)
3270 return -1;
3271 return 0;
3272}
3273
3274static void dx_leaf_sort_swap(void *a, void *b, int size)
3275{
3276 struct ocfs2_dx_entry *entry1 = a;
3277 struct ocfs2_dx_entry *entry2 = b;
3278 struct ocfs2_dx_entry tmp;
3279
3280 BUG_ON(size != sizeof(*entry1));
3281
3282 tmp = *entry1;
3283 *entry1 = *entry2;
3284 *entry2 = tmp;
3285}
3286
3287static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3288{
3289 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3290 int i, num = le16_to_cpu(dl_list->de_num_used);
3291
3292 for (i = 0; i < (num - 1); i++) {
3293 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3294 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3295 return 0;
3296 }
3297
3298 return 1;
3299}
3300
3301/*
3302 * Find the optimal value to split this leaf on. This expects the leaf
3303 * entries to be in sorted order.
3304 *
3305 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3306 * the hash we want to insert.
3307 *
3308 * This function is only concerned with the major hash - that which
3309 * determines which cluster an item belongs to.
3310 */
3311static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3312 u32 leaf_cpos, u32 insert_hash,
3313 u32 *split_hash)
3314{
3315 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3316 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3317 int allsame;
3318
3319 /*
3320 * There's a couple rare, but nasty corner cases we have to
3321 * check for here. All of them involve a leaf where all value
3322 * have the same hash, which is what we look for first.
3323 *
3324 * Most of the time, all of the above is false, and we simply
3325 * pick the median value for a split.
3326 */
3327 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3328 if (allsame) {
3329 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3330
3331 if (val == insert_hash) {
3332 /*
3333 * No matter where we would choose to split,
3334 * the new entry would want to occupy the same
3335 * block as these. Since there's no space left
3336 * in their existing block, we know there
3337 * won't be space after the split.
3338 */
3339 return -ENOSPC;
3340 }
3341
3342 if (val == leaf_cpos) {
3343 /*
3344 * Because val is the same as leaf_cpos (which
3345 * is the smallest value this leaf can have),
3346 * yet is not equal to insert_hash, then we
3347 * know that insert_hash *must* be larger than
3348 * val (and leaf_cpos). At least cpos+1 in value.
3349 *
3350 * We also know then, that there cannot be an
3351 * adjacent extent (otherwise we'd be looking
3352 * at it). Choosing this value gives us a
3353 * chance to get some contiguousness.
3354 */
3355 *split_hash = leaf_cpos + 1;
3356 return 0;
3357 }
3358
3359 if (val > insert_hash) {
3360 /*
3361 * val can not be the same as insert hash, and
3362 * also must be larger than leaf_cpos. Also,
3363 * we know that there can't be a leaf between
3364 * cpos and val, otherwise the entries with
3365 * hash 'val' would be there.
3366 */
3367 *split_hash = val;
3368 return 0;
3369 }
3370
3371 *split_hash = insert_hash;
3372 return 0;
3373 }
3374
3375 /*
3376 * Since the records are sorted and the checks above
3377 * guaranteed that not all records in this block are the same,
3378 * we simple travel forward, from the median, and pick the 1st
3379 * record whose value is larger than leaf_cpos.
3380 */
3381 for (i = (num_used / 2); i < num_used; i++)
3382 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3383 leaf_cpos)
3384 break;
3385
3386 BUG_ON(i == num_used); /* Should be impossible */
3387 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3388 return 0;
3389}
3390
3391/*
3392 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3393 * larger than split_hash into new_dx_leaves. We use a temporary
3394 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3395 *
3396 * Since the block offset inside a leaf (cluster) is a constant mask
3397 * of minor_hash, we can optimize - an item at block offset X within
3398 * the original cluster, will be at offset X within the new cluster.
3399 */
3400static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3401 handle_t *handle,
3402 struct ocfs2_dx_leaf *tmp_dx_leaf,
3403 struct buffer_head **orig_dx_leaves,
3404 struct buffer_head **new_dx_leaves,
3405 int num_dx_leaves)
3406{
3407 int i, j, num_used;
3408 u32 major_hash;
3409 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3410 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3411 struct ocfs2_dx_entry *dx_entry;
3412
3413 tmp_list = &tmp_dx_leaf->dl_list;
3414
3415 for (i = 0; i < num_dx_leaves; i++) {
3416 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3417 orig_list = &orig_dx_leaf->dl_list;
3418 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3419 new_list = &new_dx_leaf->dl_list;
3420
3421 num_used = le16_to_cpu(orig_list->de_num_used);
3422
3423 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3424 tmp_list->de_num_used = cpu_to_le16(0);
3425 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3426
3427 for (j = 0; j < num_used; j++) {
3428 dx_entry = &orig_list->de_entries[j];
3429 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3430 if (major_hash >= split_hash)
3431 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3432 dx_entry);
3433 else
3434 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3435 dx_entry);
3436 }
3437 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3438
3439 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3440 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3441 }
3442}
3443
3444static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3445 struct ocfs2_dx_root_block *dx_root)
3446{
3447 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3448
3449 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list, 1);
3450 credits += ocfs2_quota_trans_credits(osb->sb);
3451 return credits;
3452}
3453
3454/*
3455 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3456 * half our entries into.
3457 */
3458static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3459 struct buffer_head *dx_root_bh,
3460 struct buffer_head *dx_leaf_bh,
3461 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3462 u64 leaf_blkno)
3463{
3464 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3465 int credits, ret, i, num_used, did_quota = 0;
3466 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3467 u64 orig_leaves_start;
3468 int num_dx_leaves;
3469 struct buffer_head **orig_dx_leaves = NULL;
3470 struct buffer_head **new_dx_leaves = NULL;
3471 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3472 struct ocfs2_extent_tree et;
3473 handle_t *handle = NULL;
3474 struct ocfs2_dx_root_block *dx_root;
3475 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3476
3477 mlog(0, "DX Dir: %llu, rebalance leaf leaf_blkno: %llu insert: %u\n",
3478 (unsigned long long)OCFS2_I(dir)->ip_blkno,
3479 (unsigned long long)leaf_blkno, insert_hash);
3480
3481 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
3482
3483 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3484 /*
3485 * XXX: This is a rather large limit. We should use a more
3486 * realistic value.
3487 */
3488 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3489 return -ENOSPC;
3490
3491 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3492 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3493 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3494 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3495 (unsigned long long)leaf_blkno, num_used);
3496 ret = -EIO;
3497 goto out;
3498 }
3499
3500 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3501 if (!orig_dx_leaves) {
3502 ret = -ENOMEM;
3503 mlog_errno(ret);
3504 goto out;
3505 }
3506
3507 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3508 if (!new_dx_leaves) {
3509 ret = -ENOMEM;
3510 mlog_errno(ret);
3511 goto out;
3512 }
3513
3514 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3515 if (ret) {
3516 if (ret != -ENOSPC)
3517 mlog_errno(ret);
3518 goto out;
3519 }
3520
3521 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3522 handle = ocfs2_start_trans(osb, credits);
3523 if (IS_ERR(handle)) {
3524 ret = PTR_ERR(handle);
3525 handle = NULL;
3526 mlog_errno(ret);
3527 goto out;
3528 }
3529
3530 if (vfs_dq_alloc_space_nodirty(dir,
3531 ocfs2_clusters_to_bytes(dir->i_sb, 1))) {
3532 ret = -EDQUOT;
3533 goto out_commit;
3534 }
3535 did_quota = 1;
3536
3537 ret = ocfs2_journal_access_dl(handle, dir, dx_leaf_bh,
3538 OCFS2_JOURNAL_ACCESS_WRITE);
3539 if (ret) {
3540 mlog_errno(ret);
3541 goto out_commit;
3542 }
3543
3544 /*
3545 * This block is changing anyway, so we can sort it in place.
3546 */
3547 sort(dx_leaf->dl_list.de_entries, num_used,
3548 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3549 dx_leaf_sort_swap);
3550
3551 ret = ocfs2_journal_dirty(handle, dx_leaf_bh);
3552 if (ret) {
3553 mlog_errno(ret);
3554 goto out_commit;
3555 }
3556
3557 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3558 &split_hash);
3559 if (ret) {
3560 mlog_errno(ret);
3561 goto out_commit;
3562 }
3563
3564 mlog(0, "Split leaf (%u) at %u, insert major hash is %u\n",
3565 leaf_cpos, split_hash, insert_hash);
3566
3567 /*
3568 * We have to carefully order operations here. There are items
3569 * which want to be in the new cluster before insert, but in
3570 * order to put those items in the new cluster, we alter the
3571 * old cluster. A failure to insert gets nasty.
3572 *
3573 * So, start by reserving writes to the old
3574 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3575 * the new cluster for us, before inserting it. The insert
3576 * won't happen if there's an error before that. Once the
3577 * insert is done then, we can transfer from one leaf into the
3578 * other without fear of hitting any error.
3579 */
3580
3581 /*
3582 * The leaf transfer wants some scratch space so that we don't
3583 * wind up doing a bunch of expensive memmove().
3584 */
3585 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3586 if (!tmp_dx_leaf) {
3587 ret = -ENOMEM;
3588 mlog_errno(ret);
3589 goto out_commit;
3590 }
3591
3592 orig_leaves_start = leaf_blkno & ~(osb->s_clustersize_bits -
3593 osb->sb->s_blocksize_bits);
3594 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3595 orig_dx_leaves);
3596 if (ret) {
3597 mlog_errno(ret);
3598 goto out_commit;
3599 }
3600
3601 for (i = 0; i < num_dx_leaves; i++) {
3602 ret = ocfs2_journal_access_dl(handle, dir, orig_dx_leaves[i],
3603 OCFS2_JOURNAL_ACCESS_WRITE);
3604 if (ret) {
3605 mlog_errno(ret);
3606 goto out_commit;
3607 }
3608 }
3609
3610 cpos = split_hash;
3611 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3612 data_ac, meta_ac, new_dx_leaves,
3613 num_dx_leaves);
3614 if (ret) {
3615 mlog_errno(ret);
3616 goto out_commit;
3617 }
3618
3619 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3620 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3621
3622out_commit:
3623 if (ret < 0 && did_quota)
3624 vfs_dq_free_space_nodirty(dir,
3625 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3626
3627 ocfs2_commit_trans(osb, handle);
3628
3629out:
3630 if (orig_dx_leaves || new_dx_leaves) {
3631 for (i = 0; i < num_dx_leaves; i++) {
3632 if (orig_dx_leaves)
3633 brelse(orig_dx_leaves[i]);
3634 if (new_dx_leaves)
3635 brelse(new_dx_leaves[i]);
3636 }
3637 kfree(orig_dx_leaves);
3638 kfree(new_dx_leaves);
3639 }
3640
3641 if (meta_ac)
3642 ocfs2_free_alloc_context(meta_ac);
3643 if (data_ac)
3644 ocfs2_free_alloc_context(data_ac);
3645
3646 kfree(tmp_dx_leaf);
3647 return ret;
3648}
3649
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003650static int ocfs2_expand_inline_dx_root(struct inode *dir,
3651 struct buffer_head *dx_root_bh)
3652{
3653 int ret, num_dx_leaves, i, j, did_quota = 0;
3654 struct buffer_head **dx_leaves = NULL;
3655 struct ocfs2_extent_tree et;
3656 u64 insert_blkno;
3657 struct ocfs2_alloc_context *data_ac = NULL;
3658 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
3659 handle_t *handle = NULL;
3660 struct ocfs2_dx_root_block *dx_root;
3661 struct ocfs2_dx_entry_list *entry_list;
3662 struct ocfs2_dx_entry *dx_entry;
3663 struct ocfs2_dx_leaf *target_leaf;
3664
3665 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
3666 if (ret) {
3667 mlog_errno(ret);
3668 goto out;
3669 }
3670
3671 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3672 if (!dx_leaves) {
3673 ret = -ENOMEM;
3674 mlog_errno(ret);
3675 goto out;
3676 }
3677
3678 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
3679 if (IS_ERR(handle)) {
3680 ret = PTR_ERR(handle);
3681 mlog_errno(ret);
3682 goto out;
3683 }
3684
3685 if (vfs_dq_alloc_space_nodirty(dir,
3686 ocfs2_clusters_to_bytes(osb->sb, 1))) {
3687 ret = -EDQUOT;
3688 goto out_commit;
3689 }
3690 did_quota = 1;
3691
3692 /*
3693 * We do this up front, before the allocation, so that a
3694 * failure to add the dx_root_bh to the journal won't result
3695 * us losing clusters.
3696 */
3697 ret = ocfs2_journal_access_dr(handle, dir, dx_root_bh,
3698 OCFS2_JOURNAL_ACCESS_WRITE);
3699 if (ret) {
3700 mlog_errno(ret);
3701 goto out_commit;
3702 }
3703
3704 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
3705 num_dx_leaves, &insert_blkno);
3706 if (ret) {
3707 mlog_errno(ret);
3708 goto out_commit;
3709 }
3710
3711 /*
3712 * Transfer the entries from our dx_root into the appropriate
3713 * block
3714 */
3715 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3716 entry_list = &dx_root->dr_entries;
3717
3718 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
3719 dx_entry = &entry_list->de_entries[i];
3720
3721 j = __ocfs2_dx_dir_hash_idx(osb,
3722 le32_to_cpu(dx_entry->dx_minor_hash));
3723 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
3724
3725 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
3726
3727 /* Each leaf has been passed to the journal already
3728 * via __ocfs2_dx_dir_new_cluster() */
3729 }
3730
3731 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
3732 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
3733 offsetof(struct ocfs2_dx_root_block, dr_list));
3734 dx_root->dr_list.l_count =
3735 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
3736
3737 /* This should never fail considering we start with an empty
3738 * dx_root. */
3739 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
3740 ret = ocfs2_insert_extent(osb, handle, dir, &et, 0,
3741 insert_blkno, 1, 0, NULL);
3742 if (ret)
3743 mlog_errno(ret);
3744 did_quota = 0;
3745
3746 ocfs2_journal_dirty(handle, dx_root_bh);
3747
3748out_commit:
3749 if (ret < 0 && did_quota)
3750 vfs_dq_free_space_nodirty(dir,
3751 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3752
3753 ocfs2_commit_trans(osb, handle);
3754
3755out:
3756 if (data_ac)
3757 ocfs2_free_alloc_context(data_ac);
3758
3759 if (dx_leaves) {
3760 for (i = 0; i < num_dx_leaves; i++)
3761 brelse(dx_leaves[i]);
3762 kfree(dx_leaves);
3763 }
3764 return ret;
3765}
3766
3767static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
3768{
3769 struct ocfs2_dx_root_block *dx_root;
3770 struct ocfs2_dx_entry_list *entry_list;
3771
3772 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3773 entry_list = &dx_root->dr_entries;
3774
3775 if (le16_to_cpu(entry_list->de_num_used) >=
3776 le16_to_cpu(entry_list->de_count))
3777 return -ENOSPC;
3778
3779 return 0;
3780}
3781
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003782static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3783 struct buffer_head *di_bh, const char *name,
3784 int namelen,
3785 struct ocfs2_dir_lookup_result *lookup)
3786{
3787 int ret, rebalanced = 0;
3788 struct buffer_head *dx_root_bh = NULL;
3789 struct ocfs2_dx_root_block *dx_root;
3790 struct buffer_head *dx_leaf_bh = NULL;
3791 struct ocfs2_dx_leaf *dx_leaf;
3792 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3793 u64 blkno;
3794 u32 leaf_cpos;
3795
3796 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
3797 if (ret) {
3798 mlog_errno(ret);
3799 goto out;
3800 }
3801
3802 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003803 if (ocfs2_dx_root_inline(dx_root)) {
3804 ret = ocfs2_inline_dx_has_space(dx_root_bh);
3805
3806 if (ret == 0)
3807 goto search_el;
3808
3809 /*
3810 * We ran out of room in the root block. Expand it to
3811 * an extent, then allow ocfs2_find_dir_space_dx to do
3812 * the rest.
3813 */
3814 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
3815 if (ret) {
3816 mlog_errno(ret);
3817 goto out;
3818 }
3819 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003820
3821restart_search:
3822 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3823 &leaf_cpos, &blkno);
3824 if (ret) {
3825 mlog_errno(ret);
3826 goto out;
3827 }
3828
3829 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3830 if (ret) {
3831 mlog_errno(ret);
3832 goto out;
3833 }
3834
3835 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3836
3837 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3838 le16_to_cpu(dx_leaf->dl_list.de_count)) {
3839 if (rebalanced) {
3840 /*
3841 * Rebalancing should have provided us with
3842 * space in an appropriate leaf.
3843 *
3844 * XXX: Is this an abnormal condition then?
3845 * Should we print a message here?
3846 */
3847 ret = -ENOSPC;
3848 goto out;
3849 }
3850
3851 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3852 &lookup->dl_hinfo, leaf_cpos,
3853 blkno);
3854 if (ret) {
3855 if (ret != -ENOSPC)
3856 mlog_errno(ret);
3857 goto out;
3858 }
3859
3860 /*
3861 * Restart the lookup. The rebalance might have
3862 * changed which block our item fits into. Mark our
3863 * progress, so we only execute this once.
3864 */
3865 brelse(dx_leaf_bh);
3866 dx_leaf_bh = NULL;
3867 rebalanced = 1;
3868 goto restart_search;
3869 }
3870
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003871search_el:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003872 lookup->dl_dx_leaf_bh = dx_leaf_bh;
3873 dx_leaf_bh = NULL;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003874 lookup->dl_dx_root_bh = dx_root_bh;
3875 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003876
3877out:
3878 brelse(dx_leaf_bh);
3879 brelse(dx_root_bh);
3880 return ret;
3881}
3882
Mark Fasheh4a12ca32008-11-12 15:43:34 -08003883/*
3884 * Get a directory ready for insert. Any directory allocation required
3885 * happens here. Success returns zero, and enough context in the dir
3886 * lookup result that ocfs2_add_entry() will be able complete the task
3887 * with minimal performance impact.
3888 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003889int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
3890 struct inode *dir,
3891 struct buffer_head *parent_fe_bh,
3892 const char *name,
3893 int namelen,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08003894 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003895{
3896 int ret;
3897 unsigned int blocks_wanted = 1;
3898 struct buffer_head *bh = NULL;
3899
3900 mlog(0, "getting ready to insert namelen %d into dir %llu\n",
3901 namelen, (unsigned long long)OCFS2_I(dir)->ip_blkno);
3902
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003903 if (!namelen) {
3904 ret = -EINVAL;
3905 mlog_errno(ret);
3906 goto out;
3907 }
3908
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003909 /*
3910 * Do this up front to reduce confusion.
3911 *
3912 * The directory might start inline, then be turned into an
3913 * indexed one, in which case we'd need to hash deep inside
3914 * ocfs2_find_dir_space_id(). Since
3915 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
3916 * done, there seems no point in spreading out the calls. We
3917 * can optimize away the case where the file system doesn't
3918 * support indexing.
3919 */
3920 if (ocfs2_supports_indexed_dirs(osb))
3921 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
3922
3923 if (ocfs2_dir_indexed(dir)) {
3924 ret = ocfs2_find_dir_space_dx(osb, dir, parent_fe_bh, name,
3925 namelen, lookup);
3926 if (ret) {
3927 mlog_errno(ret);
3928 goto out;
3929 }
3930
3931 /*
3932 * We intentionally fall through so that the unindexed
3933 * tree can also be prepared.
3934 */
3935 }
3936
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003937 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
3938 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
3939 namelen, &bh, &blocks_wanted);
3940 } else
3941 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
3942
3943 if (ret && ret != -ENOSPC) {
3944 mlog_errno(ret);
3945 goto out;
3946 }
3947
3948 if (ret == -ENOSPC) {
3949 /*
3950 * We have to expand the directory to add this name.
3951 */
3952 BUG_ON(bh);
3953
3954 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003955 lookup, &bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003956 if (ret) {
3957 if (ret != -ENOSPC)
3958 mlog_errno(ret);
3959 goto out;
3960 }
3961
3962 BUG_ON(!bh);
3963 }
3964
Mark Fasheh4a12ca32008-11-12 15:43:34 -08003965 lookup->dl_leaf_bh = bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003966 bh = NULL;
3967out:
Mark Fasheha81cb882008-10-07 14:25:16 -07003968 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003969 return ret;
3970}
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003971
3972static int ocfs2_dx_dir_remove_index(struct inode *dir,
3973 struct buffer_head *di_bh,
3974 struct buffer_head *dx_root_bh)
3975{
3976 int ret;
3977 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
3978 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3979 struct ocfs2_dx_root_block *dx_root;
3980 struct inode *dx_alloc_inode = NULL;
3981 struct buffer_head *dx_alloc_bh = NULL;
3982 handle_t *handle;
3983 u64 blk;
3984 u16 bit;
3985 u64 bg_blkno;
3986
3987 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
3988
3989 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
3990 EXTENT_ALLOC_SYSTEM_INODE,
3991 le16_to_cpu(dx_root->dr_suballoc_slot));
3992 if (!dx_alloc_inode) {
3993 ret = -ENOMEM;
3994 mlog_errno(ret);
3995 goto out;
3996 }
3997 mutex_lock(&dx_alloc_inode->i_mutex);
3998
3999 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4000 if (ret) {
4001 mlog_errno(ret);
4002 goto out_mutex;
4003 }
4004
4005 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4006 if (IS_ERR(handle)) {
4007 ret = PTR_ERR(handle);
4008 mlog_errno(ret);
4009 goto out_unlock;
4010 }
4011
4012 ret = ocfs2_journal_access_di(handle, dir, di_bh,
4013 OCFS2_JOURNAL_ACCESS_WRITE);
4014 if (ret) {
4015 mlog_errno(ret);
4016 goto out_commit;
4017 }
4018
4019 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4020 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4021 di->i_dx_root = cpu_to_le64(0ULL);
4022
4023 ocfs2_journal_dirty(handle, di_bh);
4024
4025 blk = le64_to_cpu(dx_root->dr_blkno);
4026 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4027 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4028 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4029 bit, bg_blkno, 1);
4030 if (ret)
4031 mlog_errno(ret);
4032
4033out_commit:
4034 ocfs2_commit_trans(osb, handle);
4035
4036out_unlock:
4037 ocfs2_inode_unlock(dx_alloc_inode, 1);
4038
4039out_mutex:
4040 mutex_unlock(&dx_alloc_inode->i_mutex);
4041 brelse(dx_alloc_bh);
4042out:
4043 iput(dx_alloc_inode);
4044 return ret;
4045}
4046
4047int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4048{
4049 int ret;
4050 unsigned int uninitialized_var(clen);
4051 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4052 u64 uninitialized_var(blkno);
4053 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4054 struct buffer_head *dx_root_bh = NULL;
4055 struct ocfs2_dx_root_block *dx_root;
4056 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4057 struct ocfs2_cached_dealloc_ctxt dealloc;
4058 struct ocfs2_extent_tree et;
4059
4060 ocfs2_init_dealloc_ctxt(&dealloc);
4061
4062 if (!ocfs2_dir_indexed(dir))
4063 return 0;
4064
4065 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4066 if (ret) {
4067 mlog_errno(ret);
4068 goto out;
4069 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004070 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4071
4072 if (ocfs2_dx_root_inline(dx_root))
4073 goto remove_index;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004074
4075 ocfs2_init_dx_root_extent_tree(&et, dir, dx_root_bh);
4076
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004077 /* XXX: What if dr_clusters is too large? */
4078 while (le32_to_cpu(dx_root->dr_clusters)) {
4079 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4080 major_hash, &cpos, &blkno, &clen);
4081 if (ret) {
4082 mlog_errno(ret);
4083 goto out;
4084 }
4085
4086 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4087
4088 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen,
4089 &dealloc);
4090 if (ret) {
4091 mlog_errno(ret);
4092 goto out;
4093 }
4094
4095 if (cpos == 0)
4096 break;
4097
4098 major_hash = cpos - 1;
4099 }
4100
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004101remove_index:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004102 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4103 if (ret) {
4104 mlog_errno(ret);
4105 goto out;
4106 }
4107
4108 ocfs2_remove_from_cache(dir, dx_root_bh);
4109out:
4110 ocfs2_schedule_truncate_log_flush(osb, 1);
4111 ocfs2_run_deallocs(osb, &dealloc);
4112
4113 brelse(dx_root_bh);
4114 return ret;
4115}