blob: 25f03af09237a78b700ee04d7dfdd2cddacbd32c [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 *
Jakub Wilk762515a2015-04-14 15:43:41 -070021 * Copyright (C) 1991, 1992 Linus Torvalds
Mark Fashehccd979b2005-12-15 14:31:24 -080022 *
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
Mark Fashehccd979b2005-12-15 14:31:24 -080046#include <cluster/masklog.h>
47
48#include "ocfs2.h"
49
50#include "alloc.h"
Joel Beckerc175a512008-12-10 17:58:22 -080051#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052#include "dir.h"
53#include "dlmglue.h"
54#include "extent_map.h"
55#include "file.h"
56#include "inode.h"
57#include "journal.h"
58#include "namei.h"
59#include "suballoc.h"
Mark Fasheh316f4b92007-09-07 18:21:26 -070060#include "super.h"
Mark Fasheh9b7895e2008-11-12 16:27:44 -080061#include "sysfile.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080062#include "uptodate.h"
Tao Maf1088d472011-02-23 22:30:23 +080063#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080064
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)
Mark Fasheh316f4b92007-09-07 18:21:26 -070070
Mark Fashehccd979b2005-12-15 14:31:24 -080071static unsigned char ocfs2_filetype_table[] = {
72 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
73};
74
Mark Fasheh316f4b92007-09-07 18:21:26 -070075static int ocfs2_do_extend_dir(struct super_block *sb,
76 handle_t *handle,
77 struct inode *dir,
78 struct buffer_head *parent_fe_bh,
79 struct ocfs2_alloc_context *data_ac,
80 struct ocfs2_alloc_context *meta_ac,
81 struct buffer_head **new_bh);
Mark Fashehe7c17e42009-01-29 18:17:46 -080082static int ocfs2_dir_indexed(struct inode *inode);
Mark Fasheh316f4b92007-09-07 18:21:26 -070083
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 */
Mark Fashehe7c17e42009-01-29 18:17:46 -080088static int ocfs2_supports_dir_trailer(struct inode *dir)
Mark Fasheh87d35a72008-12-10 17:36:25 -080089{
Mark Fashehe7c17e42009-01-29 18:17:46 -080090 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
91
Mark Fasheh87d35a72008-12-10 17:36:25 -080092 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
93 return 0;
94
Mark Fashehe7c17e42009-01-29 18:17:46 -080095 return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
Mark Fasheh87d35a72008-12-10 17:36:25 -080096}
97
Mark Fashehe7c17e42009-01-29 18:17:46 -080098/*
99 * "new' here refers to the point at which we're creating a new
100 * directory via "mkdir()", but also when we're expanding an inline
101 * directory. In either case, we don't yet have the indexing bit set
102 * on the directory, so the standard checks will fail in when metaecc
103 * is turned off. Only directory-initialization type functions should
104 * use this then. Everything else wants ocfs2_supports_dir_trailer()
105 */
106static int ocfs2_new_dir_wants_trailer(struct inode *dir)
Mark Fasheh87d35a72008-12-10 17:36:25 -0800107{
Mark Fashehe7c17e42009-01-29 18:17:46 -0800108 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
109
110 return ocfs2_meta_ecc(osb) ||
111 ocfs2_supports_indexed_dirs(osb);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800112}
113
114static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
115{
116 return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
117}
118
119#define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
120
Joel Beckerc175a512008-12-10 17:58:22 -0800121/* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
122 * them more consistent? */
123struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
124 void *data)
125{
126 char *p = data;
127
128 p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
129 return (struct ocfs2_dir_block_trailer *)p;
130}
131
Mark Fasheh87d35a72008-12-10 17:36:25 -0800132/*
133 * XXX: This is executed once on every dirent. We should consider optimizing
134 * it.
135 */
136static int ocfs2_skip_dir_trailer(struct inode *dir,
137 struct ocfs2_dir_entry *de,
138 unsigned long offset,
139 unsigned long blklen)
140{
141 unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
142
Mark Fashehe7c17e42009-01-29 18:17:46 -0800143 if (!ocfs2_supports_dir_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -0800144 return 0;
145
146 if (offset != toff)
147 return 0;
148
149 return 1;
150}
151
152static void ocfs2_init_dir_trailer(struct inode *inode,
Mark Fashehe7c17e42009-01-29 18:17:46 -0800153 struct buffer_head *bh, u16 rec_len)
Mark Fasheh87d35a72008-12-10 17:36:25 -0800154{
155 struct ocfs2_dir_block_trailer *trailer;
156
157 trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
158 strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
159 trailer->db_compat_rec_len =
160 cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
161 trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
162 trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
Mark Fashehe7c17e42009-01-29 18:17:46 -0800163 trailer->db_free_rec_len = cpu_to_le16(rec_len);
164}
165/*
166 * Link an unindexed block with a dir trailer structure into the index free
167 * list. This function will modify dirdata_bh, but assumes you've already
168 * passed it to the journal.
169 */
170static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
171 struct buffer_head *dx_root_bh,
172 struct buffer_head *dirdata_bh)
173{
174 int ret;
175 struct ocfs2_dx_root_block *dx_root;
176 struct ocfs2_dir_block_trailer *trailer;
177
Joel Becker0cf2f762009-02-12 16:41:25 -0800178 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -0800179 OCFS2_JOURNAL_ACCESS_WRITE);
180 if (ret) {
181 mlog_errno(ret);
182 goto out;
183 }
184 trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
185 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
186
187 trailer->db_free_next = dx_root->dr_free_blk;
188 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
189
190 ocfs2_journal_dirty(handle, dx_root_bh);
191
192out:
193 return ret;
194}
195
196static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
197{
198 return res->dl_prev_leaf_bh == NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -0800199}
200
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800201void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
202{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800203 brelse(res->dl_dx_root_bh);
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800204 brelse(res->dl_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800205 brelse(res->dl_dx_leaf_bh);
Mark Fashehe7c17e42009-01-29 18:17:46 -0800206 brelse(res->dl_prev_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800207}
208
209static int ocfs2_dir_indexed(struct inode *inode)
210{
211 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
212 return 1;
213 return 0;
214}
215
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800216static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
217{
218 return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
219}
220
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800221/*
222 * Hashing code adapted from ext3
223 */
224#define DELTA 0x9E3779B9
225
226static void TEA_transform(__u32 buf[4], __u32 const in[])
227{
228 __u32 sum = 0;
229 __u32 b0 = buf[0], b1 = buf[1];
230 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
231 int n = 16;
232
233 do {
234 sum += DELTA;
235 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
236 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
237 } while (--n);
238
239 buf[0] += b0;
240 buf[1] += b1;
241}
242
243static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
244{
245 __u32 pad, val;
246 int i;
247
248 pad = (__u32)len | ((__u32)len << 8);
249 pad |= pad << 16;
250
251 val = pad;
252 if (len > num*4)
253 len = num * 4;
254 for (i = 0; i < len; i++) {
255 if ((i % 4) == 0)
256 val = pad;
257 val = msg[i] + (val << 8);
258 if ((i % 4) == 3) {
259 *buf++ = val;
260 val = pad;
261 num--;
262 }
263 }
264 if (--num >= 0)
265 *buf++ = val;
266 while (--num >= 0)
267 *buf++ = pad;
268}
269
270static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
271 struct ocfs2_dx_hinfo *hinfo)
272{
273 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
274 const char *p;
275 __u32 in[8], buf[4];
276
277 /*
278 * XXX: Is this really necessary, if the index is never looked
279 * at by readdir? Is a hash value of '0' a bad idea?
280 */
281 if ((len == 1 && !strncmp(".", name, 1)) ||
282 (len == 2 && !strncmp("..", name, 2))) {
283 buf[0] = buf[1] = 0;
284 goto out;
285 }
286
287#ifdef OCFS2_DEBUG_DX_DIRS
288 /*
289 * This makes it very easy to debug indexing problems. We
290 * should never allow this to be selected without hand editing
291 * this file though.
292 */
293 buf[0] = buf[1] = len;
294 goto out;
295#endif
296
297 memcpy(buf, osb->osb_dx_seed, sizeof(buf));
298
299 p = name;
300 while (len > 0) {
301 str2hashbuf(p, len, in, 4);
302 TEA_transform(buf, in);
303 len -= 16;
304 p += 16;
305 }
306
307out:
308 hinfo->major_hash = buf[0];
309 hinfo->minor_hash = buf[1];
Mark Fasheh4a12ca32008-11-12 15:43:34 -0800310}
311
Mark Fasheh87d35a72008-12-10 17:36:25 -0800312/*
Mark Fasheh23193e52007-09-12 13:01:18 -0700313 * bh passed here can be an inode block or a dir data block, depending
314 * on the inode inline data flag.
315 */
Mark Fasheh5eae5b92007-09-10 17:50:51 -0700316static int ocfs2_check_dir_entry(struct inode * dir,
317 struct ocfs2_dir_entry * de,
318 struct buffer_head * bh,
319 unsigned long offset)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700320{
321 const char *error_msg = NULL;
322 const int rlen = le16_to_cpu(de->rec_len);
323
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800324 if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
Mark Fasheh316f4b92007-09-07 18:21:26 -0700325 error_msg = "rec_len is smaller than minimal";
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800326 else if (unlikely(rlen % 4 != 0))
Mark Fasheh316f4b92007-09-07 18:21:26 -0700327 error_msg = "rec_len % 4 != 0";
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800328 else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
Mark Fasheh316f4b92007-09-07 18:21:26 -0700329 error_msg = "rec_len is too small for name_len";
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800330 else if (unlikely(
331 ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
Mark Fasheh316f4b92007-09-07 18:21:26 -0700332 error_msg = "directory entry across blocks";
333
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800334 if (unlikely(error_msg != NULL))
Mark Fasheh316f4b92007-09-07 18:21:26 -0700335 mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
336 "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
337 (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
338 offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
339 de->name_len);
Tao Ma1dd9ffc2011-01-24 23:23:30 +0800340
Mark Fasheh316f4b92007-09-07 18:21:26 -0700341 return error_msg == NULL ? 1 : 0;
342}
343
344static inline int ocfs2_match(int len,
345 const char * const name,
346 struct ocfs2_dir_entry *de)
347{
348 if (len != de->name_len)
349 return 0;
350 if (!de->inode)
351 return 0;
352 return !memcmp(name, de->name, len);
353}
354
355/*
356 * Returns 0 if not found, -1 on failure, and 1 on success
357 */
Jesper Juhl42b16b32011-01-17 00:09:38 +0100358static inline int ocfs2_search_dirblock(struct buffer_head *bh,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700359 struct inode *dir,
360 const char *name, int namelen,
361 unsigned long offset,
Mark Fasheh23193e52007-09-12 13:01:18 -0700362 char *first_de,
363 unsigned int bytes,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700364 struct ocfs2_dir_entry **res_dir)
365{
366 struct ocfs2_dir_entry *de;
367 char *dlimit, *de_buf;
368 int de_len;
369 int ret = 0;
370
Mark Fasheh23193e52007-09-12 13:01:18 -0700371 de_buf = first_de;
372 dlimit = de_buf + bytes;
Mark Fasheh316f4b92007-09-07 18:21:26 -0700373
374 while (de_buf < dlimit) {
375 /* this code is executed quadratically often */
376 /* do minimal checking `by hand' */
377
378 de = (struct ocfs2_dir_entry *) de_buf;
379
380 if (de_buf + namelen <= dlimit &&
381 ocfs2_match(namelen, name, de)) {
382 /* found a match - just to be sure, do a full check */
383 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
384 ret = -1;
385 goto bail;
386 }
387 *res_dir = de;
388 ret = 1;
389 goto bail;
390 }
391
392 /* prevent looping on a bad block */
393 de_len = le16_to_cpu(de->rec_len);
394 if (de_len <= 0) {
395 ret = -1;
396 goto bail;
397 }
398
399 de_buf += de_len;
400 offset += de_len;
401 }
402
403bail:
Tao Maf1088d472011-02-23 22:30:23 +0800404 trace_ocfs2_search_dirblock(ret);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700405 return ret;
406}
407
Mark Fasheh23193e52007-09-12 13:01:18 -0700408static struct buffer_head *ocfs2_find_entry_id(const char *name,
409 int namelen,
410 struct inode *dir,
411 struct ocfs2_dir_entry **res_dir)
412{
413 int ret, found;
414 struct buffer_head *di_bh = NULL;
415 struct ocfs2_dinode *di;
416 struct ocfs2_inline_data *data;
417
Joel Beckerb657c952008-11-13 14:49:11 -0800418 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -0700419 if (ret) {
420 mlog_errno(ret);
421 goto out;
422 }
423
424 di = (struct ocfs2_dinode *)di_bh->b_data;
425 data = &di->id2.i_data;
426
427 found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
428 data->id_data, i_size_read(dir), res_dir);
429 if (found == 1)
430 return di_bh;
431
432 brelse(di_bh);
433out:
434 return NULL;
435}
436
Joel Beckera22305c2008-11-13 14:49:17 -0800437static int ocfs2_validate_dir_block(struct super_block *sb,
438 struct buffer_head *bh)
439{
Joel Beckerc175a512008-12-10 17:58:22 -0800440 int rc;
441 struct ocfs2_dir_block_trailer *trailer =
442 ocfs2_trailer_from_bh(bh, sb);
443
444
Joel Beckera22305c2008-11-13 14:49:17 -0800445 /*
Joel Beckerc175a512008-12-10 17:58:22 -0800446 * We don't validate dirents here, that's handled
Joel Beckera22305c2008-11-13 14:49:17 -0800447 * in-place when the code walks them.
448 */
Tao Maf1088d472011-02-23 22:30:23 +0800449 trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
Joel Beckera22305c2008-11-13 14:49:17 -0800450
Joel Beckerc175a512008-12-10 17:58:22 -0800451 BUG_ON(!buffer_uptodate(bh));
452
453 /*
454 * If the ecc fails, we return the error but otherwise
455 * leave the filesystem running. We know any error is
456 * local to this block.
457 *
458 * Note that we are safe to call this even if the directory
459 * doesn't have a trailer. Filesystems without metaecc will do
460 * nothing, and filesystems with it will have one.
461 */
462 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
463 if (rc)
464 mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
465 (unsigned long long)bh->b_blocknr);
466
467 return rc;
Joel Beckera22305c2008-11-13 14:49:17 -0800468}
469
470/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800471 * Validate a directory trailer.
472 *
473 * We check the trailer here rather than in ocfs2_validate_dir_block()
474 * because that function doesn't have the inode to test.
475 */
476static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
477{
478 int rc = 0;
479 struct ocfs2_dir_block_trailer *trailer;
480
481 trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
482 if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700483 rc = ocfs2_error(dir->i_sb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800484 "Invalid dirblock #%llu: "
485 "signature = %.*s\n",
486 (unsigned long long)bh->b_blocknr, 7,
487 trailer->db_signature);
488 goto out;
489 }
490 if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700491 rc = ocfs2_error(dir->i_sb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800492 "Directory block #%llu has an invalid "
493 "db_blkno of %llu",
494 (unsigned long long)bh->b_blocknr,
495 (unsigned long long)le64_to_cpu(trailer->db_blkno));
496 goto out;
497 }
498 if (le64_to_cpu(trailer->db_parent_dinode) !=
499 OCFS2_I(dir)->ip_blkno) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700500 rc = ocfs2_error(dir->i_sb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800501 "Directory block #%llu on dinode "
502 "#%llu has an invalid parent_dinode "
503 "of %llu",
504 (unsigned long long)bh->b_blocknr,
505 (unsigned long long)OCFS2_I(dir)->ip_blkno,
506 (unsigned long long)le64_to_cpu(trailer->db_blkno));
507 goto out;
508 }
509out:
510 return rc;
511}
512
513/*
Joel Beckera22305c2008-11-13 14:49:17 -0800514 * This function forces all errors to -EIO for consistency with its
515 * predecessor, ocfs2_bread(). We haven't audited what returning the
516 * real error codes would do to callers. We log the real codes with
517 * mlog_errno() before we squash them.
518 */
519static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
520 struct buffer_head **bh, int flags)
521{
522 int rc = 0;
523 struct buffer_head *tmp = *bh;
Joel Beckera22305c2008-11-13 14:49:17 -0800524
Joel Becker511308d2008-11-13 14:49:21 -0800525 rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
526 ocfs2_validate_dir_block);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800527 if (rc) {
Joel Beckera22305c2008-11-13 14:49:17 -0800528 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800529 goto out;
530 }
531
Mark Fasheh87d35a72008-12-10 17:36:25 -0800532 if (!(flags & OCFS2_BH_READAHEAD) &&
Mark Fashehe7c17e42009-01-29 18:17:46 -0800533 ocfs2_supports_dir_trailer(inode)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800534 rc = ocfs2_check_dir_trailer(inode, tmp);
535 if (rc) {
536 if (!*bh)
537 brelse(tmp);
538 mlog_errno(rc);
Mark Fasheh87d35a72008-12-10 17:36:25 -0800539 goto out;
540 }
541 }
Joel Beckera22305c2008-11-13 14:49:17 -0800542
Joel Becker511308d2008-11-13 14:49:21 -0800543 /* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
Mark Fasheh87d35a72008-12-10 17:36:25 -0800544 if (!*bh)
Joel Beckera22305c2008-11-13 14:49:17 -0800545 *bh = tmp;
546
Mark Fasheh87d35a72008-12-10 17:36:25 -0800547out:
Joel Beckera22305c2008-11-13 14:49:17 -0800548 return rc ? -EIO : 0;
549}
550
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800551/*
552 * Read the block at 'phys' which belongs to this directory
553 * inode. This function does no virtual->physical block translation -
554 * what's passed in is assumed to be a valid directory block.
555 */
556static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
557 struct buffer_head **bh)
558{
559 int ret;
560 struct buffer_head *tmp = *bh;
561
Joel Becker8cb471e2009-02-10 20:00:41 -0800562 ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
563 ocfs2_validate_dir_block);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800564 if (ret) {
565 mlog_errno(ret);
566 goto out;
567 }
568
569 if (ocfs2_supports_dir_trailer(dir)) {
570 ret = ocfs2_check_dir_trailer(dir, tmp);
571 if (ret) {
572 if (!*bh)
573 brelse(tmp);
574 mlog_errno(ret);
575 goto out;
576 }
577 }
578
579 if (!ret && !*bh)
580 *bh = tmp;
581out:
582 return ret;
583}
584
585static int ocfs2_validate_dx_root(struct super_block *sb,
586 struct buffer_head *bh)
587{
588 int ret;
589 struct ocfs2_dx_root_block *dx_root;
590
591 BUG_ON(!buffer_uptodate(bh));
592
593 dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
594
595 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
596 if (ret) {
597 mlog(ML_ERROR,
598 "Checksum failed for dir index root block %llu\n",
599 (unsigned long long)bh->b_blocknr);
600 return ret;
601 }
602
603 if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700604 ret = ocfs2_error(sb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800605 "Dir Index Root # %llu has bad signature %.*s",
606 (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
607 7, dx_root->dr_signature);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800608 }
609
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700610 return ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800611}
612
613static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
614 struct buffer_head **dx_root_bh)
615{
616 int ret;
617 u64 blkno = le64_to_cpu(di->i_dx_root);
618 struct buffer_head *tmp = *dx_root_bh;
619
Joel Becker8cb471e2009-02-10 20:00:41 -0800620 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
621 ocfs2_validate_dx_root);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800622
623 /* If ocfs2_read_block() got us a new bh, pass it up. */
624 if (!ret && !*dx_root_bh)
625 *dx_root_bh = tmp;
626
627 return ret;
628}
629
630static int ocfs2_validate_dx_leaf(struct super_block *sb,
631 struct buffer_head *bh)
632{
633 int ret;
634 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
635
636 BUG_ON(!buffer_uptodate(bh));
637
638 ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
639 if (ret) {
640 mlog(ML_ERROR,
641 "Checksum failed for dir index leaf block %llu\n",
642 (unsigned long long)bh->b_blocknr);
643 return ret;
644 }
645
646 if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700647 ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s",
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800648 7, dx_leaf->dl_signature);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800649 }
650
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700651 return ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800652}
653
654static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
655 struct buffer_head **dx_leaf_bh)
656{
657 int ret;
658 struct buffer_head *tmp = *dx_leaf_bh;
659
Joel Becker8cb471e2009-02-10 20:00:41 -0800660 ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
661 ocfs2_validate_dx_leaf);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800662
663 /* If ocfs2_read_block() got us a new bh, pass it up. */
664 if (!ret && !*dx_leaf_bh)
665 *dx_leaf_bh = tmp;
666
667 return ret;
668}
669
670/*
671 * Read a series of dx_leaf blocks. This expects all buffer_head
672 * pointers to be NULL on function entry.
673 */
674static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
675 struct buffer_head **dx_leaf_bhs)
676{
677 int ret;
678
Joel Becker8cb471e2009-02-10 20:00:41 -0800679 ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800680 ocfs2_validate_dx_leaf);
681 if (ret)
682 mlog_errno(ret);
683
684 return ret;
685}
686
Adrian Bunk0af4bd32007-10-24 18:23:27 +0200687static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
688 struct inode *dir,
689 struct ocfs2_dir_entry **res_dir)
Mark Fasheh316f4b92007-09-07 18:21:26 -0700690{
691 struct super_block *sb;
692 struct buffer_head *bh_use[NAMEI_RA_SIZE];
693 struct buffer_head *bh, *ret = NULL;
694 unsigned long start, block, b;
695 int ra_max = 0; /* Number of bh's in the readahead
696 buffer, bh_use[] */
697 int ra_ptr = 0; /* Current index into readahead
698 buffer */
699 int num = 0;
700 int nblocks, i, err;
701
Mark Fasheh316f4b92007-09-07 18:21:26 -0700702 sb = dir->i_sb;
703
704 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
705 start = OCFS2_I(dir)->ip_dir_start_lookup;
706 if (start >= nblocks)
707 start = 0;
708 block = start;
709
710restart:
711 do {
712 /*
713 * We deal with the read-ahead logic here.
714 */
715 if (ra_ptr >= ra_max) {
716 /* Refill the readahead buffer */
717 ra_ptr = 0;
718 b = block;
719 for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
720 /*
721 * Terminate if we reach the end of the
722 * directory and must wrap, or if our
723 * search has finished at this block.
724 */
725 if (b >= nblocks || (num && block == start)) {
726 bh_use[ra_max] = NULL;
727 break;
728 }
729 num++;
730
Joel Beckera22305c2008-11-13 14:49:17 -0800731 bh = NULL;
732 err = ocfs2_read_dir_block(dir, b++, &bh,
733 OCFS2_BH_READAHEAD);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700734 bh_use[ra_max] = bh;
735 }
736 }
737 if ((bh = bh_use[ra_ptr++]) == NULL)
738 goto next;
Joel Beckera22305c2008-11-13 14:49:17 -0800739 if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
Joel Becker5e0b3de2008-10-09 17:20:33 -0700740 /* read error, skip block & hope for the best.
Joel Beckera22305c2008-11-13 14:49:17 -0800741 * ocfs2_read_dir_block() has released the bh. */
jiangyiwen61fb9ea2014-12-10 15:42:02 -0800742 mlog(ML_ERROR, "reading directory %llu, "
Mark Fasheh316f4b92007-09-07 18:21:26 -0700743 "offset %lu\n",
744 (unsigned long long)OCFS2_I(dir)->ip_blkno,
745 block);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700746 goto next;
747 }
748 i = ocfs2_search_dirblock(bh, dir, name, namelen,
749 block << sb->s_blocksize_bits,
Mark Fasheh23193e52007-09-12 13:01:18 -0700750 bh->b_data, sb->s_blocksize,
Mark Fasheh316f4b92007-09-07 18:21:26 -0700751 res_dir);
752 if (i == 1) {
753 OCFS2_I(dir)->ip_dir_start_lookup = block;
754 ret = bh;
755 goto cleanup_and_exit;
756 } else {
757 brelse(bh);
758 if (i < 0)
759 goto cleanup_and_exit;
760 }
761 next:
762 if (++block >= nblocks)
763 block = 0;
764 } while (block != start);
765
766 /*
767 * If the directory has grown while we were searching, then
768 * search the last part of the directory before giving up.
769 */
770 block = nblocks;
771 nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
772 if (block < nblocks) {
773 start = 0;
774 goto restart;
775 }
776
777cleanup_and_exit:
778 /* Clean up the read-ahead blocks */
779 for (; ra_ptr < ra_max; ra_ptr++)
780 brelse(bh_use[ra_ptr]);
781
Tao Maf1088d472011-02-23 22:30:23 +0800782 trace_ocfs2_find_entry_el(ret);
Mark Fasheh316f4b92007-09-07 18:21:26 -0700783 return ret;
784}
785
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800786static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
787 struct ocfs2_extent_list *el,
788 u32 major_hash,
789 u32 *ret_cpos,
790 u64 *ret_phys_blkno,
791 unsigned int *ret_clen)
792{
793 int ret = 0, i, found;
794 struct buffer_head *eb_bh = NULL;
795 struct ocfs2_extent_block *eb;
796 struct ocfs2_extent_rec *rec = NULL;
797
798 if (el->l_tree_depth) {
Joel Beckerfacdb772009-02-12 18:08:48 -0800799 ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
800 &eb_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800801 if (ret) {
802 mlog_errno(ret);
803 goto out;
804 }
805
806 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
807 el = &eb->h_list;
808
809 if (el->l_tree_depth) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700810 ret = ocfs2_error(inode->i_sb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800811 "Inode %lu has non zero tree depth in "
812 "btree tree block %llu\n", inode->i_ino,
813 (unsigned long long)eb_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800814 goto out;
815 }
816 }
817
818 found = 0;
819 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
820 rec = &el->l_recs[i];
821
822 if (le32_to_cpu(rec->e_cpos) <= major_hash) {
823 found = 1;
824 break;
825 }
826 }
827
828 if (!found) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700829 ret = ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800830 "record (%u, %u, 0) in btree", inode->i_ino,
831 le32_to_cpu(rec->e_cpos),
832 ocfs2_rec_clusters(el, rec));
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800833 goto out;
834 }
835
836 if (ret_phys_blkno)
837 *ret_phys_blkno = le64_to_cpu(rec->e_blkno);
838 if (ret_cpos)
839 *ret_cpos = le32_to_cpu(rec->e_cpos);
840 if (ret_clen)
841 *ret_clen = le16_to_cpu(rec->e_leaf_clusters);
842
843out:
844 brelse(eb_bh);
845 return ret;
846}
847
848/*
849 * Returns the block index, from the start of the cluster which this
850 * hash belongs too.
851 */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800852static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
853 u32 minor_hash)
854{
855 return minor_hash & osb->osb_dx_mask;
856}
857
858static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800859 struct ocfs2_dx_hinfo *hinfo)
860{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800861 return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800862}
863
864static int ocfs2_dx_dir_lookup(struct inode *inode,
865 struct ocfs2_extent_list *el,
866 struct ocfs2_dx_hinfo *hinfo,
867 u32 *ret_cpos,
868 u64 *ret_phys_blkno)
869{
870 int ret = 0;
871 unsigned int cend, uninitialized_var(clen);
872 u32 uninitialized_var(cpos);
873 u64 uninitialized_var(blkno);
874 u32 name_hash = hinfo->major_hash;
875
876 ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
877 &clen);
878 if (ret) {
879 mlog_errno(ret);
880 goto out;
881 }
882
883 cend = cpos + clen;
884 if (name_hash >= cend) {
885 /* We want the last cluster */
886 blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
887 cpos += clen - 1;
888 } else {
889 blkno += ocfs2_clusters_to_blocks(inode->i_sb,
890 name_hash - cpos);
891 cpos = name_hash;
892 }
893
894 /*
895 * We now have the cluster which should hold our entry. To
896 * find the exact block from the start of the cluster to
897 * search, we take the lower bits of the hash.
898 */
899 blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
900
901 if (ret_phys_blkno)
902 *ret_phys_blkno = blkno;
903 if (ret_cpos)
904 *ret_cpos = cpos;
905
906out:
907
908 return ret;
909}
910
911static int ocfs2_dx_dir_search(const char *name, int namelen,
912 struct inode *dir,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800913 struct ocfs2_dx_root_block *dx_root,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800914 struct ocfs2_dir_lookup_result *res)
915{
916 int ret, i, found;
917 u64 uninitialized_var(phys);
918 struct buffer_head *dx_leaf_bh = NULL;
919 struct ocfs2_dx_leaf *dx_leaf;
920 struct ocfs2_dx_entry *dx_entry = NULL;
921 struct buffer_head *dir_ent_bh = NULL;
922 struct ocfs2_dir_entry *dir_ent = NULL;
923 struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800924 struct ocfs2_extent_list *dr_el;
925 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800926
927 ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
928
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800929 if (ocfs2_dx_root_inline(dx_root)) {
930 entry_list = &dx_root->dr_entries;
931 goto search;
932 }
933
934 dr_el = &dx_root->dr_list;
935
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800936 ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
937 if (ret) {
938 mlog_errno(ret);
939 goto out;
940 }
941
Tao Maf1088d472011-02-23 22:30:23 +0800942 trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
943 namelen, name, hinfo->major_hash,
944 hinfo->minor_hash, (unsigned long long)phys);
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800945
946 ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
947 if (ret) {
948 mlog_errno(ret);
949 goto out;
950 }
951
952 dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
953
Tao Maf1088d472011-02-23 22:30:23 +0800954 trace_ocfs2_dx_dir_search_leaf_info(
955 le16_to_cpu(dx_leaf->dl_list.de_num_used),
956 le16_to_cpu(dx_leaf->dl_list.de_count));
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800957
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800958 entry_list = &dx_leaf->dl_list;
959
960search:
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800961 /*
962 * Empty leaf is legal, so no need to check for that.
963 */
964 found = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -0800965 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
966 dx_entry = &entry_list->de_entries[i];
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800967
968 if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
969 || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
970 continue;
971
972 /*
973 * Search unindexed leaf block now. We're not
974 * guaranteed to find anything.
975 */
976 ret = ocfs2_read_dir_block_direct(dir,
977 le64_to_cpu(dx_entry->dx_dirent_blk),
978 &dir_ent_bh);
979 if (ret) {
980 mlog_errno(ret);
981 goto out;
982 }
983
984 /*
985 * XXX: We should check the unindexed block here,
986 * before using it.
987 */
988
989 found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
990 0, dir_ent_bh->b_data,
991 dir->i_sb->s_blocksize, &dir_ent);
992 if (found == 1)
993 break;
994
995 if (found == -1) {
996 /* This means we found a bad directory entry. */
997 ret = -EIO;
998 mlog_errno(ret);
999 goto out;
1000 }
1001
1002 brelse(dir_ent_bh);
1003 dir_ent_bh = NULL;
1004 }
1005
1006 if (found <= 0) {
1007 ret = -ENOENT;
1008 goto out;
1009 }
1010
1011 res->dl_leaf_bh = dir_ent_bh;
1012 res->dl_entry = dir_ent;
1013 res->dl_dx_leaf_bh = dx_leaf_bh;
1014 res->dl_dx_entry = dx_entry;
1015
1016 ret = 0;
1017out:
1018 if (ret) {
1019 brelse(dx_leaf_bh);
1020 brelse(dir_ent_bh);
1021 }
1022 return ret;
1023}
1024
1025static int ocfs2_find_entry_dx(const char *name, int namelen,
1026 struct inode *dir,
1027 struct ocfs2_dir_lookup_result *lookup)
1028{
1029 int ret;
1030 struct buffer_head *di_bh = NULL;
1031 struct ocfs2_dinode *di;
1032 struct buffer_head *dx_root_bh = NULL;
1033 struct ocfs2_dx_root_block *dx_root;
1034
1035 ret = ocfs2_read_inode_block(dir, &di_bh);
1036 if (ret) {
1037 mlog_errno(ret);
1038 goto out;
1039 }
1040
1041 di = (struct ocfs2_dinode *)di_bh->b_data;
1042
1043 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1044 if (ret) {
1045 mlog_errno(ret);
1046 goto out;
1047 }
1048 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1049
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001050 ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001051 if (ret) {
1052 if (ret != -ENOENT)
1053 mlog_errno(ret);
1054 goto out;
1055 }
1056
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001057 lookup->dl_dx_root_bh = dx_root_bh;
1058 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001059out:
1060 brelse(di_bh);
1061 brelse(dx_root_bh);
1062 return ret;
1063}
1064
Mark Fasheh23193e52007-09-12 13:01:18 -07001065/*
1066 * Try to find an entry of the provided name within 'dir'.
1067 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001068 * If nothing was found, -ENOENT is returned. Otherwise, zero is
1069 * returned and the struct 'res' will contain information useful to
1070 * other directory manipulation functions.
Mark Fasheh23193e52007-09-12 13:01:18 -07001071 *
1072 * Caller can NOT assume anything about the contents of the
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001073 * buffer_heads - they are passed back only so that it can be passed
1074 * into any one of the manipulation functions (add entry, delete
1075 * entry, etc). As an example, bh in the extent directory case is a
1076 * data block, in the inline-data case it actually points to an inode,
1077 * in the indexed directory case, multiple buffers are involved.
Mark Fasheh23193e52007-09-12 13:01:18 -07001078 */
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001079int ocfs2_find_entry(const char *name, int namelen,
1080 struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh23193e52007-09-12 13:01:18 -07001081{
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001082 struct buffer_head *bh;
1083 struct ocfs2_dir_entry *res_dir = NULL;
Mark Fasheh23193e52007-09-12 13:01:18 -07001084
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001085 if (ocfs2_dir_indexed(dir))
1086 return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1087
1088 /*
1089 * The unindexed dir code only uses part of the lookup
1090 * structure, so there's no reason to push it down further
1091 * than this.
1092 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001093 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001094 bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1095 else
1096 bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
Mark Fasheh23193e52007-09-12 13:01:18 -07001097
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001098 if (bh == NULL)
1099 return -ENOENT;
1100
1101 lookup->dl_leaf_bh = bh;
1102 lookup->dl_entry = res_dir;
1103 return 0;
Mark Fasheh23193e52007-09-12 13:01:18 -07001104}
1105
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001106/*
1107 * Update inode number and type of a previously found directory entry.
1108 */
Mark Fasheh38760e22007-09-11 17:21:56 -07001109int ocfs2_update_entry(struct inode *dir, handle_t *handle,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001110 struct ocfs2_dir_lookup_result *res,
Mark Fasheh38760e22007-09-11 17:21:56 -07001111 struct inode *new_entry_inode)
1112{
1113 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07001114 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001115 struct ocfs2_dir_entry *de = res->dl_entry;
1116 struct buffer_head *de_bh = res->dl_leaf_bh;
Mark Fasheh38760e22007-09-11 17:21:56 -07001117
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001118 /*
1119 * The same code works fine for both inline-data and extent
Joel Becker13723d02008-10-17 19:25:01 -07001120 * based directories, so no need to split this up. The only
1121 * difference is the journal_access function.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001122 */
1123
Joel Becker13723d02008-10-17 19:25:01 -07001124 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1125 access = ocfs2_journal_access_di;
1126
Joel Becker0cf2f762009-02-12 16:41:25 -08001127 ret = access(handle, INODE_CACHE(dir), de_bh,
1128 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh38760e22007-09-11 17:21:56 -07001129 if (ret) {
1130 mlog_errno(ret);
1131 goto out;
1132 }
1133
1134 de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1135 ocfs2_set_de_type(de, new_entry_inode->i_mode);
1136
1137 ocfs2_journal_dirty(handle, de_bh);
1138
1139out:
1140 return ret;
1141}
1142
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001143/*
1144 * __ocfs2_delete_entry deletes a directory entry by merging it with the
1145 * previous entry
1146 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001147static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1148 struct ocfs2_dir_entry *de_del,
1149 struct buffer_head *bh, char *first_de,
1150 unsigned int bytes)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001151{
1152 struct ocfs2_dir_entry *de, *pde;
1153 int i, status = -ENOENT;
Joel Becker13723d02008-10-17 19:25:01 -07001154 ocfs2_journal_access_func access = ocfs2_journal_access_db;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001155
Joel Becker13723d02008-10-17 19:25:01 -07001156 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1157 access = ocfs2_journal_access_di;
1158
Mark Fasheh316f4b92007-09-07 18:21:26 -07001159 i = 0;
1160 pde = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001161 de = (struct ocfs2_dir_entry *) first_de;
1162 while (i < bytes) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001163 if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
1164 status = -EIO;
1165 mlog_errno(status);
1166 goto bail;
1167 }
1168 if (de == de_del) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001169 status = access(handle, INODE_CACHE(dir), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001170 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001171 if (status < 0) {
1172 status = -EIO;
1173 mlog_errno(status);
1174 goto bail;
1175 }
1176 if (pde)
Marcin Slusarz0dd32562008-02-13 00:06:18 +01001177 le16_add_cpu(&pde->rec_len,
1178 le16_to_cpu(de->rec_len));
Wengang Wang82985242011-07-12 16:43:14 +08001179 de->inode = 0;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001180 dir->i_version++;
Joel Beckerec20cec2010-03-19 14:13:52 -07001181 ocfs2_journal_dirty(handle, bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001182 goto bail;
1183 }
1184 i += le16_to_cpu(de->rec_len);
1185 pde = de;
1186 de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1187 }
1188bail:
Mark Fasheh316f4b92007-09-07 18:21:26 -07001189 return status;
1190}
1191
Mark Fashehe7c17e42009-01-29 18:17:46 -08001192static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1193{
1194 unsigned int hole;
1195
1196 if (le64_to_cpu(de->inode) == 0)
1197 hole = le16_to_cpu(de->rec_len);
1198 else
1199 hole = le16_to_cpu(de->rec_len) -
1200 OCFS2_DIR_REC_LEN(de->name_len);
1201
1202 return hole;
1203}
1204
1205static int ocfs2_find_max_rec_len(struct super_block *sb,
1206 struct buffer_head *dirblock_bh)
1207{
1208 int size, this_hole, largest_hole = 0;
1209 char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1210 struct ocfs2_dir_entry *de;
1211
1212 trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1213 size = ocfs2_dir_trailer_blk_off(sb);
1214 limit = start + size;
1215 de_buf = start;
1216 de = (struct ocfs2_dir_entry *)de_buf;
1217 do {
1218 if (de_buf != trailer) {
1219 this_hole = ocfs2_figure_dirent_hole(de);
1220 if (this_hole > largest_hole)
1221 largest_hole = this_hole;
1222 }
1223
1224 de_buf += le16_to_cpu(de->rec_len);
1225 de = (struct ocfs2_dir_entry *)de_buf;
1226 } while (de_buf < limit);
1227
1228 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1229 return largest_hole;
1230 return 0;
1231}
1232
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001233static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1234 int index)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001235{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001236 int num_used = le16_to_cpu(entry_list->de_num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001237
1238 if (num_used == 1 || index == (num_used - 1))
1239 goto clear;
1240
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001241 memmove(&entry_list->de_entries[index],
1242 &entry_list->de_entries[index + 1],
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001243 (num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1244clear:
1245 num_used--;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001246 memset(&entry_list->de_entries[num_used], 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001247 sizeof(struct ocfs2_dx_entry));
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001248 entry_list->de_num_used = cpu_to_le16(num_used);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001249}
1250
1251static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1252 struct ocfs2_dir_lookup_result *lookup)
1253{
Mark Fashehe7c17e42009-01-29 18:17:46 -08001254 int ret, index, max_rec_len, add_to_free_list = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001255 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001256 struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1257 struct ocfs2_dx_leaf *dx_leaf;
1258 struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
Mark Fashehe7c17e42009-01-29 18:17:46 -08001259 struct ocfs2_dir_block_trailer *trailer;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001260 struct ocfs2_dx_root_block *dx_root;
1261 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001262
Mark Fashehe7c17e42009-01-29 18:17:46 -08001263 /*
1264 * This function gets a bit messy because we might have to
1265 * modify the root block, regardless of whether the indexed
1266 * entries are stored inline.
1267 */
1268
1269 /*
1270 * *Only* set 'entry_list' here, based on where we're looking
1271 * for the indexed entries. Later, we might still want to
1272 * journal both blocks, based on free list state.
1273 */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001274 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1275 if (ocfs2_dx_root_inline(dx_root)) {
1276 entry_list = &dx_root->dr_entries;
1277 } else {
1278 dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1279 entry_list = &dx_leaf->dl_list;
1280 }
1281
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001282 /* Neither of these are a disk corruption - that should have
1283 * been caught by lookup, before we got here. */
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001284 BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1285 BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001286
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001287 index = (char *)dx_entry - (char *)entry_list->de_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001288 index /= sizeof(*dx_entry);
1289
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001290 if (index >= le16_to_cpu(entry_list->de_num_used)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001291 mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001292 (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1293 entry_list, dx_entry);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001294 return -EIO;
1295 }
1296
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001297 /*
Mark Fashehe7c17e42009-01-29 18:17:46 -08001298 * We know that removal of this dirent will leave enough room
1299 * for a new one, so add this block to the free list if it
1300 * isn't already there.
1301 */
1302 trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1303 if (trailer->db_free_rec_len == 0)
1304 add_to_free_list = 1;
1305
1306 /*
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001307 * Add the block holding our index into the journal before
1308 * removing the unindexed entry. If we get an error return
1309 * from __ocfs2_delete_entry(), then it hasn't removed the
1310 * entry yet. Likewise, successful return means we *must*
1311 * remove the indexed entry.
1312 *
Mark Fashehe3a93c22009-02-17 15:29:35 -08001313 * We're also careful to journal the root tree block here as
1314 * the entry count needs to be updated. Also, we might be
1315 * adding to the start of the free list.
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001316 */
Joel Becker0cf2f762009-02-12 16:41:25 -08001317 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
Mark Fashehe3a93c22009-02-17 15:29:35 -08001318 OCFS2_JOURNAL_ACCESS_WRITE);
1319 if (ret) {
1320 mlog_errno(ret);
1321 goto out;
Mark Fashehe7c17e42009-01-29 18:17:46 -08001322 }
1323
1324 if (!ocfs2_dx_root_inline(dx_root)) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001325 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001326 lookup->dl_dx_leaf_bh,
1327 OCFS2_JOURNAL_ACCESS_WRITE);
1328 if (ret) {
1329 mlog_errno(ret);
1330 goto out;
1331 }
1332 }
1333
Tao Maf1088d472011-02-23 22:30:23 +08001334 trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
1335 index);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001336
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001337 ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1338 leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1339 if (ret) {
1340 mlog_errno(ret);
1341 goto out;
1342 }
1343
Mark Fashehe7c17e42009-01-29 18:17:46 -08001344 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1345 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1346 if (add_to_free_list) {
1347 trailer->db_free_next = dx_root->dr_free_blk;
1348 dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1349 ocfs2_journal_dirty(handle, dx_root_bh);
1350 }
1351
1352 /* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1353 ocfs2_journal_dirty(handle, leaf_bh);
1354
Mark Fashehe3a93c22009-02-17 15:29:35 -08001355 le32_add_cpu(&dx_root->dr_num_entries, -1);
1356 ocfs2_journal_dirty(handle, dx_root_bh);
1357
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001358 ocfs2_dx_list_remove_entry(entry_list, index);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001359
Mark Fashehe3a93c22009-02-17 15:29:35 -08001360 if (!ocfs2_dx_root_inline(dx_root))
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001361 ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001362
1363out:
1364 return ret;
1365}
1366
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001367static inline int ocfs2_delete_entry_id(handle_t *handle,
1368 struct inode *dir,
1369 struct ocfs2_dir_entry *de_del,
1370 struct buffer_head *bh)
1371{
1372 int ret;
1373 struct buffer_head *di_bh = NULL;
1374 struct ocfs2_dinode *di;
1375 struct ocfs2_inline_data *data;
1376
Joel Beckerb657c952008-11-13 14:49:11 -08001377 ret = ocfs2_read_inode_block(dir, &di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001378 if (ret) {
1379 mlog_errno(ret);
1380 goto out;
1381 }
1382
1383 di = (struct ocfs2_dinode *)di_bh->b_data;
1384 data = &di->id2.i_data;
1385
1386 ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1387 i_size_read(dir));
1388
1389 brelse(di_bh);
1390out:
1391 return ret;
1392}
1393
1394static inline int ocfs2_delete_entry_el(handle_t *handle,
1395 struct inode *dir,
1396 struct ocfs2_dir_entry *de_del,
1397 struct buffer_head *bh)
1398{
1399 return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1400 bh->b_size);
1401}
1402
1403/*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001404 * Delete a directory entry. Hide the details of directory
1405 * implementation from the caller.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001406 */
1407int ocfs2_delete_entry(handle_t *handle,
1408 struct inode *dir,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001409 struct ocfs2_dir_lookup_result *res)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001410{
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001411 if (ocfs2_dir_indexed(dir))
1412 return ocfs2_delete_entry_dx(handle, dir, res);
1413
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001414 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001415 return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1416 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001417
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001418 return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1419 res->dl_leaf_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001420}
1421
Mark Fasheh8553cf42007-09-13 16:29:01 -07001422/*
1423 * Check whether 'de' has enough room to hold an entry of
1424 * 'new_rec_len' bytes.
1425 */
1426static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1427 unsigned int new_rec_len)
1428{
1429 unsigned int de_really_used;
1430
1431 /* Check whether this is an empty record with enough space */
1432 if (le64_to_cpu(de->inode) == 0 &&
1433 le16_to_cpu(de->rec_len) >= new_rec_len)
1434 return 1;
1435
1436 /*
1437 * Record might have free space at the end which we can
1438 * use.
1439 */
1440 de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1441 if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1442 return 1;
1443
1444 return 0;
1445}
1446
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001447static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1448 struct ocfs2_dx_entry *dx_new_entry)
1449{
1450 int i;
1451
1452 i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1453 dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1454
1455 le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1456}
1457
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001458static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1459 struct ocfs2_dx_hinfo *hinfo,
1460 u64 dirent_blk)
1461{
1462 int i;
1463 struct ocfs2_dx_entry *dx_entry;
1464
1465 i = le16_to_cpu(entry_list->de_num_used);
1466 dx_entry = &entry_list->de_entries[i];
1467
1468 memset(dx_entry, 0, sizeof(*dx_entry));
1469 dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1470 dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1471 dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1472
1473 le16_add_cpu(&entry_list->de_num_used, 1);
1474}
1475
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001476static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1477 struct ocfs2_dx_hinfo *hinfo,
1478 u64 dirent_blk,
1479 struct buffer_head *dx_leaf_bh)
1480{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001481 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001482 struct ocfs2_dx_leaf *dx_leaf;
1483
Joel Becker0cf2f762009-02-12 16:41:25 -08001484 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001485 OCFS2_JOURNAL_ACCESS_WRITE);
1486 if (ret) {
1487 mlog_errno(ret);
1488 goto out;
1489 }
1490
1491 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001492 ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001493 ocfs2_journal_dirty(handle, dx_leaf_bh);
1494
1495out:
1496 return ret;
1497}
1498
Mark Fashehe3a93c22009-02-17 15:29:35 -08001499static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1500 struct ocfs2_dx_hinfo *hinfo,
1501 u64 dirent_blk,
1502 struct ocfs2_dx_root_block *dx_root)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001503{
Mark Fashehe3a93c22009-02-17 15:29:35 -08001504 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1505}
1506
1507static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1508 struct ocfs2_dir_lookup_result *lookup)
1509{
1510 int ret = 0;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001511 struct ocfs2_dx_root_block *dx_root;
Mark Fashehe3a93c22009-02-17 15:29:35 -08001512 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001513
Joel Becker0cf2f762009-02-12 16:41:25 -08001514 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001515 OCFS2_JOURNAL_ACCESS_WRITE);
1516 if (ret) {
1517 mlog_errno(ret);
1518 goto out;
1519 }
1520
Mark Fashehe3a93c22009-02-17 15:29:35 -08001521 dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1522 if (ocfs2_dx_root_inline(dx_root)) {
1523 ocfs2_dx_inline_root_insert(dir, handle,
1524 &lookup->dl_hinfo,
1525 lookup->dl_leaf_bh->b_blocknr,
1526 dx_root);
1527 } else {
1528 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1529 lookup->dl_leaf_bh->b_blocknr,
1530 lookup->dl_dx_leaf_bh);
1531 if (ret)
1532 goto out;
1533 }
1534
1535 le32_add_cpu(&dx_root->dr_num_entries, 1);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001536 ocfs2_journal_dirty(handle, dx_root_bh);
1537
1538out:
1539 return ret;
1540}
1541
Mark Fashehe7c17e42009-01-29 18:17:46 -08001542static void ocfs2_remove_block_from_free_list(struct inode *dir,
1543 handle_t *handle,
1544 struct ocfs2_dir_lookup_result *lookup)
1545{
1546 struct ocfs2_dir_block_trailer *trailer, *prev;
1547 struct ocfs2_dx_root_block *dx_root;
1548 struct buffer_head *bh;
1549
1550 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1551
1552 if (ocfs2_free_list_at_root(lookup)) {
1553 bh = lookup->dl_dx_root_bh;
1554 dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1555 dx_root->dr_free_blk = trailer->db_free_next;
1556 } else {
1557 bh = lookup->dl_prev_leaf_bh;
1558 prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1559 prev->db_free_next = trailer->db_free_next;
1560 }
1561
1562 trailer->db_free_rec_len = cpu_to_le16(0);
1563 trailer->db_free_next = cpu_to_le64(0);
1564
1565 ocfs2_journal_dirty(handle, bh);
1566 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1567}
1568
1569/*
1570 * This expects that a journal write has been reserved on
1571 * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1572 */
1573static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1574 struct ocfs2_dir_lookup_result *lookup)
1575{
1576 int max_rec_len;
1577 struct ocfs2_dir_block_trailer *trailer;
1578
1579 /* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1580 max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1581 if (max_rec_len) {
1582 /*
1583 * There's still room in this block, so no need to remove it
1584 * from the free list. In this case, we just want to update
1585 * the rec len accounting.
1586 */
1587 trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1588 trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1589 ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1590 } else {
1591 ocfs2_remove_block_from_free_list(dir, handle, lookup);
1592 }
1593}
1594
Mark Fasheh316f4b92007-09-07 18:21:26 -07001595/* we don't always have a dentry for what we want to add, so people
1596 * like orphan dir can call this instead.
1597 *
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001598 * The lookup context must have been filled from
1599 * ocfs2_prepare_dir_for_insert.
Mark Fasheh316f4b92007-09-07 18:21:26 -07001600 */
1601int __ocfs2_add_entry(handle_t *handle,
1602 struct inode *dir,
1603 const char *name, int namelen,
1604 struct inode *inode, u64 blkno,
1605 struct buffer_head *parent_fe_bh,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001606 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh316f4b92007-09-07 18:21:26 -07001607{
1608 unsigned long offset;
1609 unsigned short rec_len;
1610 struct ocfs2_dir_entry *de, *de1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001611 struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1612 struct super_block *sb = dir->i_sb;
Daeseok Youn2e173152015-06-24 16:55:01 -07001613 int retval;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001614 unsigned int size = sb->s_blocksize;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001615 struct buffer_head *insert_bh = lookup->dl_leaf_bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001616 char *data_start = insert_bh->b_data;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001617
Mark Fasheh316f4b92007-09-07 18:21:26 -07001618 if (!namelen)
1619 return -EINVAL;
1620
Mark Fashehe7c17e42009-01-29 18:17:46 -08001621 if (ocfs2_dir_indexed(dir)) {
1622 struct buffer_head *bh;
1623
1624 /*
1625 * An indexed dir may require that we update the free space
1626 * list. Reserve a write to the previous node in the list so
1627 * that we don't fail later.
1628 *
1629 * XXX: This can be either a dx_root_block, or an unindexed
1630 * directory tree leaf block.
1631 */
1632 if (ocfs2_free_list_at_root(lookup)) {
1633 bh = lookup->dl_dx_root_bh;
Joel Becker0cf2f762009-02-12 16:41:25 -08001634 retval = ocfs2_journal_access_dr(handle,
1635 INODE_CACHE(dir), bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08001636 OCFS2_JOURNAL_ACCESS_WRITE);
1637 } else {
1638 bh = lookup->dl_prev_leaf_bh;
Joel Becker0cf2f762009-02-12 16:41:25 -08001639 retval = ocfs2_journal_access_db(handle,
1640 INODE_CACHE(dir), bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08001641 OCFS2_JOURNAL_ACCESS_WRITE);
1642 }
1643 if (retval) {
1644 mlog_errno(retval);
1645 return retval;
1646 }
1647 } else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001648 data_start = di->id2.i_data.id_data;
1649 size = i_size_read(dir);
1650
1651 BUG_ON(insert_bh != parent_fe_bh);
1652 }
1653
Mark Fasheh316f4b92007-09-07 18:21:26 -07001654 rec_len = OCFS2_DIR_REC_LEN(namelen);
1655 offset = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001656 de = (struct ocfs2_dir_entry *) data_start;
Mark Fasheh316f4b92007-09-07 18:21:26 -07001657 while (1) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07001658 BUG_ON((char *)de >= (size + data_start));
1659
Mark Fasheh316f4b92007-09-07 18:21:26 -07001660 /* These checks should've already been passed by the
1661 * prepare function, but I guess we can leave them
1662 * here anyway. */
1663 if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
1664 retval = -ENOENT;
1665 goto bail;
1666 }
1667 if (ocfs2_match(namelen, name, de)) {
1668 retval = -EEXIST;
1669 goto bail;
1670 }
Mark Fasheh8553cf42007-09-13 16:29:01 -07001671
Mark Fasheh87d35a72008-12-10 17:36:25 -08001672 /* We're guaranteed that we should have space, so we
1673 * can't possibly have hit the trailer...right? */
1674 mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1675 "Hit dir trailer trying to insert %.*s "
1676 "(namelen %d) into directory %llu. "
1677 "offset is %lu, trailer offset is %d\n",
1678 namelen, name, namelen,
1679 (unsigned long long)parent_fe_bh->b_blocknr,
1680 offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1681
Mark Fasheh8553cf42007-09-13 16:29:01 -07001682 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fasheh316f4b92007-09-07 18:21:26 -07001683 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1684 retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1685 if (retval < 0) {
1686 mlog_errno(retval);
1687 goto bail;
1688 }
1689
Joel Becker13723d02008-10-17 19:25:01 -07001690 if (insert_bh == parent_fe_bh)
Daeseok Youn2e173152015-06-24 16:55:01 -07001691 retval = ocfs2_journal_access_di(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001692 INODE_CACHE(dir),
Joel Becker13723d02008-10-17 19:25:01 -07001693 insert_bh,
1694 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001695 else {
Daeseok Youn2e173152015-06-24 16:55:01 -07001696 retval = ocfs2_journal_access_db(handle,
Joel Becker0cf2f762009-02-12 16:41:25 -08001697 INODE_CACHE(dir),
Joel Becker13723d02008-10-17 19:25:01 -07001698 insert_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001699 OCFS2_JOURNAL_ACCESS_WRITE);
1700
Daeseok Youn2e173152015-06-24 16:55:01 -07001701 if (!retval && ocfs2_dir_indexed(dir))
1702 retval = ocfs2_dx_dir_insert(dir,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08001703 handle,
1704 lookup);
Daeseok Youn2e173152015-06-24 16:55:01 -07001705 }
1706
1707 if (retval) {
1708 mlog_errno(retval);
1709 goto bail;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001710 }
1711
Mark Fasheh316f4b92007-09-07 18:21:26 -07001712 /* By now the buffer is marked for journaling */
1713 offset += le16_to_cpu(de->rec_len);
1714 if (le64_to_cpu(de->inode)) {
1715 de1 = (struct ocfs2_dir_entry *)((char *) de +
1716 OCFS2_DIR_REC_LEN(de->name_len));
1717 de1->rec_len =
1718 cpu_to_le16(le16_to_cpu(de->rec_len) -
1719 OCFS2_DIR_REC_LEN(de->name_len));
1720 de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1721 de = de1;
1722 }
1723 de->file_type = OCFS2_FT_UNKNOWN;
1724 if (blkno) {
1725 de->inode = cpu_to_le64(blkno);
1726 ocfs2_set_de_type(de, inode->i_mode);
1727 } else
1728 de->inode = 0;
1729 de->name_len = namelen;
1730 memcpy(de->name, name, namelen);
1731
Mark Fashehe7c17e42009-01-29 18:17:46 -08001732 if (ocfs2_dir_indexed(dir))
1733 ocfs2_recalc_free_list(dir, handle, lookup);
1734
Mark Fasheh316f4b92007-09-07 18:21:26 -07001735 dir->i_version++;
Joel Beckerec20cec2010-03-19 14:13:52 -07001736 ocfs2_journal_dirty(handle, insert_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001737 retval = 0;
1738 goto bail;
1739 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08001740
Mark Fasheh316f4b92007-09-07 18:21:26 -07001741 offset += le16_to_cpu(de->rec_len);
1742 de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1743 }
1744
1745 /* when you think about it, the assert above should prevent us
1746 * from ever getting here. */
1747 retval = -ENOSPC;
1748bail:
Tao Mac1e8d352011-03-07 16:43:21 +08001749 if (retval)
1750 mlog_errno(retval);
Mark Fasheh316f4b92007-09-07 18:21:26 -07001751
Mark Fasheh316f4b92007-09-07 18:21:26 -07001752 return retval;
1753}
1754
Mark Fasheh23193e52007-09-12 13:01:18 -07001755static int ocfs2_dir_foreach_blk_id(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001756 u64 *f_version,
Al Viro3704412b2013-05-22 21:06:00 -04001757 struct dir_context *ctx)
Mark Fasheh23193e52007-09-12 13:01:18 -07001758{
Al Viro3704412b2013-05-22 21:06:00 -04001759 int ret, i;
1760 unsigned long offset = ctx->pos;
Mark Fasheh23193e52007-09-12 13:01:18 -07001761 struct buffer_head *di_bh = NULL;
1762 struct ocfs2_dinode *di;
1763 struct ocfs2_inline_data *data;
1764 struct ocfs2_dir_entry *de;
1765
Joel Beckerb657c952008-11-13 14:49:11 -08001766 ret = ocfs2_read_inode_block(inode, &di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -07001767 if (ret) {
1768 mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1769 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1770 goto out;
1771 }
1772
1773 di = (struct ocfs2_dinode *)di_bh->b_data;
1774 data = &di->id2.i_data;
1775
Al Viro3704412b2013-05-22 21:06:00 -04001776 while (ctx->pos < i_size_read(inode)) {
Mark Fasheh23193e52007-09-12 13:01:18 -07001777 /* If the dir block has changed since the last call to
1778 * readdir(2), then we might be pointing to an invalid
1779 * dirent right now. Scan from the start of the block
1780 * to make sure. */
1781 if (*f_version != inode->i_version) {
1782 for (i = 0; i < i_size_read(inode) && i < offset; ) {
1783 de = (struct ocfs2_dir_entry *)
1784 (data->id_data + i);
1785 /* It's too expensive to do a full
1786 * dirent test each time round this
1787 * loop, but we do have to test at
1788 * least that it is non-zero. A
1789 * failure will be detected in the
1790 * dirent test below. */
1791 if (le16_to_cpu(de->rec_len) <
1792 OCFS2_DIR_REC_LEN(1))
1793 break;
1794 i += le16_to_cpu(de->rec_len);
1795 }
Al Viro3704412b2013-05-22 21:06:00 -04001796 ctx->pos = offset = i;
Mark Fasheh23193e52007-09-12 13:01:18 -07001797 *f_version = inode->i_version;
1798 }
1799
Al Viro3704412b2013-05-22 21:06:00 -04001800 de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
1801 if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
Mark Fasheh23193e52007-09-12 13:01:18 -07001802 /* On error, skip the f_pos to the end. */
Al Viro3704412b2013-05-22 21:06:00 -04001803 ctx->pos = i_size_read(inode);
1804 break;
Mark Fasheh23193e52007-09-12 13:01:18 -07001805 }
1806 offset += le16_to_cpu(de->rec_len);
1807 if (le64_to_cpu(de->inode)) {
Mark Fasheh23193e52007-09-12 13:01:18 -07001808 unsigned char d_type = DT_UNKNOWN;
1809
1810 if (de->file_type < OCFS2_FT_MAX)
1811 d_type = ocfs2_filetype_table[de->file_type];
1812
Al Viro3704412b2013-05-22 21:06:00 -04001813 if (!dir_emit(ctx, de->name, de->name_len,
1814 le64_to_cpu(de->inode), d_type))
1815 goto out;
Mark Fasheh23193e52007-09-12 13:01:18 -07001816 }
Al Viro3704412b2013-05-22 21:06:00 -04001817 ctx->pos += le16_to_cpu(de->rec_len);
Mark Fasheh23193e52007-09-12 13:01:18 -07001818 }
Mark Fasheh23193e52007-09-12 13:01:18 -07001819out:
1820 brelse(di_bh);
Mark Fasheh23193e52007-09-12 13:01:18 -07001821 return 0;
1822}
1823
Mark Fasheh9b7895e2008-11-12 16:27:44 -08001824/*
1825 * NOTE: This function can be called against unindexed directories,
1826 * and indexed ones.
1827 */
Mark Fasheh23193e52007-09-12 13:01:18 -07001828static int ocfs2_dir_foreach_blk_el(struct inode *inode,
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001829 u64 *f_version,
Al Viro3704412b2013-05-22 21:06:00 -04001830 struct dir_context *ctx,
1831 bool persist)
Mark Fashehccd979b2005-12-15 14:31:24 -08001832{
Mark Fashehaa958872006-04-21 13:49:02 -07001833 unsigned long offset, blk, last_ra_blk = 0;
Al Viro3704412b2013-05-22 21:06:00 -04001834 int i;
Mark Fashehccd979b2005-12-15 14:31:24 -08001835 struct buffer_head * bh, * tmp;
1836 struct ocfs2_dir_entry * de;
Mark Fashehccd979b2005-12-15 14:31:24 -08001837 struct super_block * sb = inode->i_sb;
Mark Fashehaa958872006-04-21 13:49:02 -07001838 unsigned int ra_sectors = 16;
Al Viro3704412b2013-05-22 21:06:00 -04001839 int stored = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001840
Mark Fashehccd979b2005-12-15 14:31:24 -08001841 bh = NULL;
1842
Al Viro3704412b2013-05-22 21:06:00 -04001843 offset = ctx->pos & (sb->s_blocksize - 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001844
Al Viro3704412b2013-05-22 21:06:00 -04001845 while (ctx->pos < i_size_read(inode)) {
1846 blk = ctx->pos >> sb->s_blocksize_bits;
Joel Beckera22305c2008-11-13 14:49:17 -08001847 if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1848 /* Skip the corrupt dirblock and keep trying */
Al Viro3704412b2013-05-22 21:06:00 -04001849 ctx->pos += sb->s_blocksize - offset;
Mark Fashehccd979b2005-12-15 14:31:24 -08001850 continue;
1851 }
1852
Mark Fashehaa958872006-04-21 13:49:02 -07001853 /* The idea here is to begin with 8k read-ahead and to stay
1854 * 4k ahead of our current position.
1855 *
1856 * TODO: Use the pagecache for this. We just need to
1857 * make sure it's cluster-safe... */
1858 if (!last_ra_blk
1859 || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1860 for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
Mark Fashehccd979b2005-12-15 14:31:24 -08001861 i > 0; i--) {
Joel Beckera22305c2008-11-13 14:49:17 -08001862 tmp = NULL;
1863 if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1864 OCFS2_BH_READAHEAD))
1865 brelse(tmp);
Mark Fashehccd979b2005-12-15 14:31:24 -08001866 }
Mark Fashehaa958872006-04-21 13:49:02 -07001867 last_ra_blk = blk;
1868 ra_sectors = 8;
Mark Fashehccd979b2005-12-15 14:31:24 -08001869 }
1870
Mark Fashehccd979b2005-12-15 14:31:24 -08001871 /* If the dir block has changed since the last call to
1872 * readdir(2), then we might be pointing to an invalid
1873 * dirent right now. Scan from the start of the block
1874 * to make sure. */
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001875 if (*f_version != inode->i_version) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001876 for (i = 0; i < sb->s_blocksize && i < offset; ) {
1877 de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1878 /* It's too expensive to do a full
1879 * dirent test each time round this
1880 * loop, but we do have to test at
1881 * least that it is non-zero. A
1882 * failure will be detected in the
1883 * dirent test below. */
1884 if (le16_to_cpu(de->rec_len) <
1885 OCFS2_DIR_REC_LEN(1))
1886 break;
1887 i += le16_to_cpu(de->rec_len);
1888 }
1889 offset = i;
Al Viro3704412b2013-05-22 21:06:00 -04001890 ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
Mark Fashehccd979b2005-12-15 14:31:24 -08001891 | offset;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001892 *f_version = inode->i_version;
Mark Fashehccd979b2005-12-15 14:31:24 -08001893 }
1894
Al Viro3704412b2013-05-22 21:06:00 -04001895 while (ctx->pos < i_size_read(inode)
Mark Fashehccd979b2005-12-15 14:31:24 -08001896 && offset < sb->s_blocksize) {
1897 de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1898 if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
1899 /* On error, skip the f_pos to the
1900 next block. */
Al Viro3704412b2013-05-22 21:06:00 -04001901 ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08001902 brelse(bh);
Al Viro3704412b2013-05-22 21:06:00 -04001903 continue;
Mark Fashehccd979b2005-12-15 14:31:24 -08001904 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001905 if (le64_to_cpu(de->inode)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08001906 unsigned char d_type = DT_UNKNOWN;
1907
1908 if (de->file_type < OCFS2_FT_MAX)
1909 d_type = ocfs2_filetype_table[de->file_type];
Al Viro3704412b2013-05-22 21:06:00 -04001910 if (!dir_emit(ctx, de->name,
Mark Fashehccd979b2005-12-15 14:31:24 -08001911 de->name_len,
Mark Fasheh7e853672007-09-10 17:30:26 -07001912 le64_to_cpu(de->inode),
Al Viro3704412b2013-05-22 21:06:00 -04001913 d_type)) {
1914 brelse(bh);
1915 return 0;
Mark Fashehe7b34012007-09-24 14:25:27 -07001916 }
Al Viro3704412b2013-05-22 21:06:00 -04001917 stored++;
Mark Fashehccd979b2005-12-15 14:31:24 -08001918 }
Al Viro3704412b2013-05-22 21:06:00 -04001919 offset += le16_to_cpu(de->rec_len);
1920 ctx->pos += le16_to_cpu(de->rec_len);
Mark Fashehccd979b2005-12-15 14:31:24 -08001921 }
1922 offset = 0;
1923 brelse(bh);
Joel Beckera22305c2008-11-13 14:49:17 -08001924 bh = NULL;
Al Viro3704412b2013-05-22 21:06:00 -04001925 if (!persist && stored)
1926 break;
Mark Fashehccd979b2005-12-15 14:31:24 -08001927 }
Al Viro3704412b2013-05-22 21:06:00 -04001928 return 0;
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001929}
1930
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001931static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
Al Viro3704412b2013-05-22 21:06:00 -04001932 struct dir_context *ctx,
1933 bool persist)
Mark Fasheh23193e52007-09-12 13:01:18 -07001934{
1935 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
Al Viro3704412b2013-05-22 21:06:00 -04001936 return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
1937 return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
Mark Fasheh23193e52007-09-12 13:01:18 -07001938}
1939
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001940/*
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001941 * This is intended to be called from inside other kernel functions,
1942 * so we fake some arguments.
1943 */
Al Viro3704412b2013-05-22 21:06:00 -04001944int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001945{
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07001946 u64 version = inode->i_version;
Al Viro3704412b2013-05-22 21:06:00 -04001947 ocfs2_dir_foreach_blk(inode, &version, ctx, true);
Mark Fasheh5eae5b92007-09-10 17:50:51 -07001948 return 0;
1949}
1950
1951/*
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001952 * ocfs2_readdir()
1953 *
1954 */
Al Viro3704412b2013-05-22 21:06:00 -04001955int ocfs2_readdir(struct file *file, struct dir_context *ctx)
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001956{
1957 int error = 0;
Al Viro3704412b2013-05-22 21:06:00 -04001958 struct inode *inode = file_inode(file);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001959 int lock_level = 0;
1960
Tao Maf1088d472011-02-23 22:30:23 +08001961 trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001962
Al Viro3704412b2013-05-22 21:06:00 -04001963 error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001964 if (lock_level && error >= 0) {
1965 /* We release EX lock which used to update atime
1966 * and get PR lock again to reduce contention
1967 * on commonly accessed directories. */
Mark Fashehe63aecb62007-10-18 15:30:42 -07001968 ocfs2_inode_unlock(inode, 1);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001969 lock_level = 0;
Mark Fashehe63aecb62007-10-18 15:30:42 -07001970 error = ocfs2_inode_lock(inode, NULL, 0);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001971 }
1972 if (error < 0) {
1973 if (error != -ENOENT)
1974 mlog_errno(error);
1975 /* we haven't got any yet, so propagate the error. */
1976 goto bail_nolock;
1977 }
1978
Al Viro3704412b2013-05-22 21:06:00 -04001979 error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001980
Mark Fashehe63aecb62007-10-18 15:30:42 -07001981 ocfs2_inode_unlock(inode, lock_level);
Tao Mac1e8d352011-03-07 16:43:21 +08001982 if (error)
1983 mlog_errno(error);
Mark Fashehccd979b2005-12-15 14:31:24 -08001984
Mark Fashehaa958872006-04-21 13:49:02 -07001985bail_nolock:
Mark Fashehccd979b2005-12-15 14:31:24 -08001986
Mark Fashehb8bc5f42007-09-10 17:17:52 -07001987 return error;
Mark Fashehccd979b2005-12-15 14:31:24 -08001988}
1989
1990/*
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001991 * NOTE: this should always be called with parent dir i_mutex taken.
Mark Fashehccd979b2005-12-15 14:31:24 -08001992 */
1993int ocfs2_find_files_on_disk(const char *name,
1994 int namelen,
1995 u64 *blkno,
1996 struct inode *inode,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08001997 struct ocfs2_dir_lookup_result *lookup)
Mark Fashehccd979b2005-12-15 14:31:24 -08001998{
1999 int status = -ENOENT;
Mark Fashehccd979b2005-12-15 14:31:24 -08002000
Tao Maf1088d472011-02-23 22:30:23 +08002001 trace_ocfs2_find_files_on_disk(namelen, name, blkno,
2002 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08002003
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002004 status = ocfs2_find_entry(name, namelen, inode, lookup);
2005 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08002006 goto leave;
Mark Fashehccd979b2005-12-15 14:31:24 -08002007
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002008 *blkno = le64_to_cpu(lookup->dl_entry->inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08002009
2010 status = 0;
2011leave:
Mark Fashehccd979b2005-12-15 14:31:24 -08002012
Mark Fashehccd979b2005-12-15 14:31:24 -08002013 return status;
2014}
2015
Mark Fashehbe94d112007-09-11 15:22:06 -07002016/*
2017 * Convenience function for callers which just want the block number
2018 * mapped to a name and don't require the full dirent info, etc.
2019 */
2020int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2021 int namelen, u64 *blkno)
2022{
2023 int ret;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002024 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehbe94d112007-09-11 15:22:06 -07002025
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002026 ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2027 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehbe94d112007-09-11 15:22:06 -07002028
2029 return ret;
2030}
2031
Mark Fashehccd979b2005-12-15 14:31:24 -08002032/* Check for a name within a directory.
2033 *
2034 * Return 0 if the name does not exist
2035 * Return -EEXIST if the directory contains the name
2036 *
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002037 * Callers should have i_mutex + a cluster lock on dir
Mark Fashehccd979b2005-12-15 14:31:24 -08002038 */
2039int ocfs2_check_dir_for_entry(struct inode *dir,
2040 const char *name,
2041 int namelen)
2042{
Daeseok Youn7c01ad82015-04-14 15:43:30 -07002043 int ret = 0;
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002044 struct ocfs2_dir_lookup_result lookup = { NULL, };
Mark Fashehccd979b2005-12-15 14:31:24 -08002045
Tao Maf1088d472011-02-23 22:30:23 +08002046 trace_ocfs2_check_dir_for_entry(
2047 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
Mark Fashehccd979b2005-12-15 14:31:24 -08002048
Daeseok Youn7c01ad82015-04-14 15:43:30 -07002049 if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
2050 ret = -EEXIST;
2051 mlog_errno(ret);
2052 }
Mark Fashehccd979b2005-12-15 14:31:24 -08002053
Mark Fasheh4a12ca32008-11-12 15:43:34 -08002054 ocfs2_free_dir_lookup_result(&lookup);
Mark Fashehccd979b2005-12-15 14:31:24 -08002055
Mark Fashehccd979b2005-12-15 14:31:24 -08002056 return ret;
2057}
2058
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002059struct ocfs2_empty_dir_priv {
Al Viro3704412b2013-05-22 21:06:00 -04002060 struct dir_context ctx;
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002061 unsigned seen_dot;
2062 unsigned seen_dot_dot;
2063 unsigned seen_other;
Mark Fashehe3a93c22009-02-17 15:29:35 -08002064 unsigned dx_dir;
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002065};
Miklos Szerediac7576f2014-10-30 17:37:34 +01002066static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
2067 int name_len, loff_t pos, u64 ino,
2068 unsigned type)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002069{
Miklos Szerediac7576f2014-10-30 17:37:34 +01002070 struct ocfs2_empty_dir_priv *p =
2071 container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002072
2073 /*
2074 * Check the positions of "." and ".." records to be sure
2075 * they're in the correct place.
Mark Fashehe3a93c22009-02-17 15:29:35 -08002076 *
2077 * Indexed directories don't need to proceed past the first
2078 * two entries, so we end the scan after seeing '..'. Despite
2079 * that, we allow the scan to proceed In the event that we
2080 * have a corrupted indexed directory (no dot or dot dot
2081 * entries). This allows us to double check for existing
2082 * entries which might not have been found in the index.
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002083 */
2084 if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2085 p->seen_dot = 1;
2086 return 0;
2087 }
2088
2089 if (name_len == 2 && !strncmp("..", name, 2) &&
2090 pos == OCFS2_DIR_REC_LEN(1)) {
2091 p->seen_dot_dot = 1;
Mark Fashehe3a93c22009-02-17 15:29:35 -08002092
2093 if (p->dx_dir && p->seen_dot)
2094 return 1;
2095
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002096 return 0;
2097 }
2098
2099 p->seen_other = 1;
2100 return 1;
2101}
Mark Fashehe3a93c22009-02-17 15:29:35 -08002102
2103static int ocfs2_empty_dir_dx(struct inode *inode,
2104 struct ocfs2_empty_dir_priv *priv)
2105{
2106 int ret;
2107 struct buffer_head *di_bh = NULL;
2108 struct buffer_head *dx_root_bh = NULL;
2109 struct ocfs2_dinode *di;
2110 struct ocfs2_dx_root_block *dx_root;
2111
2112 priv->dx_dir = 1;
2113
2114 ret = ocfs2_read_inode_block(inode, &di_bh);
2115 if (ret) {
2116 mlog_errno(ret);
2117 goto out;
2118 }
2119 di = (struct ocfs2_dinode *)di_bh->b_data;
2120
2121 ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2122 if (ret) {
2123 mlog_errno(ret);
2124 goto out;
2125 }
2126 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2127
2128 if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2129 priv->seen_other = 1;
2130
2131out:
2132 brelse(di_bh);
2133 brelse(dx_root_bh);
2134 return ret;
2135}
2136
Mark Fashehccd979b2005-12-15 14:31:24 -08002137/*
2138 * routine to check that the specified directory is empty (for rmdir)
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002139 *
2140 * Returns 1 if dir is empty, zero otherwise.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002141 *
Mark Fashehe3a93c22009-02-17 15:29:35 -08002142 * XXX: This is a performance problem for unindexed directories.
Mark Fashehccd979b2005-12-15 14:31:24 -08002143 */
2144int ocfs2_empty_dir(struct inode *inode)
2145{
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002146 int ret;
Al Viro3704412b2013-05-22 21:06:00 -04002147 struct ocfs2_empty_dir_priv priv = {
Jeff Liud6394b52013-08-13 16:01:01 -07002148 .ctx.actor = ocfs2_empty_dir_filldir,
Al Viro3704412b2013-05-22 21:06:00 -04002149 };
Mark Fashehccd979b2005-12-15 14:31:24 -08002150
Mark Fashehe3a93c22009-02-17 15:29:35 -08002151 if (ocfs2_dir_indexed(inode)) {
2152 ret = ocfs2_empty_dir_dx(inode, &priv);
2153 if (ret)
2154 mlog_errno(ret);
2155 /*
2156 * We still run ocfs2_dir_foreach to get the checks
2157 * for "." and "..".
2158 */
2159 }
2160
Al Viro3704412b2013-05-22 21:06:00 -04002161 ret = ocfs2_dir_foreach(inode, &priv.ctx);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002162 if (ret)
2163 mlog_errno(ret);
2164
2165 if (!priv.seen_dot || !priv.seen_dot_dot) {
2166 mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
Mark Fashehb06970532006-03-03 10:24:33 -08002167 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002168 /*
2169 * XXX: Is it really safe to allow an unlink to continue?
2170 */
Mark Fashehccd979b2005-12-15 14:31:24 -08002171 return 1;
2172 }
2173
Mark Fasheh0bfbbf62007-09-12 11:19:00 -07002174 return !priv.seen_other;
Mark Fashehccd979b2005-12-15 14:31:24 -08002175}
2176
Mark Fasheh87d35a72008-12-10 17:36:25 -08002177/*
2178 * Fills "." and ".." dirents in a new directory block. Returns dirent for
2179 * "..", which might be used during creation of a directory with a trailing
2180 * header. It is otherwise safe to ignore the return code.
2181 */
2182static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2183 struct inode *parent,
2184 char *start,
2185 unsigned int size)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002186{
2187 struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2188
2189 de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2190 de->name_len = 1;
2191 de->rec_len =
2192 cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2193 strcpy(de->name, ".");
2194 ocfs2_set_de_type(de, S_IFDIR);
2195
2196 de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2197 de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2198 de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2199 de->name_len = 2;
2200 strcpy(de->name, "..");
2201 ocfs2_set_de_type(de, S_IFDIR);
Mark Fasheh87d35a72008-12-10 17:36:25 -08002202
2203 return de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002204}
2205
2206/*
2207 * This works together with code in ocfs2_mknod_locked() which sets
2208 * the inline-data flag and initializes the inline-data section.
2209 */
2210static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2211 handle_t *handle,
2212 struct inode *parent,
2213 struct inode *inode,
2214 struct buffer_head *di_bh)
2215{
2216 int ret;
2217 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2218 struct ocfs2_inline_data *data = &di->id2.i_data;
2219 unsigned int size = le16_to_cpu(data->id_count);
2220
Joel Becker0cf2f762009-02-12 16:41:25 -08002221 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002222 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002223 if (ret) {
2224 mlog_errno(ret);
2225 goto out;
2226 }
2227
2228 ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002229 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002230
2231 i_size_write(inode, size);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002232 set_nlink(inode, 2);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002233 inode->i_blocks = ocfs2_inode_sector_count(inode);
2234
2235 ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2236 if (ret < 0)
2237 mlog_errno(ret);
2238
2239out:
2240 return ret;
2241}
2242
2243static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2244 handle_t *handle,
2245 struct inode *parent,
2246 struct inode *inode,
2247 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002248 struct ocfs2_alloc_context *data_ac,
2249 struct buffer_head **ret_new_bh)
Mark Fasheh316f4b92007-09-07 18:21:26 -07002250{
2251 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002252 unsigned int size = osb->sb->s_blocksize;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002253 struct buffer_head *new_bh = NULL;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002254 struct ocfs2_dir_entry *de;
Mark Fasheh316f4b92007-09-07 18:21:26 -07002255
Mark Fashehe7c17e42009-01-29 18:17:46 -08002256 if (ocfs2_new_dir_wants_trailer(inode))
Mark Fasheh87d35a72008-12-10 17:36:25 -08002257 size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2258
Mark Fasheh316f4b92007-09-07 18:21:26 -07002259 status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2260 data_ac, NULL, &new_bh);
2261 if (status < 0) {
2262 mlog_errno(status);
2263 goto bail;
2264 }
2265
Joel Becker8cb471e2009-02-10 20:00:41 -08002266 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002267
Joel Becker0cf2f762009-02-12 16:41:25 -08002268 status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002269 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002270 if (status < 0) {
2271 mlog_errno(status);
2272 goto bail;
2273 }
2274 memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2275
Mark Fasheh87d35a72008-12-10 17:36:25 -08002276 de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002277 if (ocfs2_new_dir_wants_trailer(inode)) {
2278 int size = le16_to_cpu(de->rec_len);
2279
2280 /*
2281 * Figure out the size of the hole left over after
2282 * insertion of '.' and '..'. The trailer wants this
2283 * information.
2284 */
2285 size -= OCFS2_DIR_REC_LEN(2);
2286 size -= sizeof(struct ocfs2_dir_block_trailer);
2287
2288 ocfs2_init_dir_trailer(inode, new_bh, size);
2289 }
Mark Fasheh316f4b92007-09-07 18:21:26 -07002290
Joel Beckerec20cec2010-03-19 14:13:52 -07002291 ocfs2_journal_dirty(handle, new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002292
2293 i_size_write(inode, inode->i_sb->s_blocksize);
Miklos Szeredibfe86842011-10-28 14:13:29 +02002294 set_nlink(inode, 2);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002295 inode->i_blocks = ocfs2_inode_sector_count(inode);
2296 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2297 if (status < 0) {
2298 mlog_errno(status);
2299 goto bail;
2300 }
2301
2302 status = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002303 if (ret_new_bh) {
2304 *ret_new_bh = new_bh;
2305 new_bh = NULL;
2306 }
Mark Fasheh316f4b92007-09-07 18:21:26 -07002307bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07002308 brelse(new_bh);
Mark Fasheh316f4b92007-09-07 18:21:26 -07002309
Mark Fasheh316f4b92007-09-07 18:21:26 -07002310 return status;
2311}
2312
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002313static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2314 handle_t *handle, struct inode *dir,
2315 struct buffer_head *di_bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08002316 struct buffer_head *dirdata_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002317 struct ocfs2_alloc_context *meta_ac,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002318 int dx_inline, u32 num_entries,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002319 struct buffer_head **ret_dx_root_bh)
2320{
2321 int ret;
2322 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2323 u16 dr_suballoc_bit;
Joel Becker2b6cb572010-03-26 10:09:15 +08002324 u64 suballoc_loc, dr_blkno;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002325 unsigned int num_bits;
2326 struct buffer_head *dx_root_bh = NULL;
2327 struct ocfs2_dx_root_block *dx_root;
Mark Fashehe7c17e42009-01-29 18:17:46 -08002328 struct ocfs2_dir_block_trailer *trailer =
2329 ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002330
Joel Becker2b6cb572010-03-26 10:09:15 +08002331 ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2332 &dr_suballoc_bit, &num_bits, &dr_blkno);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002333 if (ret) {
2334 mlog_errno(ret);
2335 goto out;
2336 }
2337
Tao Maf1088d472011-02-23 22:30:23 +08002338 trace_ocfs2_dx_dir_attach_index(
2339 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2340 (unsigned long long)dr_blkno);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002341
2342 dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2343 if (dx_root_bh == NULL) {
Rui Xiang7391a292013-11-12 15:06:54 -08002344 ret = -ENOMEM;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002345 goto out;
2346 }
Joel Becker8cb471e2009-02-10 20:00:41 -08002347 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002348
Joel Becker0cf2f762009-02-12 16:41:25 -08002349 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002350 OCFS2_JOURNAL_ACCESS_CREATE);
2351 if (ret < 0) {
2352 mlog_errno(ret);
2353 goto out;
2354 }
2355
2356 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2357 memset(dx_root, 0, osb->sb->s_blocksize);
2358 strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
Tiger Yangb89c5422010-01-25 14:11:06 +08002359 dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
Joel Becker2b6cb572010-03-26 10:09:15 +08002360 dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002361 dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2362 dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2363 dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2364 dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
Mark Fashehe3a93c22009-02-17 15:29:35 -08002365 dx_root->dr_num_entries = cpu_to_le32(num_entries);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002366 if (le16_to_cpu(trailer->db_free_rec_len))
2367 dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2368 else
2369 dx_root->dr_free_blk = cpu_to_le64(0);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002370
2371 if (dx_inline) {
2372 dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2373 dx_root->dr_entries.de_count =
2374 cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2375 } else {
2376 dx_root->dr_list.l_count =
2377 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2378 }
Joel Beckerec20cec2010-03-19 14:13:52 -07002379 ocfs2_journal_dirty(handle, dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002380
Joel Becker0cf2f762009-02-12 16:41:25 -08002381 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002382 OCFS2_JOURNAL_ACCESS_CREATE);
2383 if (ret) {
2384 mlog_errno(ret);
2385 goto out;
2386 }
2387
2388 di->i_dx_root = cpu_to_le64(dr_blkno);
2389
Tao Ma8ac33dc2010-12-15 16:30:00 +08002390 spin_lock(&OCFS2_I(dir)->ip_lock);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002391 OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2392 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
Tao Ma8ac33dc2010-12-15 16:30:00 +08002393 spin_unlock(&OCFS2_I(dir)->ip_lock);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002394
Joel Beckerec20cec2010-03-19 14:13:52 -07002395 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002396
2397 *ret_dx_root_bh = dx_root_bh;
2398 dx_root_bh = NULL;
2399
2400out:
2401 brelse(dx_root_bh);
2402 return ret;
2403}
2404
2405static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2406 handle_t *handle, struct inode *dir,
2407 struct buffer_head **dx_leaves,
2408 int num_dx_leaves, u64 start_blk)
2409{
2410 int ret, i;
2411 struct ocfs2_dx_leaf *dx_leaf;
2412 struct buffer_head *bh;
2413
2414 for (i = 0; i < num_dx_leaves; i++) {
2415 bh = sb_getblk(osb->sb, start_blk + i);
2416 if (bh == NULL) {
Rui Xiang7391a292013-11-12 15:06:54 -08002417 ret = -ENOMEM;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002418 goto out;
2419 }
2420 dx_leaves[i] = bh;
2421
Joel Becker8cb471e2009-02-10 20:00:41 -08002422 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002423
Joel Becker0cf2f762009-02-12 16:41:25 -08002424 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002425 OCFS2_JOURNAL_ACCESS_CREATE);
2426 if (ret < 0) {
2427 mlog_errno(ret);
2428 goto out;
2429 }
2430
2431 dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2432
2433 memset(dx_leaf, 0, osb->sb->s_blocksize);
2434 strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2435 dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2436 dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2437 dx_leaf->dl_list.de_count =
2438 cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2439
Tao Maf1088d472011-02-23 22:30:23 +08002440 trace_ocfs2_dx_dir_format_cluster(
2441 (unsigned long long)OCFS2_I(dir)->ip_blkno,
2442 (unsigned long long)bh->b_blocknr,
2443 le16_to_cpu(dx_leaf->dl_list.de_count));
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002444
2445 ocfs2_journal_dirty(handle, bh);
2446 }
2447
2448 ret = 0;
2449out:
2450 return ret;
2451}
2452
2453/*
2454 * Allocates and formats a new cluster for use in an indexed dir
2455 * leaf. This version will not do the extent insert, so that it can be
2456 * used by operations which need careful ordering.
2457 */
2458static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2459 u32 cpos, handle_t *handle,
2460 struct ocfs2_alloc_context *data_ac,
2461 struct buffer_head **dx_leaves,
2462 int num_dx_leaves, u64 *ret_phys_blkno)
2463{
2464 int ret;
2465 u32 phys, num;
2466 u64 phys_blkno;
2467 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2468
2469 /*
2470 * XXX: For create, this should claim cluster for the index
2471 * *before* the unindexed insert so that we have a better
2472 * chance of contiguousness as the directory grows in number
2473 * of entries.
2474 */
Joel Becker1ed9b772010-05-06 13:59:06 +08002475 ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002476 if (ret) {
2477 mlog_errno(ret);
2478 goto out;
2479 }
2480
2481 /*
2482 * Format the new cluster first. That way, we're inserting
2483 * valid data.
2484 */
2485 phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2486 ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2487 num_dx_leaves, phys_blkno);
2488 if (ret) {
2489 mlog_errno(ret);
2490 goto out;
2491 }
2492
2493 *ret_phys_blkno = phys_blkno;
2494out:
2495 return ret;
2496}
2497
2498static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2499 struct ocfs2_extent_tree *et,
2500 u32 cpos, handle_t *handle,
2501 struct ocfs2_alloc_context *data_ac,
2502 struct ocfs2_alloc_context *meta_ac,
2503 struct buffer_head **dx_leaves,
2504 int num_dx_leaves)
2505{
2506 int ret;
2507 u64 phys_blkno;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002508
2509 ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2510 num_dx_leaves, &phys_blkno);
2511 if (ret) {
2512 mlog_errno(ret);
2513 goto out;
2514 }
2515
Joel Beckercc79d8c2009-02-13 03:24:43 -08002516 ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002517 meta_ac);
2518 if (ret)
2519 mlog_errno(ret);
2520out:
2521 return ret;
2522}
2523
2524static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2525 int *ret_num_leaves)
2526{
2527 int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2528 struct buffer_head **dx_leaves;
2529
2530 dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2531 GFP_NOFS);
2532 if (dx_leaves && ret_num_leaves)
2533 *ret_num_leaves = num_dx_leaves;
2534
2535 return dx_leaves;
2536}
2537
2538static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2539 handle_t *handle,
2540 struct inode *parent,
2541 struct inode *inode,
2542 struct buffer_head *di_bh,
2543 struct ocfs2_alloc_context *data_ac,
2544 struct ocfs2_alloc_context *meta_ac)
2545{
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002546 int ret;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002547 struct buffer_head *leaf_bh = NULL;
2548 struct buffer_head *dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002549 struct ocfs2_dx_hinfo hinfo;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002550 struct ocfs2_dx_root_block *dx_root;
2551 struct ocfs2_dx_entry_list *entry_list;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002552
2553 /*
2554 * Our strategy is to create the directory as though it were
2555 * unindexed, then add the index block. This works with very
2556 * little complication since the state of a new directory is a
2557 * very well known quantity.
2558 *
2559 * Essentially, we have two dirents ("." and ".."), in the 1st
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002560 * block which need indexing. These are easily inserted into
2561 * the index block.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002562 */
2563
2564 ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2565 data_ac, &leaf_bh);
2566 if (ret) {
2567 mlog_errno(ret);
2568 goto out;
2569 }
2570
Mark Fashehe7c17e42009-01-29 18:17:46 -08002571 ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002572 meta_ac, 1, 2, &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002573 if (ret) {
2574 mlog_errno(ret);
2575 goto out;
2576 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002577 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2578 entry_list = &dx_root->dr_entries;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002579
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002580 /* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
Mark Fashehe7c17e42009-01-29 18:17:46 -08002581 ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002582 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002583
2584 ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002585 ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002586
2587out:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002588 brelse(dx_root_bh);
2589 brelse(leaf_bh);
2590 return ret;
2591}
2592
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002593int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2594 handle_t *handle,
2595 struct inode *parent,
2596 struct inode *inode,
2597 struct buffer_head *fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002598 struct ocfs2_alloc_context *data_ac,
2599 struct ocfs2_alloc_context *meta_ac)
2600
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002601{
2602 BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2603
2604 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2605 return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2606
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002607 if (ocfs2_supports_indexed_dirs(osb))
2608 return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2609 data_ac, meta_ac);
2610
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002611 return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002612 data_ac, NULL);
2613}
2614
2615static int ocfs2_dx_dir_index_block(struct inode *dir,
2616 handle_t *handle,
2617 struct buffer_head **dx_leaves,
2618 int num_dx_leaves,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002619 u32 *num_dx_entries,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002620 struct buffer_head *dirent_bh)
2621{
Tao Ma0fba8132009-03-19 05:08:43 +08002622 int ret = 0, namelen, i;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002623 char *de_buf, *limit;
2624 struct ocfs2_dir_entry *de;
2625 struct buffer_head *dx_leaf_bh;
2626 struct ocfs2_dx_hinfo hinfo;
2627 u64 dirent_blk = dirent_bh->b_blocknr;
2628
2629 de_buf = dirent_bh->b_data;
2630 limit = de_buf + dir->i_sb->s_blocksize;
2631
2632 while (de_buf < limit) {
2633 de = (struct ocfs2_dir_entry *)de_buf;
2634
2635 namelen = de->name_len;
2636 if (!namelen || !de->inode)
2637 goto inc;
2638
2639 ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2640
2641 i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2642 dx_leaf_bh = dx_leaves[i];
2643
2644 ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2645 dirent_blk, dx_leaf_bh);
2646 if (ret) {
2647 mlog_errno(ret);
2648 goto out;
2649 }
2650
Mark Fashehe3a93c22009-02-17 15:29:35 -08002651 *num_dx_entries = *num_dx_entries + 1;
2652
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002653inc:
2654 de_buf += le16_to_cpu(de->rec_len);
2655 }
2656
2657out:
2658 return ret;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002659}
Mark Fashehe7c17e42009-01-29 18:17:46 -08002660
2661/*
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002662 * XXX: This expects dx_root_bh to already be part of the transaction.
2663 */
2664static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2665 struct buffer_head *dx_root_bh,
2666 struct buffer_head *dirent_bh)
2667{
2668 char *de_buf, *limit;
2669 struct ocfs2_dx_root_block *dx_root;
2670 struct ocfs2_dir_entry *de;
2671 struct ocfs2_dx_hinfo hinfo;
2672 u64 dirent_blk = dirent_bh->b_blocknr;
2673
2674 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2675
2676 de_buf = dirent_bh->b_data;
2677 limit = de_buf + dir->i_sb->s_blocksize;
2678
2679 while (de_buf < limit) {
2680 de = (struct ocfs2_dir_entry *)de_buf;
2681
2682 if (!de->name_len || !de->inode)
2683 goto inc;
2684
2685 ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2686
Tao Maf1088d472011-02-23 22:30:23 +08002687 trace_ocfs2_dx_dir_index_root_block(
2688 (unsigned long long)dir->i_ino,
2689 hinfo.major_hash, hinfo.minor_hash,
2690 de->name_len, de->name,
2691 le16_to_cpu(dx_root->dr_entries.de_num_used));
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002692
2693 ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2694 dirent_blk);
Mark Fashehe3a93c22009-02-17 15:29:35 -08002695
2696 le32_add_cpu(&dx_root->dr_num_entries, 1);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002697inc:
2698 de_buf += le16_to_cpu(de->rec_len);
2699 }
2700}
2701
2702/*
2703 * Count the number of inline directory entries in di_bh and compare
2704 * them against the number of entries we can hold in an inline dx root
2705 * block.
2706 */
2707static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2708 struct buffer_head *di_bh)
2709{
2710 int dirent_count = 0;
2711 char *de_buf, *limit;
2712 struct ocfs2_dir_entry *de;
2713 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2714
2715 de_buf = di->id2.i_data.id_data;
2716 limit = de_buf + i_size_read(dir);
2717
2718 while (de_buf < limit) {
2719 de = (struct ocfs2_dir_entry *)de_buf;
2720
2721 if (de->name_len && de->inode)
2722 dirent_count++;
2723
2724 de_buf += le16_to_cpu(de->rec_len);
2725 }
2726
2727 /* We are careful to leave room for one extra record. */
2728 return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2729}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002730
Mark Fasheh87d35a72008-12-10 17:36:25 -08002731/*
2732 * Expand rec_len of the rightmost dirent in a directory block so that it
2733 * contains the end of our valid space for dirents. We do this during
2734 * expansion from an inline directory to one with extents. The first dir block
2735 * in that case is taken from the inline data portion of the inode block.
2736 *
Mark Fashehe7c17e42009-01-29 18:17:46 -08002737 * This will also return the largest amount of contiguous space for a dirent
2738 * in the block. That value is *not* necessarily the last dirent, even after
2739 * expansion. The directory indexing code wants this value for free space
2740 * accounting. We do this here since we're already walking the entire dir
2741 * block.
2742 *
Mark Fasheh87d35a72008-12-10 17:36:25 -08002743 * We add the dir trailer if this filesystem wants it.
2744 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08002745static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2746 struct inode *dir)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002747{
Mark Fashehe7c17e42009-01-29 18:17:46 -08002748 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002749 struct ocfs2_dir_entry *de;
2750 struct ocfs2_dir_entry *prev_de;
2751 char *de_buf, *limit;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002752 unsigned int new_size = sb->s_blocksize;
Mark Fashehe7c17e42009-01-29 18:17:46 -08002753 unsigned int bytes, this_hole;
2754 unsigned int largest_hole = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08002755
Mark Fashehe7c17e42009-01-29 18:17:46 -08002756 if (ocfs2_new_dir_wants_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -08002757 new_size = ocfs2_dir_trailer_blk_off(sb);
2758
2759 bytes = new_size - old_size;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002760
2761 limit = start + old_size;
2762 de_buf = start;
2763 de = (struct ocfs2_dir_entry *)de_buf;
2764 do {
Mark Fashehe7c17e42009-01-29 18:17:46 -08002765 this_hole = ocfs2_figure_dirent_hole(de);
2766 if (this_hole > largest_hole)
2767 largest_hole = this_hole;
2768
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002769 prev_de = de;
2770 de_buf += le16_to_cpu(de->rec_len);
2771 de = (struct ocfs2_dir_entry *)de_buf;
2772 } while (de_buf < limit);
2773
2774 le16_add_cpu(&prev_de->rec_len, bytes);
Mark Fashehe7c17e42009-01-29 18:17:46 -08002775
2776 /* We need to double check this after modification of the final
2777 * dirent. */
2778 this_hole = ocfs2_figure_dirent_hole(prev_de);
2779 if (this_hole > largest_hole)
2780 largest_hole = this_hole;
2781
2782 if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2783 return largest_hole;
2784 return 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002785}
2786
2787/*
2788 * We allocate enough clusters to fulfill "blocks_wanted", but set
2789 * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2790 * rest automatically for us.
2791 *
2792 * *first_block_bh is a pointer to the 1st data block allocated to the
2793 * directory.
2794 */
2795static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2796 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002797 struct ocfs2_dir_lookup_result *lookup,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002798 struct buffer_head **first_block_bh)
2799{
Mark Fashehe3a93c22009-02-17 15:29:35 -08002800 u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002801 struct super_block *sb = dir->i_sb;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002802 int ret, i, num_dx_leaves = 0, dx_inline = 0,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002803 credits = ocfs2_inline_to_extents_credits(sb);
2804 u64 dx_insert_blkno, blkno,
2805 bytes = blocks_wanted << sb->s_blocksize_bits;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002806 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2807 struct ocfs2_inode_info *oi = OCFS2_I(dir);
Marcus Meissner5d446702011-05-05 10:44:11 -07002808 struct ocfs2_alloc_context *data_ac = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002809 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002810 struct buffer_head *dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002811 struct buffer_head *dx_root_bh = NULL;
2812 struct buffer_head **dx_leaves = NULL;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002813 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2814 handle_t *handle;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002815 struct ocfs2_extent_tree et;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002816 struct ocfs2_extent_tree dx_et;
2817 int did_quota = 0, bytes_allocated = 0;
Joel Beckerf99b9b72008-08-20 19:36:33 -07002818
Joel Becker5e404e92009-02-13 03:54:22 -08002819 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002820
2821 alloc = ocfs2_clusters_for_bytes(sb, bytes);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002822 dx_alloc = 0;
2823
Jan Karaedd45c02009-06-02 14:24:03 +02002824 down_write(&oi->ip_alloc_sem);
2825
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002826 if (ocfs2_supports_indexed_dirs(osb)) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002827 credits += ocfs2_add_dir_index_credits(sb);
2828
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002829 dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2830 if (!dx_inline) {
2831 /* Add one more cluster for an index leaf */
2832 dx_alloc++;
2833 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2834 &num_dx_leaves);
2835 if (!dx_leaves) {
2836 ret = -ENOMEM;
2837 mlog_errno(ret);
2838 goto out;
2839 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002840 }
2841
2842 /* This gets us the dx_root */
2843 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2844 if (ret) {
2845 mlog_errno(ret);
2846 goto out;
2847 }
2848 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002849
2850 /*
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002851 * We should never need more than 2 clusters for the unindexed
2852 * tree - maximum dirent size is far less than one block. In
2853 * fact, the only time we'd need more than one cluster is if
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002854 * blocksize == clustersize and the dirent won't fit in the
2855 * extra space that the expansion to a single block gives. As
2856 * of today, that only happens on 4k/4k file systems.
2857 */
2858 BUG_ON(alloc > 2);
2859
Tao Ma035a5712009-04-07 07:40:57 +08002860 ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002861 if (ret) {
2862 mlog_errno(ret);
2863 goto out;
2864 }
2865
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002866 /*
Joe Perchesc78bad12008-02-03 17:33:42 +02002867 * Prepare for worst case allocation scenario of two separate
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002868 * extents in the unindexed tree.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002869 */
2870 if (alloc == 2)
2871 credits += OCFS2_SUBALLOC_ALLOC;
2872
2873 handle = ocfs2_start_trans(osb, credits);
2874 if (IS_ERR(handle)) {
2875 ret = PTR_ERR(handle);
2876 mlog_errno(ret);
Jan Karaedd45c02009-06-02 14:24:03 +02002877 goto out;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002878 }
2879
Christoph Hellwig5dd40562010-03-03 09:05:00 -05002880 ret = dquot_alloc_space_nodirty(dir,
2881 ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2882 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02002883 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02002884 did_quota = 1;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002885
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002886 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002887 /*
2888 * Allocate our index cluster first, to maximize the
2889 * possibility that unindexed leaves grow
2890 * contiguously.
2891 */
2892 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2893 dx_leaves, num_dx_leaves,
2894 &dx_insert_blkno);
2895 if (ret) {
2896 mlog_errno(ret);
2897 goto out_commit;
2898 }
2899 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2900 }
2901
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002902 /*
2903 * Try to claim as many clusters as the bitmap can give though
2904 * if we only get one now, that's enough to continue. The rest
2905 * will be claimed after the conversion to extents.
2906 */
Mark Fasheh83f92312010-04-05 18:17:16 -07002907 if (ocfs2_dir_resv_allowed(osb))
2908 data_ac->ac_resv = &oi->ip_la_data_resv;
Joel Becker1ed9b772010-05-06 13:59:06 +08002909 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002910 if (ret) {
2911 mlog_errno(ret);
2912 goto out_commit;
2913 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002914 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002915
2916 /*
2917 * Operations are carefully ordered so that we set up the new
2918 * data block first. The conversion from inline data to
2919 * extents follows.
2920 */
2921 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2922 dirdata_bh = sb_getblk(sb, blkno);
2923 if (!dirdata_bh) {
Rui Xiang7391a292013-11-12 15:06:54 -08002924 ret = -ENOMEM;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002925 mlog_errno(ret);
2926 goto out_commit;
2927 }
2928
Joel Becker8cb471e2009-02-10 20:00:41 -08002929 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002930
Joel Becker0cf2f762009-02-12 16:41:25 -08002931 ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002932 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002933 if (ret) {
2934 mlog_errno(ret);
2935 goto out_commit;
2936 }
2937
2938 memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2939 memset(dirdata_bh->b_data + i_size_read(dir), 0,
2940 sb->s_blocksize - i_size_read(dir));
Mark Fashehe7c17e42009-01-29 18:17:46 -08002941 i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
2942 if (ocfs2_new_dir_wants_trailer(dir)) {
2943 /*
2944 * Prepare the dir trailer up front. It will otherwise look
2945 * like a valid dirent. Even if inserting the index fails
2946 * (unlikely), then all we'll have done is given first dir
2947 * block a small amount of fragmentation.
2948 */
2949 ocfs2_init_dir_trailer(dir, dirdata_bh, i);
2950 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002951
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07002952 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Joel Beckerec20cec2010-03-19 14:13:52 -07002953 ocfs2_journal_dirty(handle, dirdata_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002954
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002955 if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2956 /*
2957 * Dx dirs with an external cluster need to do this up
2958 * front. Inline dx root's get handled later, after
Mark Fashehe3a93c22009-02-17 15:29:35 -08002959 * we've allocated our root block. We get passed back
2960 * a total number of items so that dr_num_entries can
2961 * be correctly set once the dx_root has been
2962 * allocated.
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08002963 */
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002964 ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
Mark Fashehe3a93c22009-02-17 15:29:35 -08002965 num_dx_leaves, &num_dx_entries,
2966 dirdata_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08002967 if (ret) {
2968 mlog_errno(ret);
2969 goto out_commit;
2970 }
2971 }
2972
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002973 /*
2974 * Set extent, i_size, etc on the directory. After this, the
2975 * inode should contain the same exact dirents as before and
2976 * be fully accessible from system calls.
2977 *
2978 * We let the later dirent insert modify c/mtime - to the user
2979 * the data hasn't changed.
2980 */
Joel Becker0cf2f762009-02-12 16:41:25 -08002981 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07002982 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07002983 if (ret) {
2984 mlog_errno(ret);
2985 goto out_commit;
2986 }
2987
2988 spin_lock(&oi->ip_lock);
2989 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
2990 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2991 spin_unlock(&oi->ip_lock);
2992
2993 ocfs2_dinode_new_extent_list(dir, di);
2994
2995 i_size_write(dir, sb->s_blocksize);
2996 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2997
2998 di->i_size = cpu_to_le64(sb->s_blocksize);
2999 di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
3000 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07003001 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003002
3003 /*
3004 * This should never fail as our extent list is empty and all
3005 * related blocks have been journaled already.
3006 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08003007 ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
Joel Beckerf99b9b72008-08-20 19:36:33 -07003008 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003009 if (ret) {
3010 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08003011 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003012 }
3013
Mark Fasheh9780eb62008-08-05 11:32:46 -07003014 /*
3015 * Set i_blocks after the extent insert for the most up to
3016 * date ip_clusters value.
3017 */
3018 dir->i_blocks = ocfs2_inode_sector_count(dir);
3019
Joel Beckerec20cec2010-03-19 14:13:52 -07003020 ocfs2_journal_dirty(handle, di_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003021
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003022 if (ocfs2_supports_indexed_dirs(osb)) {
3023 ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
Mark Fashehe7c17e42009-01-29 18:17:46 -08003024 dirdata_bh, meta_ac, dx_inline,
Mark Fashehe3a93c22009-02-17 15:29:35 -08003025 num_dx_entries, &dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003026 if (ret) {
3027 mlog_errno(ret);
3028 goto out_commit;
3029 }
3030
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003031 if (dx_inline) {
3032 ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3033 dirdata_bh);
3034 } else {
Joel Becker5e404e92009-02-13 03:54:22 -08003035 ocfs2_init_dx_root_extent_tree(&dx_et,
3036 INODE_CACHE(dir),
3037 dx_root_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08003038 ret = ocfs2_insert_extent(handle, &dx_et, 0,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003039 dx_insert_blkno, 1, 0, NULL);
3040 if (ret)
3041 mlog_errno(ret);
3042 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003043 }
3044
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003045 /*
3046 * We asked for two clusters, but only got one in the 1st
3047 * pass. Claim the 2nd cluster as a separate extent.
3048 */
3049 if (alloc > len) {
Joel Becker1ed9b772010-05-06 13:59:06 +08003050 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003051 &len);
3052 if (ret) {
3053 mlog_errno(ret);
3054 goto out_commit;
3055 }
3056 blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3057
Joel Beckercc79d8c2009-02-13 03:24:43 -08003058 ret = ocfs2_insert_extent(handle, &et, 1,
Joel Beckerf99b9b72008-08-20 19:36:33 -07003059 blkno, len, 0, NULL);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003060 if (ret) {
3061 mlog_errno(ret);
Tao Ma83cab532008-08-21 14:14:27 +08003062 goto out_commit;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003063 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003064 bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003065 }
3066
3067 *first_block_bh = dirdata_bh;
3068 dirdata_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003069 if (ocfs2_supports_indexed_dirs(osb)) {
3070 unsigned int off;
3071
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08003072 if (!dx_inline) {
3073 /*
3074 * We need to return the correct block within the
3075 * cluster which should hold our entry.
3076 */
3077 off = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb),
3078 &lookup->dl_hinfo);
3079 get_bh(dx_leaves[off]);
3080 lookup->dl_dx_leaf_bh = dx_leaves[off];
3081 }
3082 lookup->dl_dx_root_bh = dx_root_bh;
3083 dx_root_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003084 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003085
3086out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02003087 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003088 dquot_free_space_nodirty(dir, bytes_allocated);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003089
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003090 ocfs2_commit_trans(osb, handle);
3091
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003092out:
Jan Karaedd45c02009-06-02 14:24:03 +02003093 up_write(&oi->ip_alloc_sem);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003094 if (data_ac)
3095 ocfs2_free_alloc_context(data_ac);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003096 if (meta_ac)
3097 ocfs2_free_alloc_context(meta_ac);
3098
3099 if (dx_leaves) {
3100 for (i = 0; i < num_dx_leaves; i++)
3101 brelse(dx_leaves[i]);
3102 kfree(dx_leaves);
3103 }
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003104
3105 brelse(dirdata_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003106 brelse(dx_root_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003107
3108 return ret;
3109}
3110
Mark Fashehccd979b2005-12-15 14:31:24 -08003111/* returns a bh of the 1st new block in the allocation. */
Mark Fasheh316f4b92007-09-07 18:21:26 -07003112static int ocfs2_do_extend_dir(struct super_block *sb,
3113 handle_t *handle,
3114 struct inode *dir,
3115 struct buffer_head *parent_fe_bh,
3116 struct ocfs2_alloc_context *data_ac,
3117 struct ocfs2_alloc_context *meta_ac,
3118 struct buffer_head **new_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08003119{
3120 int status;
Jan Karaa90714c2008-10-09 19:38:40 +02003121 int extend, did_quota = 0;
Mark Fasheh8110b072007-03-22 16:53:23 -07003122 u64 p_blkno, v_blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08003123
3124 spin_lock(&OCFS2_I(dir)->ip_lock);
3125 extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3126 spin_unlock(&OCFS2_I(dir)->ip_lock);
3127
3128 if (extend) {
Mark Fashehdcd05382007-01-16 11:32:23 -08003129 u32 offset = OCFS2_I(dir)->ip_clusters;
3130
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003131 status = dquot_alloc_space_nodirty(dir,
3132 ocfs2_clusters_to_bytes(sb, 1));
3133 if (status)
Jan Karaa90714c2008-10-09 19:38:40 +02003134 goto bail;
Jan Karaa90714c2008-10-09 19:38:40 +02003135 did_quota = 1;
3136
Tao Ma0eb8d47e2008-08-18 17:38:45 +08003137 status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3138 1, 0, parent_fe_bh, handle,
3139 data_ac, meta_ac, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003140 BUG_ON(status == -EAGAIN);
3141 if (status < 0) {
3142 mlog_errno(status);
3143 goto bail;
3144 }
3145 }
3146
Mark Fasheh8110b072007-03-22 16:53:23 -07003147 v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3148 status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -08003149 if (status < 0) {
3150 mlog_errno(status);
3151 goto bail;
3152 }
3153
3154 *new_bh = sb_getblk(sb, p_blkno);
3155 if (!*new_bh) {
Rui Xiang7391a292013-11-12 15:06:54 -08003156 status = -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -08003157 mlog_errno(status);
3158 goto bail;
3159 }
3160 status = 0;
3161bail:
Jan Karaa90714c2008-10-09 19:38:40 +02003162 if (did_quota && status < 0)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003163 dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
Mark Fashehccd979b2005-12-15 14:31:24 -08003164 return status;
3165}
3166
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003167/*
3168 * Assumes you already have a cluster lock on the directory.
3169 *
3170 * 'blocks_wanted' is only used if we have an inline directory which
3171 * is to be turned into an extent based one. The size of the dirent to
3172 * insert might be larger than the space gained by growing to just one
3173 * block, so we may have to grow the inode by two blocks in that case.
Mark Fashehe7c17e42009-01-29 18:17:46 -08003174 *
3175 * If the directory is already indexed, dx_root_bh must be provided.
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003176 */
Mark Fashehccd979b2005-12-15 14:31:24 -08003177static int ocfs2_extend_dir(struct ocfs2_super *osb,
3178 struct inode *dir,
3179 struct buffer_head *parent_fe_bh,
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003180 unsigned int blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003181 struct ocfs2_dir_lookup_result *lookup,
Mark Fashehccd979b2005-12-15 14:31:24 -08003182 struct buffer_head **new_de_bh)
3183{
3184 int status = 0;
Joel Beckeree19a772007-03-28 18:27:07 -07003185 int credits, num_free_extents, drop_alloc_sem = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08003186 loff_t dir_i_size;
3187 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
Tao Ma811f9332008-08-18 17:38:43 +08003188 struct ocfs2_extent_list *el = &fe->id2.i_list;
Mark Fashehccd979b2005-12-15 14:31:24 -08003189 struct ocfs2_alloc_context *data_ac = NULL;
3190 struct ocfs2_alloc_context *meta_ac = NULL;
Mark Fasheh1fabe142006-10-09 18:11:45 -07003191 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003192 struct buffer_head *new_bh = NULL;
3193 struct ocfs2_dir_entry * de;
3194 struct super_block *sb = osb->sb;
Joel Beckerf99b9b72008-08-20 19:36:33 -07003195 struct ocfs2_extent_tree et;
Mark Fashehe7c17e42009-01-29 18:17:46 -08003196 struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
Mark Fashehccd979b2005-12-15 14:31:24 -08003197
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003198 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08003199 /*
3200 * This would be a code error as an inline directory should
3201 * never have an index root.
3202 */
3203 BUG_ON(dx_root_bh);
3204
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003205 status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003206 blocks_wanted, lookup,
3207 &new_bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003208 if (status) {
3209 mlog_errno(status);
3210 goto bail;
3211 }
3212
Mark Fashehe7c17e42009-01-29 18:17:46 -08003213 /* Expansion from inline to an indexed directory will
3214 * have given us this. */
3215 dx_root_bh = lookup->dl_dx_root_bh;
3216
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003217 if (blocks_wanted == 1) {
3218 /*
3219 * If the new dirent will fit inside the space
3220 * created by pushing out to one block, then
3221 * we can complete the operation
3222 * here. Otherwise we have to expand i_size
3223 * and format the 2nd block below.
3224 */
3225 BUG_ON(new_bh == NULL);
3226 goto bail_bh;
3227 }
3228
3229 /*
3230 * Get rid of 'new_bh' - we want to format the 2nd
3231 * data block and return that instead.
3232 */
3233 brelse(new_bh);
3234 new_bh = NULL;
3235
Jan Karaedd45c02009-06-02 14:24:03 +02003236 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3237 drop_alloc_sem = 1;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003238 dir_i_size = i_size_read(dir);
3239 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3240 goto do_extend;
3241 }
3242
Jan Karaedd45c02009-06-02 14:24:03 +02003243 down_write(&OCFS2_I(dir)->ip_alloc_sem);
3244 drop_alloc_sem = 1;
Mark Fashehccd979b2005-12-15 14:31:24 -08003245 dir_i_size = i_size_read(dir);
Tao Maf1088d472011-02-23 22:30:23 +08003246 trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
3247 dir_i_size);
Mark Fashehccd979b2005-12-15 14:31:24 -08003248
Mark Fashehccd979b2005-12-15 14:31:24 -08003249 /* dir->i_size is always block aligned. */
3250 spin_lock(&OCFS2_I(dir)->ip_lock);
3251 if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3252 spin_unlock(&OCFS2_I(dir)->ip_lock);
Joel Becker5e404e92009-02-13 03:54:22 -08003253 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3254 parent_fe_bh);
Joel Becker3d03a302009-02-12 17:49:26 -08003255 num_free_extents = ocfs2_num_free_extents(osb, &et);
Mark Fashehccd979b2005-12-15 14:31:24 -08003256 if (num_free_extents < 0) {
3257 status = num_free_extents;
3258 mlog_errno(status);
3259 goto bail;
3260 }
3261
3262 if (!num_free_extents) {
Tao Ma811f9332008-08-18 17:38:43 +08003263 status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003264 if (status < 0) {
3265 if (status != -ENOSPC)
3266 mlog_errno(status);
3267 goto bail;
3268 }
3269 }
3270
Mark Fashehda5cbf22006-10-06 18:34:35 -07003271 status = ocfs2_reserve_clusters(osb, 1, &data_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08003272 if (status < 0) {
3273 if (status != -ENOSPC)
3274 mlog_errno(status);
3275 goto bail;
3276 }
3277
Mark Fasheh83f92312010-04-05 18:17:16 -07003278 if (ocfs2_dir_resv_allowed(osb))
3279 data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
Mark Fashehe3b4a972009-12-07 13:16:07 -08003280
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003281 credits = ocfs2_calc_extend_credits(sb, el);
Mark Fashehccd979b2005-12-15 14:31:24 -08003282 } else {
3283 spin_unlock(&OCFS2_I(dir)->ip_lock);
3284 credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3285 }
3286
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003287do_extend:
Mark Fashehe7c17e42009-01-29 18:17:46 -08003288 if (ocfs2_dir_indexed(dir))
3289 credits++; /* For attaching the new dirent block to the
3290 * dx_root */
3291
Mark Fasheh65eff9c2006-10-09 17:26:22 -07003292 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08003293 if (IS_ERR(handle)) {
3294 status = PTR_ERR(handle);
3295 handle = NULL;
3296 mlog_errno(status);
3297 goto bail;
3298 }
3299
3300 status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3301 data_ac, meta_ac, &new_bh);
3302 if (status < 0) {
3303 mlog_errno(status);
3304 goto bail;
3305 }
3306
Joel Becker8cb471e2009-02-10 20:00:41 -08003307 ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003308
Joel Becker0cf2f762009-02-12 16:41:25 -08003309 status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
Joel Becker13723d02008-10-17 19:25:01 -07003310 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08003311 if (status < 0) {
3312 mlog_errno(status);
3313 goto bail;
3314 }
3315 memset(new_bh->b_data, 0, sb->s_blocksize);
Mark Fasheh87d35a72008-12-10 17:36:25 -08003316
Mark Fashehccd979b2005-12-15 14:31:24 -08003317 de = (struct ocfs2_dir_entry *) new_bh->b_data;
3318 de->inode = 0;
Mark Fashehe7c17e42009-01-29 18:17:46 -08003319 if (ocfs2_supports_dir_trailer(dir)) {
Mark Fasheh87d35a72008-12-10 17:36:25 -08003320 de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
Mark Fashehe7c17e42009-01-29 18:17:46 -08003321
3322 ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3323
3324 if (ocfs2_dir_indexed(dir)) {
3325 status = ocfs2_dx_dir_link_trailer(dir, handle,
3326 dx_root_bh, new_bh);
3327 if (status) {
3328 mlog_errno(status);
3329 goto bail;
3330 }
3331 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003332 } else {
3333 de->rec_len = cpu_to_le16(sb->s_blocksize);
3334 }
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07003335 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Joel Beckerec20cec2010-03-19 14:13:52 -07003336 ocfs2_journal_dirty(handle, new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003337
3338 dir_i_size += dir->i_sb->s_blocksize;
3339 i_size_write(dir, dir_i_size);
Mark Fasheh8110b072007-03-22 16:53:23 -07003340 dir->i_blocks = ocfs2_inode_sector_count(dir);
Mark Fashehccd979b2005-12-15 14:31:24 -08003341 status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3342 if (status < 0) {
3343 mlog_errno(status);
3344 goto bail;
3345 }
3346
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003347bail_bh:
Mark Fashehccd979b2005-12-15 14:31:24 -08003348 *new_de_bh = new_bh;
3349 get_bh(*new_de_bh);
3350bail:
3351 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07003352 ocfs2_commit_trans(osb, handle);
Jan Karaedd45c02009-06-02 14:24:03 +02003353 if (drop_alloc_sem)
3354 up_write(&OCFS2_I(dir)->ip_alloc_sem);
Mark Fashehccd979b2005-12-15 14:31:24 -08003355
3356 if (data_ac)
3357 ocfs2_free_alloc_context(data_ac);
3358 if (meta_ac)
3359 ocfs2_free_alloc_context(meta_ac);
3360
Mark Fasheha81cb882008-10-07 14:25:16 -07003361 brelse(new_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08003362
Mark Fashehccd979b2005-12-15 14:31:24 -08003363 return status;
3364}
3365
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003366static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3367 const char *name, int namelen,
3368 struct buffer_head **ret_de_bh,
3369 unsigned int *blocks_wanted)
3370{
3371 int ret;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003372 struct super_block *sb = dir->i_sb;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003373 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3374 struct ocfs2_dir_entry *de, *last_de = NULL;
3375 char *de_buf, *limit;
3376 unsigned long offset = 0;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003377 unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
3378
3379 /*
3380 * This calculates how many free bytes we'd have in block zero, should
3381 * this function force expansion to an extent tree.
3382 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08003383 if (ocfs2_new_dir_wants_trailer(dir))
Mark Fasheh87d35a72008-12-10 17:36:25 -08003384 free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3385 else
3386 free_space = dir->i_sb->s_blocksize - i_size_read(dir);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003387
3388 de_buf = di->id2.i_data.id_data;
3389 limit = de_buf + i_size_read(dir);
3390 rec_len = OCFS2_DIR_REC_LEN(namelen);
3391
3392 while (de_buf < limit) {
3393 de = (struct ocfs2_dir_entry *)de_buf;
3394
3395 if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
3396 ret = -ENOENT;
3397 goto out;
3398 }
3399 if (ocfs2_match(namelen, name, de)) {
3400 ret = -EEXIST;
3401 goto out;
3402 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003403 /*
3404 * No need to check for a trailing dirent record here as
3405 * they're not used for inline dirs.
3406 */
3407
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003408 if (ocfs2_dirent_would_fit(de, rec_len)) {
3409 /* Ok, we found a spot. Return this bh and let
3410 * the caller actually fill it in. */
3411 *ret_de_bh = di_bh;
3412 get_bh(*ret_de_bh);
3413 ret = 0;
3414 goto out;
3415 }
3416
3417 last_de = de;
3418 de_buf += le16_to_cpu(de->rec_len);
3419 offset += le16_to_cpu(de->rec_len);
3420 }
3421
3422 /*
3423 * We're going to require expansion of the directory - figure
3424 * out how many blocks we'll need so that a place for the
3425 * dirent can be found.
3426 */
3427 *blocks_wanted = 1;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003428 new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003429 if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3430 *blocks_wanted = 2;
3431
3432 ret = -ENOSPC;
3433out:
3434 return ret;
3435}
3436
3437static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3438 int namelen, struct buffer_head **ret_de_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08003439{
3440 unsigned long offset;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003441 struct buffer_head *bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08003442 unsigned short rec_len;
Mark Fashehccd979b2005-12-15 14:31:24 -08003443 struct ocfs2_dir_entry *de;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003444 struct super_block *sb = dir->i_sb;
Mark Fashehccd979b2005-12-15 14:31:24 -08003445 int status;
Mark Fasheh87d35a72008-12-10 17:36:25 -08003446 int blocksize = dir->i_sb->s_blocksize;
Mark Fashehccd979b2005-12-15 14:31:24 -08003447
Joel Beckera22305c2008-11-13 14:49:17 -08003448 status = ocfs2_read_dir_block(dir, 0, &bh, 0);
Daeseok Youn9b572692015-02-10 14:09:15 -08003449 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08003450 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08003451
3452 rec_len = OCFS2_DIR_REC_LEN(namelen);
3453 offset = 0;
3454 de = (struct ocfs2_dir_entry *) bh->b_data;
3455 while (1) {
3456 if ((char *)de >= sb->s_blocksize + bh->b_data) {
3457 brelse(bh);
3458 bh = NULL;
3459
3460 if (i_size_read(dir) <= offset) {
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003461 /*
3462 * Caller will have to expand this
3463 * directory.
3464 */
3465 status = -ENOSPC;
Mark Fashehccd979b2005-12-15 14:31:24 -08003466 goto bail;
3467 }
Joel Beckera22305c2008-11-13 14:49:17 -08003468 status = ocfs2_read_dir_block(dir,
3469 offset >> sb->s_blocksize_bits,
3470 &bh, 0);
Daeseok Youn9b572692015-02-10 14:09:15 -08003471 if (status)
Mark Fashehccd979b2005-12-15 14:31:24 -08003472 goto bail;
Daeseok Youn9b572692015-02-10 14:09:15 -08003473
Mark Fashehccd979b2005-12-15 14:31:24 -08003474 /* move to next block */
3475 de = (struct ocfs2_dir_entry *) bh->b_data;
3476 }
3477 if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
3478 status = -ENOENT;
3479 goto bail;
3480 }
3481 if (ocfs2_match(namelen, name, de)) {
3482 status = -EEXIST;
3483 goto bail;
3484 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003485
3486 if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3487 blocksize))
3488 goto next;
3489
Mark Fasheh8553cf42007-09-13 16:29:01 -07003490 if (ocfs2_dirent_would_fit(de, rec_len)) {
Mark Fashehccd979b2005-12-15 14:31:24 -08003491 /* Ok, we found a spot. Return this bh and let
3492 * the caller actually fill it in. */
3493 *ret_de_bh = bh;
3494 get_bh(*ret_de_bh);
3495 status = 0;
3496 goto bail;
3497 }
Mark Fasheh87d35a72008-12-10 17:36:25 -08003498next:
Mark Fashehccd979b2005-12-15 14:31:24 -08003499 offset += le16_to_cpu(de->rec_len);
3500 de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3501 }
3502
Mark Fashehccd979b2005-12-15 14:31:24 -08003503bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07003504 brelse(bh);
Tao Mac1e8d352011-03-07 16:43:21 +08003505 if (status)
3506 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08003507
Mark Fashehccd979b2005-12-15 14:31:24 -08003508 return status;
3509}
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07003510
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003511static int dx_leaf_sort_cmp(const void *a, const void *b)
3512{
3513 const struct ocfs2_dx_entry *entry1 = a;
3514 const struct ocfs2_dx_entry *entry2 = b;
3515 u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3516 u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3517 u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3518 u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3519
3520 if (major_hash1 > major_hash2)
3521 return 1;
3522 if (major_hash1 < major_hash2)
3523 return -1;
3524
3525 /*
3526 * It is not strictly necessary to sort by minor
3527 */
3528 if (minor_hash1 > minor_hash2)
3529 return 1;
3530 if (minor_hash1 < minor_hash2)
3531 return -1;
3532 return 0;
3533}
3534
3535static void dx_leaf_sort_swap(void *a, void *b, int size)
3536{
3537 struct ocfs2_dx_entry *entry1 = a;
3538 struct ocfs2_dx_entry *entry2 = b;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003539
3540 BUG_ON(size != sizeof(*entry1));
3541
Fabian Frederick2a28f982015-06-24 16:55:26 -07003542 swap(*entry1, *entry2);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003543}
3544
3545static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3546{
3547 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3548 int i, num = le16_to_cpu(dl_list->de_num_used);
3549
3550 for (i = 0; i < (num - 1); i++) {
3551 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3552 le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3553 return 0;
3554 }
3555
3556 return 1;
3557}
3558
3559/*
3560 * Find the optimal value to split this leaf on. This expects the leaf
3561 * entries to be in sorted order.
3562 *
3563 * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3564 * the hash we want to insert.
3565 *
3566 * This function is only concerned with the major hash - that which
3567 * determines which cluster an item belongs to.
3568 */
3569static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3570 u32 leaf_cpos, u32 insert_hash,
3571 u32 *split_hash)
3572{
3573 struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3574 int i, num_used = le16_to_cpu(dl_list->de_num_used);
3575 int allsame;
3576
3577 /*
3578 * There's a couple rare, but nasty corner cases we have to
3579 * check for here. All of them involve a leaf where all value
3580 * have the same hash, which is what we look for first.
3581 *
3582 * Most of the time, all of the above is false, and we simply
3583 * pick the median value for a split.
3584 */
3585 allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3586 if (allsame) {
3587 u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3588
3589 if (val == insert_hash) {
3590 /*
3591 * No matter where we would choose to split,
3592 * the new entry would want to occupy the same
3593 * block as these. Since there's no space left
3594 * in their existing block, we know there
3595 * won't be space after the split.
3596 */
3597 return -ENOSPC;
3598 }
3599
3600 if (val == leaf_cpos) {
3601 /*
3602 * Because val is the same as leaf_cpos (which
3603 * is the smallest value this leaf can have),
3604 * yet is not equal to insert_hash, then we
3605 * know that insert_hash *must* be larger than
3606 * val (and leaf_cpos). At least cpos+1 in value.
3607 *
3608 * We also know then, that there cannot be an
3609 * adjacent extent (otherwise we'd be looking
3610 * at it). Choosing this value gives us a
3611 * chance to get some contiguousness.
3612 */
3613 *split_hash = leaf_cpos + 1;
3614 return 0;
3615 }
3616
3617 if (val > insert_hash) {
3618 /*
3619 * val can not be the same as insert hash, and
3620 * also must be larger than leaf_cpos. Also,
3621 * we know that there can't be a leaf between
3622 * cpos and val, otherwise the entries with
3623 * hash 'val' would be there.
3624 */
3625 *split_hash = val;
3626 return 0;
3627 }
3628
3629 *split_hash = insert_hash;
3630 return 0;
3631 }
3632
3633 /*
3634 * Since the records are sorted and the checks above
3635 * guaranteed that not all records in this block are the same,
3636 * we simple travel forward, from the median, and pick the 1st
3637 * record whose value is larger than leaf_cpos.
3638 */
3639 for (i = (num_used / 2); i < num_used; i++)
3640 if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3641 leaf_cpos)
3642 break;
3643
3644 BUG_ON(i == num_used); /* Should be impossible */
3645 *split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3646 return 0;
3647}
3648
3649/*
3650 * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3651 * larger than split_hash into new_dx_leaves. We use a temporary
3652 * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3653 *
3654 * Since the block offset inside a leaf (cluster) is a constant mask
3655 * of minor_hash, we can optimize - an item at block offset X within
3656 * the original cluster, will be at offset X within the new cluster.
3657 */
3658static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3659 handle_t *handle,
3660 struct ocfs2_dx_leaf *tmp_dx_leaf,
3661 struct buffer_head **orig_dx_leaves,
3662 struct buffer_head **new_dx_leaves,
3663 int num_dx_leaves)
3664{
3665 int i, j, num_used;
3666 u32 major_hash;
3667 struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3668 struct ocfs2_dx_entry_list *orig_list, *new_list, *tmp_list;
3669 struct ocfs2_dx_entry *dx_entry;
3670
3671 tmp_list = &tmp_dx_leaf->dl_list;
3672
3673 for (i = 0; i < num_dx_leaves; i++) {
3674 orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3675 orig_list = &orig_dx_leaf->dl_list;
3676 new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3677 new_list = &new_dx_leaf->dl_list;
3678
3679 num_used = le16_to_cpu(orig_list->de_num_used);
3680
3681 memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3682 tmp_list->de_num_used = cpu_to_le16(0);
3683 memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3684
3685 for (j = 0; j < num_used; j++) {
3686 dx_entry = &orig_list->de_entries[j];
3687 major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3688 if (major_hash >= split_hash)
3689 ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3690 dx_entry);
3691 else
3692 ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3693 dx_entry);
3694 }
3695 memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3696
3697 ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3698 ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3699 }
3700}
3701
3702static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3703 struct ocfs2_dx_root_block *dx_root)
3704{
3705 int credits = ocfs2_clusters_to_blocks(osb->sb, 2);
3706
Goldwyn Rodrigues06f9da62013-11-12 15:06:52 -08003707 credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003708 credits += ocfs2_quota_trans_credits(osb->sb);
3709 return credits;
3710}
3711
3712/*
3713 * Find the median value in dx_leaf_bh and allocate a new leaf to move
3714 * half our entries into.
3715 */
3716static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3717 struct buffer_head *dx_root_bh,
3718 struct buffer_head *dx_leaf_bh,
3719 struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3720 u64 leaf_blkno)
3721{
3722 struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3723 int credits, ret, i, num_used, did_quota = 0;
3724 u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3725 u64 orig_leaves_start;
3726 int num_dx_leaves;
3727 struct buffer_head **orig_dx_leaves = NULL;
3728 struct buffer_head **new_dx_leaves = NULL;
3729 struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3730 struct ocfs2_extent_tree et;
3731 handle_t *handle = NULL;
3732 struct ocfs2_dx_root_block *dx_root;
3733 struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3734
Tao Maf1088d472011-02-23 22:30:23 +08003735 trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
3736 (unsigned long long)leaf_blkno,
3737 insert_hash);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003738
Joel Becker5e404e92009-02-13 03:54:22 -08003739 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003740
3741 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3742 /*
3743 * XXX: This is a rather large limit. We should use a more
3744 * realistic value.
3745 */
3746 if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3747 return -ENOSPC;
3748
3749 num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3750 if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3751 mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3752 "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3753 (unsigned long long)leaf_blkno, num_used);
3754 ret = -EIO;
3755 goto out;
3756 }
3757
3758 orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3759 if (!orig_dx_leaves) {
3760 ret = -ENOMEM;
3761 mlog_errno(ret);
3762 goto out;
3763 }
3764
3765 new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3766 if (!new_dx_leaves) {
3767 ret = -ENOMEM;
3768 mlog_errno(ret);
3769 goto out;
3770 }
3771
3772 ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3773 if (ret) {
3774 if (ret != -ENOSPC)
3775 mlog_errno(ret);
3776 goto out;
3777 }
3778
3779 credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3780 handle = ocfs2_start_trans(osb, credits);
3781 if (IS_ERR(handle)) {
3782 ret = PTR_ERR(handle);
3783 handle = NULL;
3784 mlog_errno(ret);
3785 goto out;
3786 }
3787
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003788 ret = dquot_alloc_space_nodirty(dir,
3789 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3790 if (ret)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003791 goto out_commit;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003792 did_quota = 1;
3793
Joel Becker0cf2f762009-02-12 16:41:25 -08003794 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003795 OCFS2_JOURNAL_ACCESS_WRITE);
3796 if (ret) {
3797 mlog_errno(ret);
3798 goto out_commit;
3799 }
3800
3801 /*
3802 * This block is changing anyway, so we can sort it in place.
3803 */
3804 sort(dx_leaf->dl_list.de_entries, num_used,
3805 sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3806 dx_leaf_sort_swap);
3807
Joel Beckerec20cec2010-03-19 14:13:52 -07003808 ocfs2_journal_dirty(handle, dx_leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003809
3810 ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3811 &split_hash);
3812 if (ret) {
3813 mlog_errno(ret);
3814 goto out_commit;
3815 }
3816
Tao Maf1088d472011-02-23 22:30:23 +08003817 trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003818
3819 /*
3820 * We have to carefully order operations here. There are items
3821 * which want to be in the new cluster before insert, but in
3822 * order to put those items in the new cluster, we alter the
3823 * old cluster. A failure to insert gets nasty.
3824 *
3825 * So, start by reserving writes to the old
3826 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3827 * the new cluster for us, before inserting it. The insert
3828 * won't happen if there's an error before that. Once the
3829 * insert is done then, we can transfer from one leaf into the
3830 * other without fear of hitting any error.
3831 */
3832
3833 /*
3834 * The leaf transfer wants some scratch space so that we don't
3835 * wind up doing a bunch of expensive memmove().
3836 */
3837 tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3838 if (!tmp_dx_leaf) {
3839 ret = -ENOMEM;
3840 mlog_errno(ret);
3841 goto out_commit;
3842 }
3843
Mark Fasheh1d46dc02009-02-19 13:17:05 -08003844 orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003845 ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3846 orig_dx_leaves);
3847 if (ret) {
3848 mlog_errno(ret);
3849 goto out_commit;
3850 }
3851
Tristan Ye0f4da212010-09-08 17:12:38 +08003852 cpos = split_hash;
3853 ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3854 data_ac, meta_ac, new_dx_leaves,
3855 num_dx_leaves);
3856 if (ret) {
3857 mlog_errno(ret);
3858 goto out_commit;
3859 }
3860
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003861 for (i = 0; i < num_dx_leaves; i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003862 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3863 orig_dx_leaves[i],
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003864 OCFS2_JOURNAL_ACCESS_WRITE);
3865 if (ret) {
3866 mlog_errno(ret);
3867 goto out_commit;
3868 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003869
Tristan Ye0f4da212010-09-08 17:12:38 +08003870 ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3871 new_dx_leaves[i],
3872 OCFS2_JOURNAL_ACCESS_WRITE);
3873 if (ret) {
3874 mlog_errno(ret);
3875 goto out_commit;
3876 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003877 }
3878
3879 ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3880 orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3881
3882out_commit:
3883 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05003884 dquot_free_space_nodirty(dir,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003885 ocfs2_clusters_to_bytes(dir->i_sb, 1));
3886
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07003887 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08003888 ocfs2_commit_trans(osb, handle);
3889
3890out:
3891 if (orig_dx_leaves || new_dx_leaves) {
3892 for (i = 0; i < num_dx_leaves; i++) {
3893 if (orig_dx_leaves)
3894 brelse(orig_dx_leaves[i]);
3895 if (new_dx_leaves)
3896 brelse(new_dx_leaves[i]);
3897 }
3898 kfree(orig_dx_leaves);
3899 kfree(new_dx_leaves);
3900 }
3901
3902 if (meta_ac)
3903 ocfs2_free_alloc_context(meta_ac);
3904 if (data_ac)
3905 ocfs2_free_alloc_context(data_ac);
3906
3907 kfree(tmp_dx_leaf);
3908 return ret;
3909}
3910
Mark Fashehe7c17e42009-01-29 18:17:46 -08003911static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3912 struct buffer_head *di_bh,
3913 struct buffer_head *dx_root_bh,
3914 const char *name, int namelen,
3915 struct ocfs2_dir_lookup_result *lookup)
3916{
3917 int ret, rebalanced = 0;
3918 struct ocfs2_dx_root_block *dx_root;
3919 struct buffer_head *dx_leaf_bh = NULL;
3920 struct ocfs2_dx_leaf *dx_leaf;
3921 u64 blkno;
3922 u32 leaf_cpos;
3923
3924 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3925
3926restart_search:
3927 ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3928 &leaf_cpos, &blkno);
3929 if (ret) {
3930 mlog_errno(ret);
3931 goto out;
3932 }
3933
3934 ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3935 if (ret) {
3936 mlog_errno(ret);
3937 goto out;
3938 }
3939
3940 dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3941
3942 if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3943 le16_to_cpu(dx_leaf->dl_list.de_count)) {
3944 if (rebalanced) {
3945 /*
3946 * Rebalancing should have provided us with
3947 * space in an appropriate leaf.
3948 *
3949 * XXX: Is this an abnormal condition then?
3950 * Should we print a message here?
3951 */
3952 ret = -ENOSPC;
3953 goto out;
3954 }
3955
3956 ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3957 &lookup->dl_hinfo, leaf_cpos,
3958 blkno);
3959 if (ret) {
3960 if (ret != -ENOSPC)
3961 mlog_errno(ret);
3962 goto out;
3963 }
3964
3965 /*
3966 * Restart the lookup. The rebalance might have
3967 * changed which block our item fits into. Mark our
3968 * progress, so we only execute this once.
3969 */
3970 brelse(dx_leaf_bh);
3971 dx_leaf_bh = NULL;
3972 rebalanced = 1;
3973 goto restart_search;
3974 }
3975
3976 lookup->dl_dx_leaf_bh = dx_leaf_bh;
3977 dx_leaf_bh = NULL;
3978
3979out:
3980 brelse(dx_leaf_bh);
3981 return ret;
3982}
3983
3984static int ocfs2_search_dx_free_list(struct inode *dir,
3985 struct buffer_head *dx_root_bh,
3986 int namelen,
3987 struct ocfs2_dir_lookup_result *lookup)
3988{
3989 int ret = -ENOSPC;
3990 struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
3991 struct ocfs2_dir_block_trailer *db;
3992 u64 next_block;
3993 int rec_len = OCFS2_DIR_REC_LEN(namelen);
3994 struct ocfs2_dx_root_block *dx_root;
3995
3996 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3997 next_block = le64_to_cpu(dx_root->dr_free_blk);
3998
3999 while (next_block) {
4000 brelse(prev_leaf_bh);
4001 prev_leaf_bh = leaf_bh;
4002 leaf_bh = NULL;
4003
4004 ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
4005 if (ret) {
4006 mlog_errno(ret);
4007 goto out;
4008 }
4009
4010 db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
4011 if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
4012 lookup->dl_leaf_bh = leaf_bh;
4013 lookup->dl_prev_leaf_bh = prev_leaf_bh;
4014 leaf_bh = NULL;
4015 prev_leaf_bh = NULL;
4016 break;
4017 }
4018
4019 next_block = le64_to_cpu(db->db_free_next);
4020 }
4021
4022 if (!next_block)
4023 ret = -ENOSPC;
4024
4025out:
4026
4027 brelse(leaf_bh);
4028 brelse(prev_leaf_bh);
4029 return ret;
4030}
4031
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004032static int ocfs2_expand_inline_dx_root(struct inode *dir,
4033 struct buffer_head *dx_root_bh)
4034{
4035 int ret, num_dx_leaves, i, j, did_quota = 0;
4036 struct buffer_head **dx_leaves = NULL;
4037 struct ocfs2_extent_tree et;
4038 u64 insert_blkno;
4039 struct ocfs2_alloc_context *data_ac = NULL;
4040 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4041 handle_t *handle = NULL;
4042 struct ocfs2_dx_root_block *dx_root;
4043 struct ocfs2_dx_entry_list *entry_list;
4044 struct ocfs2_dx_entry *dx_entry;
4045 struct ocfs2_dx_leaf *target_leaf;
4046
4047 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4048 if (ret) {
4049 mlog_errno(ret);
4050 goto out;
4051 }
4052
4053 dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4054 if (!dx_leaves) {
4055 ret = -ENOMEM;
4056 mlog_errno(ret);
4057 goto out;
4058 }
4059
4060 handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4061 if (IS_ERR(handle)) {
4062 ret = PTR_ERR(handle);
4063 mlog_errno(ret);
4064 goto out;
4065 }
4066
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004067 ret = dquot_alloc_space_nodirty(dir,
4068 ocfs2_clusters_to_bytes(osb->sb, 1));
4069 if (ret)
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004070 goto out_commit;
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004071 did_quota = 1;
4072
4073 /*
4074 * We do this up front, before the allocation, so that a
4075 * failure to add the dx_root_bh to the journal won't result
4076 * us losing clusters.
4077 */
Joel Becker0cf2f762009-02-12 16:41:25 -08004078 ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004079 OCFS2_JOURNAL_ACCESS_WRITE);
4080 if (ret) {
4081 mlog_errno(ret);
4082 goto out_commit;
4083 }
4084
4085 ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4086 num_dx_leaves, &insert_blkno);
4087 if (ret) {
4088 mlog_errno(ret);
4089 goto out_commit;
4090 }
4091
4092 /*
4093 * Transfer the entries from our dx_root into the appropriate
4094 * block
4095 */
4096 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4097 entry_list = &dx_root->dr_entries;
4098
4099 for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4100 dx_entry = &entry_list->de_entries[i];
4101
4102 j = __ocfs2_dx_dir_hash_idx(osb,
4103 le32_to_cpu(dx_entry->dx_minor_hash));
4104 target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4105
4106 ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4107
4108 /* Each leaf has been passed to the journal already
4109 * via __ocfs2_dx_dir_new_cluster() */
4110 }
4111
4112 dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4113 memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
4114 offsetof(struct ocfs2_dx_root_block, dr_list));
4115 dx_root->dr_list.l_count =
4116 cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4117
4118 /* This should never fail considering we start with an empty
4119 * dx_root. */
Joel Becker5e404e92009-02-13 03:54:22 -08004120 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004121 ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004122 if (ret)
4123 mlog_errno(ret);
4124 did_quota = 0;
4125
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07004126 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004127 ocfs2_journal_dirty(handle, dx_root_bh);
4128
4129out_commit:
4130 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004131 dquot_free_space_nodirty(dir,
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004132 ocfs2_clusters_to_bytes(dir->i_sb, 1));
4133
4134 ocfs2_commit_trans(osb, handle);
4135
4136out:
4137 if (data_ac)
4138 ocfs2_free_alloc_context(data_ac);
4139
4140 if (dx_leaves) {
4141 for (i = 0; i < num_dx_leaves; i++)
4142 brelse(dx_leaves[i]);
4143 kfree(dx_leaves);
4144 }
4145 return ret;
4146}
4147
4148static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4149{
4150 struct ocfs2_dx_root_block *dx_root;
4151 struct ocfs2_dx_entry_list *entry_list;
4152
4153 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4154 entry_list = &dx_root->dr_entries;
4155
4156 if (le16_to_cpu(entry_list->de_num_used) >=
4157 le16_to_cpu(entry_list->de_count))
4158 return -ENOSPC;
4159
4160 return 0;
4161}
4162
Mark Fashehe7c17e42009-01-29 18:17:46 -08004163static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4164 struct buffer_head *di_bh,
4165 const char *name,
4166 int namelen,
4167 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004168{
Mark Fashehe7c17e42009-01-29 18:17:46 -08004169 int ret, free_dx_root = 1;
4170 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004171 struct buffer_head *dx_root_bh = NULL;
Mark Fashehe7c17e42009-01-29 18:17:46 -08004172 struct buffer_head *leaf_bh = NULL;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004173 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fashehe7c17e42009-01-29 18:17:46 -08004174 struct ocfs2_dx_root_block *dx_root;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004175
4176 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4177 if (ret) {
4178 mlog_errno(ret);
4179 goto out;
4180 }
4181
4182 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
Mark Fashehe3a93c22009-02-17 15:29:35 -08004183 if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4184 ret = -ENOSPC;
4185 mlog_errno(ret);
4186 goto out;
4187 }
4188
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004189 if (ocfs2_dx_root_inline(dx_root)) {
4190 ret = ocfs2_inline_dx_has_space(dx_root_bh);
4191
4192 if (ret == 0)
4193 goto search_el;
4194
4195 /*
4196 * We ran out of room in the root block. Expand it to
4197 * an extent, then allow ocfs2_find_dir_space_dx to do
4198 * the rest.
4199 */
4200 ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4201 if (ret) {
4202 mlog_errno(ret);
4203 goto out;
4204 }
4205 }
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004206
Mark Fashehe7c17e42009-01-29 18:17:46 -08004207 /*
4208 * Insert preparation for an indexed directory is split into two
4209 * steps. The call to find_dir_space_dx reserves room in the index for
4210 * an additional item. If we run out of space there, it's a real error
4211 * we can't continue on.
4212 */
4213 ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4214 namelen, lookup);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004215 if (ret) {
4216 mlog_errno(ret);
4217 goto out;
4218 }
4219
Mark Fashehe7c17e42009-01-29 18:17:46 -08004220search_el:
4221 /*
4222 * Next, we need to find space in the unindexed tree. This call
4223 * searches using the free space linked list. If the unindexed tree
4224 * lacks sufficient space, we'll expand it below. The expansion code
4225 * is smart enough to add any new blocks to the free space list.
4226 */
4227 ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4228 if (ret && ret != -ENOSPC) {
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004229 mlog_errno(ret);
4230 goto out;
4231 }
4232
Mark Fashehe7c17e42009-01-29 18:17:46 -08004233 /* Do this up here - ocfs2_extend_dir might need the dx_root */
4234 lookup->dl_dx_root_bh = dx_root_bh;
4235 free_dx_root = 0;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004236
Mark Fashehe7c17e42009-01-29 18:17:46 -08004237 if (ret == -ENOSPC) {
4238 ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004239
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004240 if (ret) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08004241 mlog_errno(ret);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004242 goto out;
4243 }
4244
4245 /*
Mark Fashehe7c17e42009-01-29 18:17:46 -08004246 * We make the assumption here that new leaf blocks are added
4247 * to the front of our free list.
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004248 */
Mark Fashehe7c17e42009-01-29 18:17:46 -08004249 lookup->dl_prev_leaf_bh = NULL;
4250 lookup->dl_leaf_bh = leaf_bh;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004251 }
4252
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004253out:
Mark Fashehe7c17e42009-01-29 18:17:46 -08004254 if (free_dx_root)
4255 brelse(dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004256 return ret;
4257}
4258
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004259/*
4260 * Get a directory ready for insert. Any directory allocation required
4261 * happens here. Success returns zero, and enough context in the dir
4262 * lookup result that ocfs2_add_entry() will be able complete the task
4263 * with minimal performance impact.
4264 */
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004265int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4266 struct inode *dir,
4267 struct buffer_head *parent_fe_bh,
4268 const char *name,
4269 int namelen,
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004270 struct ocfs2_dir_lookup_result *lookup)
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004271{
4272 int ret;
4273 unsigned int blocks_wanted = 1;
4274 struct buffer_head *bh = NULL;
4275
Tao Maf1088d472011-02-23 22:30:23 +08004276 trace_ocfs2_prepare_dir_for_insert(
4277 (unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004278
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004279 if (!namelen) {
4280 ret = -EINVAL;
4281 mlog_errno(ret);
4282 goto out;
4283 }
4284
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004285 /*
4286 * Do this up front to reduce confusion.
4287 *
4288 * The directory might start inline, then be turned into an
4289 * indexed one, in which case we'd need to hash deep inside
4290 * ocfs2_find_dir_space_id(). Since
4291 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4292 * done, there seems no point in spreading out the calls. We
4293 * can optimize away the case where the file system doesn't
4294 * support indexing.
4295 */
4296 if (ocfs2_supports_indexed_dirs(osb))
4297 ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4298
4299 if (ocfs2_dir_indexed(dir)) {
Mark Fashehe7c17e42009-01-29 18:17:46 -08004300 ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4301 name, namelen, lookup);
4302 if (ret)
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004303 mlog_errno(ret);
Mark Fashehe7c17e42009-01-29 18:17:46 -08004304 goto out;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004305 }
4306
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004307 if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4308 ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4309 namelen, &bh, &blocks_wanted);
4310 } else
4311 ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4312
4313 if (ret && ret != -ENOSPC) {
4314 mlog_errno(ret);
4315 goto out;
4316 }
4317
4318 if (ret == -ENOSPC) {
4319 /*
4320 * We have to expand the directory to add this name.
4321 */
4322 BUG_ON(bh);
4323
4324 ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004325 lookup, &bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004326 if (ret) {
4327 if (ret != -ENOSPC)
4328 mlog_errno(ret);
4329 goto out;
4330 }
4331
4332 BUG_ON(!bh);
4333 }
4334
Mark Fasheh4a12ca32008-11-12 15:43:34 -08004335 lookup->dl_leaf_bh = bh;
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004336 bh = NULL;
4337out:
Mark Fasheha81cb882008-10-07 14:25:16 -07004338 brelse(bh);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07004339 return ret;
4340}
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004341
4342static int ocfs2_dx_dir_remove_index(struct inode *dir,
4343 struct buffer_head *di_bh,
4344 struct buffer_head *dx_root_bh)
4345{
4346 int ret;
4347 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4348 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4349 struct ocfs2_dx_root_block *dx_root;
4350 struct inode *dx_alloc_inode = NULL;
4351 struct buffer_head *dx_alloc_bh = NULL;
4352 handle_t *handle;
4353 u64 blk;
4354 u16 bit;
4355 u64 bg_blkno;
4356
4357 dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4358
4359 dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4360 EXTENT_ALLOC_SYSTEM_INODE,
4361 le16_to_cpu(dx_root->dr_suballoc_slot));
4362 if (!dx_alloc_inode) {
4363 ret = -ENOMEM;
4364 mlog_errno(ret);
4365 goto out;
4366 }
4367 mutex_lock(&dx_alloc_inode->i_mutex);
4368
4369 ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4370 if (ret) {
4371 mlog_errno(ret);
4372 goto out_mutex;
4373 }
4374
4375 handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4376 if (IS_ERR(handle)) {
4377 ret = PTR_ERR(handle);
4378 mlog_errno(ret);
4379 goto out_unlock;
4380 }
4381
Joel Becker0cf2f762009-02-12 16:41:25 -08004382 ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004383 OCFS2_JOURNAL_ACCESS_WRITE);
4384 if (ret) {
4385 mlog_errno(ret);
4386 goto out_commit;
4387 }
4388
Tao Ma8ac33dc2010-12-15 16:30:00 +08004389 spin_lock(&OCFS2_I(dir)->ip_lock);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004390 OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4391 di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
Tao Ma8ac33dc2010-12-15 16:30:00 +08004392 spin_unlock(&OCFS2_I(dir)->ip_lock);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004393 di->i_dx_root = cpu_to_le64(0ULL);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07004394 ocfs2_update_inode_fsync_trans(handle, dir, 1);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004395
4396 ocfs2_journal_dirty(handle, di_bh);
4397
4398 blk = le64_to_cpu(dx_root->dr_blkno);
4399 bit = le16_to_cpu(dx_root->dr_suballoc_bit);
Tao Ma74380c42010-03-22 14:20:18 +08004400 if (dx_root->dr_suballoc_loc)
4401 bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4402 else
4403 bg_blkno = ocfs2_which_suballoc_group(blk, bit);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004404 ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4405 bit, bg_blkno, 1);
4406 if (ret)
4407 mlog_errno(ret);
4408
4409out_commit:
4410 ocfs2_commit_trans(osb, handle);
4411
4412out_unlock:
4413 ocfs2_inode_unlock(dx_alloc_inode, 1);
4414
4415out_mutex:
4416 mutex_unlock(&dx_alloc_inode->i_mutex);
4417 brelse(dx_alloc_bh);
4418out:
4419 iput(dx_alloc_inode);
4420 return ret;
4421}
4422
4423int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4424{
4425 int ret;
4426 unsigned int uninitialized_var(clen);
4427 u32 major_hash = UINT_MAX, p_cpos, uninitialized_var(cpos);
4428 u64 uninitialized_var(blkno);
4429 struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4430 struct buffer_head *dx_root_bh = NULL;
4431 struct ocfs2_dx_root_block *dx_root;
4432 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4433 struct ocfs2_cached_dealloc_ctxt dealloc;
4434 struct ocfs2_extent_tree et;
4435
4436 ocfs2_init_dealloc_ctxt(&dealloc);
4437
4438 if (!ocfs2_dir_indexed(dir))
4439 return 0;
4440
4441 ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4442 if (ret) {
4443 mlog_errno(ret);
4444 goto out;
4445 }
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004446 dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4447
4448 if (ocfs2_dx_root_inline(dx_root))
4449 goto remove_index;
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004450
Joel Becker5e404e92009-02-13 03:54:22 -08004451 ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004452
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004453 /* XXX: What if dr_clusters is too large? */
4454 while (le32_to_cpu(dx_root->dr_clusters)) {
4455 ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4456 major_hash, &cpos, &blkno, &clen);
4457 if (ret) {
4458 mlog_errno(ret);
4459 goto out;
4460 }
4461
4462 p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4463
Tristan Ye78f94672010-05-11 17:54:42 +08004464 ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
Junxiao Bif62f12b2014-12-18 16:17:32 -08004465 &dealloc, 0, false);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004466 if (ret) {
4467 mlog_errno(ret);
4468 goto out;
4469 }
4470
4471 if (cpos == 0)
4472 break;
4473
4474 major_hash = cpos - 1;
4475 }
4476
Mark Fasheh4ed8a6b2008-11-24 17:02:08 -08004477remove_index:
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004478 ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4479 if (ret) {
4480 mlog_errno(ret);
4481 goto out;
4482 }
4483
Joel Becker8cb471e2009-02-10 20:00:41 -08004484 ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
Mark Fasheh9b7895e2008-11-12 16:27:44 -08004485out:
4486 ocfs2_schedule_truncate_log_flush(osb, 1);
4487 ocfs2_run_deallocs(osb, &dealloc);
4488
4489 brelse(dx_root_bh);
4490 return ret;
4491}