Blame view

fs/ocfs2/dir.c 113 KB
328970de0   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /* -*- mode: c; c-basic-offset: 8; -*-
   * vim: noexpandtab sw=8 ts=8 sts=0:
   *
   * dir.c
   *
   * Creates, reads, walks and deletes directory-nodes
   *
   * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
   *
   *  Portions of this code from linux/fs/ext3/dir.c
   *
   *  Copyright (C) 1992, 1993, 1994, 1995
   *  Remy Card (card@masi.ibp.fr)
   *  Laboratoire MASI - Institut Blaise pascal
   *  Universite Pierre et Marie Curie (Paris VI)
   *
   *   from
   *
   *   linux/fs/minix/dir.c
   *
762515a8e   Jakub Wilk   ocfs2: fix a typo...
22
   *   Copyright (C) 1991, 1992 Linus Torvalds
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
23
24
25
26
27
28
   */
  
  #include <linux/fs.h>
  #include <linux/types.h>
  #include <linux/slab.h>
  #include <linux/highmem.h>
a90714c15   Jan Kara   ocfs2: Add quota ...
29
  #include <linux/quotaops.h>
9b7895efa   Mark Fasheh   ocfs2: Add a name...
30
  #include <linux/sort.h>
cc56c33e7   Jeff Layton   ocfs2: convert to...
31
  #include <linux/iversion.h>
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
32

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
33
34
35
36
37
  #include <cluster/masklog.h>
  
  #include "ocfs2.h"
  
  #include "alloc.h"
c175a518b   Joel Becker   ocfs2: Checksum a...
38
  #include "blockcheck.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
39
40
41
42
43
44
45
46
  #include "dir.h"
  #include "dlmglue.h"
  #include "extent_map.h"
  #include "file.h"
  #include "inode.h"
  #include "journal.h"
  #include "namei.h"
  #include "suballoc.h"
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
47
  #include "super.h"
9b7895efa   Mark Fasheh   ocfs2: Add a name...
48
  #include "sysfile.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
49
  #include "uptodate.h"
f1088d471   Tao Ma   ocfs2: Remove mlo...
50
  #include "ocfs2_trace.h"
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
51
52
  
  #include "buffer_head_io.h"
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
53
54
55
  #define NAMEI_RA_CHUNKS  2
  #define NAMEI_RA_BLOCKS  4
  #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
56

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
57
58
59
60
61
62
63
  static int ocfs2_do_extend_dir(struct super_block *sb,
  			       handle_t *handle,
  			       struct inode *dir,
  			       struct buffer_head *parent_fe_bh,
  			       struct ocfs2_alloc_context *data_ac,
  			       struct ocfs2_alloc_context *meta_ac,
  			       struct buffer_head **new_bh);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
64
  static int ocfs2_dir_indexed(struct inode *inode);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
65

23193e513   Mark Fasheh   ocfs2: Read suppo...
66
  /*
87d35a74b   Mark Fasheh   ocfs2: Add direct...
67
68
69
   * These are distinct checks because future versions of the file system will
   * want to have a trailing dirent structure independent of indexing.
   */
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
70
  static int ocfs2_supports_dir_trailer(struct inode *dir)
87d35a74b   Mark Fasheh   ocfs2: Add direct...
71
  {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
72
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
73
74
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  		return 0;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
75
  	return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
76
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
77
78
79
80
81
82
83
84
85
  /*
   * "new' here refers to the point at which we're creating a new
   * directory via "mkdir()", but also when we're expanding an inline
   * directory. In either case, we don't yet have the indexing bit set
   * on the directory, so the standard checks will fail in when metaecc
   * is turned off. Only directory-initialization type functions should
   * use this then. Everything else wants ocfs2_supports_dir_trailer()
   */
  static int ocfs2_new_dir_wants_trailer(struct inode *dir)
87d35a74b   Mark Fasheh   ocfs2: Add direct...
86
  {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
87
88
89
90
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  
  	return ocfs2_meta_ecc(osb) ||
  		ocfs2_supports_indexed_dirs(osb);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
91
92
93
94
95
96
97
98
  }
  
  static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
  {
  	return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
  }
  
  #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
c175a518b   Joel Becker   ocfs2: Checksum a...
99
100
101
102
103
104
105
106
107
108
  /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
   * them more consistent? */
  struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
  							    void *data)
  {
  	char *p = data;
  
  	p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
  	return (struct ocfs2_dir_block_trailer *)p;
  }
87d35a74b   Mark Fasheh   ocfs2: Add direct...
109
110
111
112
113
114
115
116
117
118
  /*
   * XXX: This is executed once on every dirent. We should consider optimizing
   * it.
   */
  static int ocfs2_skip_dir_trailer(struct inode *dir,
  				  struct ocfs2_dir_entry *de,
  				  unsigned long offset,
  				  unsigned long blklen)
  {
  	unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
119
  	if (!ocfs2_supports_dir_trailer(dir))
87d35a74b   Mark Fasheh   ocfs2: Add direct...
120
121
122
123
124
125
126
127
128
  		return 0;
  
  	if (offset != toff)
  		return 0;
  
  	return 1;
  }
  
  static void ocfs2_init_dir_trailer(struct inode *inode,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
129
  				   struct buffer_head *bh, u16 rec_len)
87d35a74b   Mark Fasheh   ocfs2: Add direct...
130
131
132
133
134
135
136
137
138
  {
  	struct ocfs2_dir_block_trailer *trailer;
  
  	trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
  	strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
  	trailer->db_compat_rec_len =
  			cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
  	trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  	trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  	trailer->db_free_rec_len = cpu_to_le16(rec_len);
  }
  /*
   * Link an unindexed block with a dir trailer structure into the index free
   * list. This function will modify dirdata_bh, but assumes you've already
   * passed it to the journal.
   */
  static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
  				     struct buffer_head *dx_root_bh,
  				     struct buffer_head *dirdata_bh)
  {
  	int ret;
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dir_block_trailer *trailer;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
153
  	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  	trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  
  	trailer->db_free_next = dx_root->dr_free_blk;
  	dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  
  	ocfs2_journal_dirty(handle, dx_root_bh);
  
  out:
  	return ret;
  }
  
  static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
  {
  	return res->dl_prev_leaf_bh == NULL;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
174
  }
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
175
176
  void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
  {
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
177
  	brelse(res->dl_dx_root_bh);
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
178
  	brelse(res->dl_leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
179
  	brelse(res->dl_dx_leaf_bh);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
180
  	brelse(res->dl_prev_leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
181
182
183
184
185
186
187
188
  }
  
  static int ocfs2_dir_indexed(struct inode *inode)
  {
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
  		return 1;
  	return 0;
  }
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
189
190
191
192
  static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
  {
  	return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  /*
   * Hashing code adapted from ext3
   */
  #define DELTA 0x9E3779B9
  
  static void TEA_transform(__u32 buf[4], __u32 const in[])
  {
  	__u32	sum = 0;
  	__u32	b0 = buf[0], b1 = buf[1];
  	__u32	a = in[0], b = in[1], c = in[2], d = in[3];
  	int	n = 16;
  
  	do {
  		sum += DELTA;
  		b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  		b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  	} while (--n);
  
  	buf[0] += b0;
  	buf[1] += b1;
  }
  
  static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
  {
  	__u32	pad, val;
  	int	i;
  
  	pad = (__u32)len | ((__u32)len << 8);
  	pad |= pad << 16;
  
  	val = pad;
  	if (len > num*4)
  		len = num * 4;
  	for (i = 0; i < len; i++) {
  		if ((i % 4) == 0)
  			val = pad;
  		val = msg[i] + (val << 8);
  		if ((i % 4) == 3) {
  			*buf++ = val;
  			val = pad;
  			num--;
  		}
  	}
  	if (--num >= 0)
  		*buf++ = val;
  	while (--num >= 0)
  		*buf++ = pad;
  }
  
  static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
  				   struct ocfs2_dx_hinfo *hinfo)
  {
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  	const char	*p;
  	__u32		in[8], buf[4];
  
  	/*
  	 * XXX: Is this really necessary, if the index is never looked
  	 * at by readdir? Is a hash value of '0' a bad idea?
  	 */
  	if ((len == 1 && !strncmp(".", name, 1)) ||
  	    (len == 2 && !strncmp("..", name, 2))) {
  		buf[0] = buf[1] = 0;
  		goto out;
  	}
  
  #ifdef OCFS2_DEBUG_DX_DIRS
  	/*
  	 * This makes it very easy to debug indexing problems. We
  	 * should never allow this to be selected without hand editing
  	 * this file though.
  	 */
  	buf[0] = buf[1] = len;
  	goto out;
  #endif
  
  	memcpy(buf, osb->osb_dx_seed, sizeof(buf));
  
  	p = name;
  	while (len > 0) {
  		str2hashbuf(p, len, in, 4);
  		TEA_transform(buf, in);
  		len -= 16;
  		p += 16;
  	}
  
  out:
  	hinfo->major_hash = buf[0];
  	hinfo->minor_hash = buf[1];
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
282
  }
87d35a74b   Mark Fasheh   ocfs2: Add direct...
283
  /*
23193e513   Mark Fasheh   ocfs2: Read suppo...
284
285
286
   * bh passed here can be an inode block or a dir data block, depending
   * on the inode inline data flag.
   */
5eae5b96f   Mark Fasheh   ocfs2: Remove ope...
287
288
289
290
  static int ocfs2_check_dir_entry(struct inode * dir,
  				 struct ocfs2_dir_entry * de,
  				 struct buffer_head * bh,
  				 unsigned long offset)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
291
292
293
  {
  	const char *error_msg = NULL;
  	const int rlen = le16_to_cpu(de->rec_len);
1dd9ffc82   Tao Ma   ocfs2: optimize o...
294
  	if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
295
  		error_msg = "rec_len is smaller than minimal";
1dd9ffc82   Tao Ma   ocfs2: optimize o...
296
  	else if (unlikely(rlen % 4 != 0))
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
297
  		error_msg = "rec_len % 4 != 0";
1dd9ffc82   Tao Ma   ocfs2: optimize o...
298
  	else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
299
  		error_msg = "rec_len is too small for name_len";
1dd9ffc82   Tao Ma   ocfs2: optimize o...
300
301
  	else if (unlikely(
  		 ((char *) de - bh->b_data) + rlen > dir->i_sb->s_blocksize))
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
302
  		error_msg = "directory entry across blocks";
1dd9ffc82   Tao Ma   ocfs2: optimize o...
303
  	if (unlikely(error_msg != NULL))
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
304
305
306
307
308
309
  		mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
  		     "offset=%lu, inode=%llu, rec_len=%d, name_len=%d
  ",
  		     (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
  		     offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
  		     de->name_len);
1dd9ffc82   Tao Ma   ocfs2: optimize o...
310

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  	return error_msg == NULL ? 1 : 0;
  }
  
  static inline int ocfs2_match(int len,
  			      const char * const name,
  			      struct ocfs2_dir_entry *de)
  {
  	if (len != de->name_len)
  		return 0;
  	if (!de->inode)
  		return 0;
  	return !memcmp(name, de->name, len);
  }
  
  /*
   * Returns 0 if not found, -1 on failure, and 1 on success
   */
42b16b3fb   Jesper Juhl   Kill off warning:...
328
  static inline int ocfs2_search_dirblock(struct buffer_head *bh,
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
329
330
331
  					struct inode *dir,
  					const char *name, int namelen,
  					unsigned long offset,
23193e513   Mark Fasheh   ocfs2: Read suppo...
332
333
  					char *first_de,
  					unsigned int bytes,
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
334
335
336
337
338
339
  					struct ocfs2_dir_entry **res_dir)
  {
  	struct ocfs2_dir_entry *de;
  	char *dlimit, *de_buf;
  	int de_len;
  	int ret = 0;
23193e513   Mark Fasheh   ocfs2: Read suppo...
340
341
  	de_buf = first_de;
  	dlimit = de_buf + bytes;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
  
  	while (de_buf < dlimit) {
  		/* this code is executed quadratically often */
  		/* do minimal checking `by hand' */
  
  		de = (struct ocfs2_dir_entry *) de_buf;
  
  		if (de_buf + namelen <= dlimit &&
  		    ocfs2_match(namelen, name, de)) {
  			/* found a match - just to be sure, do a full check */
  			if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  				ret = -1;
  				goto bail;
  			}
  			*res_dir = de;
  			ret = 1;
  			goto bail;
  		}
  
  		/* prevent looping on a bad block */
  		de_len = le16_to_cpu(de->rec_len);
  		if (de_len <= 0) {
  			ret = -1;
  			goto bail;
  		}
  
  		de_buf += de_len;
  		offset += de_len;
  	}
  
  bail:
f1088d471   Tao Ma   ocfs2: Remove mlo...
373
  	trace_ocfs2_search_dirblock(ret);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
374
375
  	return ret;
  }
23193e513   Mark Fasheh   ocfs2: Read suppo...
376
377
378
379
380
381
382
383
384
  static struct buffer_head *ocfs2_find_entry_id(const char *name,
  					       int namelen,
  					       struct inode *dir,
  					       struct ocfs2_dir_entry **res_dir)
  {
  	int ret, found;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_dinode *di;
  	struct ocfs2_inline_data *data;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
385
  	ret = ocfs2_read_inode_block(dir, &di_bh);
23193e513   Mark Fasheh   ocfs2: Read suppo...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	di = (struct ocfs2_dinode *)di_bh->b_data;
  	data = &di->id2.i_data;
  
  	found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
  				      data->id_data, i_size_read(dir), res_dir);
  	if (found == 1)
  		return di_bh;
  
  	brelse(di_bh);
  out:
  	return NULL;
  }
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
403
404
405
  static int ocfs2_validate_dir_block(struct super_block *sb,
  				    struct buffer_head *bh)
  {
c175a518b   Joel Becker   ocfs2: Checksum a...
406
407
408
  	int rc;
  	struct ocfs2_dir_block_trailer *trailer =
  		ocfs2_trailer_from_bh(bh, sb);
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
409
  	/*
c175a518b   Joel Becker   ocfs2: Checksum a...
410
  	 * We don't validate dirents here, that's handled
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
411
412
  	 * in-place when the code walks them.
  	 */
f1088d471   Tao Ma   ocfs2: Remove mlo...
413
  	trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
414

c175a518b   Joel Becker   ocfs2: Checksum a...
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  	BUG_ON(!buffer_uptodate(bh));
  
  	/*
  	 * If the ecc fails, we return the error but otherwise
  	 * leave the filesystem running.  We know any error is
  	 * local to this block.
  	 *
  	 * Note that we are safe to call this even if the directory
  	 * doesn't have a trailer.  Filesystems without metaecc will do
  	 * nothing, and filesystems with it will have one.
  	 */
  	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
  	if (rc)
  		mlog(ML_ERROR, "Checksum failed for dinode %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
  
  	return rc;
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
433
434
435
  }
  
  /*
9b7895efa   Mark Fasheh   ocfs2: Add a name...
436
437
438
439
440
441
442
443
444
445
446
447
   * Validate a directory trailer.
   *
   * We check the trailer here rather than in ocfs2_validate_dir_block()
   * because that function doesn't have the inode to test.
   */
  static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
  {
  	int rc = 0;
  	struct ocfs2_dir_block_trailer *trailer;
  
  	trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
  	if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
448
  		rc = ocfs2_error(dir->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
449
450
451
452
  				 "Invalid dirblock #%llu: signature = %.*s
  ",
  				 (unsigned long long)bh->b_blocknr, 7,
  				 trailer->db_signature);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
453
454
455
  		goto out;
  	}
  	if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
456
  		rc = ocfs2_error(dir->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
457
458
459
460
  				 "Directory block #%llu has an invalid db_blkno of %llu
  ",
  				 (unsigned long long)bh->b_blocknr,
  				 (unsigned long long)le64_to_cpu(trailer->db_blkno));
9b7895efa   Mark Fasheh   ocfs2: Add a name...
461
462
463
464
  		goto out;
  	}
  	if (le64_to_cpu(trailer->db_parent_dinode) !=
  	    OCFS2_I(dir)->ip_blkno) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
465
  		rc = ocfs2_error(dir->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
466
467
468
469
470
  				 "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu
  ",
  				 (unsigned long long)bh->b_blocknr,
  				 (unsigned long long)OCFS2_I(dir)->ip_blkno,
  				 (unsigned long long)le64_to_cpu(trailer->db_blkno));
9b7895efa   Mark Fasheh   ocfs2: Add a name...
471
472
473
474
475
476
477
  		goto out;
  	}
  out:
  	return rc;
  }
  
  /*
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
478
479
480
481
482
483
484
485
486
487
   * This function forces all errors to -EIO for consistency with its
   * predecessor, ocfs2_bread().  We haven't audited what returning the
   * real error codes would do to callers.  We log the real codes with
   * mlog_errno() before we squash them.
   */
  static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
  				struct buffer_head **bh, int flags)
  {
  	int rc = 0;
  	struct buffer_head *tmp = *bh;
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
488

511308d90   Joel Becker   ocfs2: Convert oc...
489
490
  	rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
  				    ocfs2_validate_dir_block);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
491
  	if (rc) {
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
492
  		mlog_errno(rc);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
493
494
  		goto out;
  	}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
495
  	if (!(flags & OCFS2_BH_READAHEAD) &&
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
496
  	    ocfs2_supports_dir_trailer(inode)) {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
497
498
499
500
501
  		rc = ocfs2_check_dir_trailer(inode, tmp);
  		if (rc) {
  			if (!*bh)
  				brelse(tmp);
  			mlog_errno(rc);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
502
503
504
  			goto out;
  		}
  	}
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
505

511308d90   Joel Becker   ocfs2: Convert oc...
506
  	/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
87d35a74b   Mark Fasheh   ocfs2: Add direct...
507
  	if (!*bh)
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
508
  		*bh = tmp;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
509
  out:
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
510
511
  	return rc ? -EIO : 0;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
512
513
514
515
516
517
518
519
520
521
  /*
   * Read the block at 'phys' which belongs to this directory
   * inode. This function does no virtual->physical block translation -
   * what's passed in is assumed to be a valid directory block.
   */
  static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
  				       struct buffer_head **bh)
  {
  	int ret;
  	struct buffer_head *tmp = *bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
522
523
  	ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
  			       ocfs2_validate_dir_block);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	if (ocfs2_supports_dir_trailer(dir)) {
  		ret = ocfs2_check_dir_trailer(dir, tmp);
  		if (ret) {
  			if (!*bh)
  				brelse(tmp);
  			mlog_errno(ret);
  			goto out;
  		}
  	}
  
  	if (!ret && !*bh)
  		*bh = tmp;
  out:
  	return ret;
  }
  
  static int ocfs2_validate_dx_root(struct super_block *sb,
  				  struct buffer_head *bh)
  {
  	int ret;
  	struct ocfs2_dx_root_block *dx_root;
  
  	BUG_ON(!buffer_uptodate(bh));
  
  	dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
  
  	ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
  	if (ret) {
  		mlog(ML_ERROR,
  		     "Checksum failed for dir index root block %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
  		return ret;
  	}
  
  	if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
565
  		ret = ocfs2_error(sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
566
567
568
569
  				  "Dir Index Root # %llu has bad signature %.*s
  ",
  				  (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
  				  7, dx_root->dr_signature);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
570
  	}
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
571
  	return ret;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
572
573
574
575
576
577
578
579
  }
  
  static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
  			      struct buffer_head **dx_root_bh)
  {
  	int ret;
  	u64 blkno = le64_to_cpu(di->i_dx_root);
  	struct buffer_head *tmp = *dx_root_bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
580
581
  	ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  			       ocfs2_validate_dx_root);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
  
  	/* If ocfs2_read_block() got us a new bh, pass it up. */
  	if (!ret && !*dx_root_bh)
  		*dx_root_bh = tmp;
  
  	return ret;
  }
  
  static int ocfs2_validate_dx_leaf(struct super_block *sb,
  				  struct buffer_head *bh)
  {
  	int ret;
  	struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
  
  	BUG_ON(!buffer_uptodate(bh));
  
  	ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
  	if (ret) {
  		mlog(ML_ERROR,
  		     "Checksum failed for dir index leaf block %llu
  ",
  		     (unsigned long long)bh->b_blocknr);
  		return ret;
  	}
  
  	if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
608
609
610
  		ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s
  ",
  				  7, dx_leaf->dl_signature);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
611
  	}
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
612
  	return ret;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
613
614
615
616
617
618
619
  }
  
  static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
  			      struct buffer_head **dx_leaf_bh)
  {
  	int ret;
  	struct buffer_head *tmp = *dx_leaf_bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
620
621
  	ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
  			       ocfs2_validate_dx_leaf);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  
  	/* If ocfs2_read_block() got us a new bh, pass it up. */
  	if (!ret && !*dx_leaf_bh)
  		*dx_leaf_bh = tmp;
  
  	return ret;
  }
  
  /*
   * Read a series of dx_leaf blocks. This expects all buffer_head
   * pointers to be NULL on function entry.
   */
  static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
  				struct buffer_head **dx_leaf_bhs)
  {
  	int ret;
8cb471e8f   Joel Becker   ocfs2: Take the i...
638
  	ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
639
640
641
642
643
644
  				ocfs2_validate_dx_leaf);
  	if (ret)
  		mlog_errno(ret);
  
  	return ret;
  }
0af4bd388   Adrian Bunk   [2.6 patch] make ...
645
646
647
  static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
  					       struct inode *dir,
  					       struct ocfs2_dir_entry **res_dir)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
648
649
650
651
652
653
654
655
656
657
  {
  	struct super_block *sb;
  	struct buffer_head *bh_use[NAMEI_RA_SIZE];
  	struct buffer_head *bh, *ret = NULL;
  	unsigned long start, block, b;
  	int ra_max = 0;		/* Number of bh's in the readahead
  				   buffer, bh_use[] */
  	int ra_ptr = 0;		/* Current index into readahead
  				   buffer */
  	int num = 0;
1a5692e47   Alex Shi   ocfs2: remove use...
658
  	int nblocks, i;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
659

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
  	sb = dir->i_sb;
  
  	nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  	start = OCFS2_I(dir)->ip_dir_start_lookup;
  	if (start >= nblocks)
  		start = 0;
  	block = start;
  
  restart:
  	do {
  		/*
  		 * We deal with the read-ahead logic here.
  		 */
  		if (ra_ptr >= ra_max) {
  			/* Refill the readahead buffer */
  			ra_ptr = 0;
  			b = block;
  			for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
  				/*
  				 * Terminate if we reach the end of the
  				 * directory and must wrap, or if our
  				 * search has finished at this block.
  				 */
  				if (b >= nblocks || (num && block == start)) {
  					bh_use[ra_max] = NULL;
  					break;
  				}
  				num++;
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
688
  				bh = NULL;
1a5692e47   Alex Shi   ocfs2: remove use...
689
  				ocfs2_read_dir_block(dir, b++, &bh,
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
690
  							   OCFS2_BH_READAHEAD);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
691
692
693
694
695
  				bh_use[ra_max] = bh;
  			}
  		}
  		if ((bh = bh_use[ra_ptr++]) == NULL)
  			goto next;
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
696
  		if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
5e0b3dec0   Joel Becker   ocfs2: Kill the l...
697
  			/* read error, skip block & hope for the best.
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
698
  			 * ocfs2_read_dir_block() has released the bh. */
61fb9ea4b   jiangyiwen   ocfs2: do not set...
699
  			mlog(ML_ERROR, "reading directory %llu, "
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
700
701
702
703
  				    "offset %lu
  ",
  				    (unsigned long long)OCFS2_I(dir)->ip_blkno,
  				    block);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
704
705
706
707
  			goto next;
  		}
  		i = ocfs2_search_dirblock(bh, dir, name, namelen,
  					  block << sb->s_blocksize_bits,
23193e513   Mark Fasheh   ocfs2: Read suppo...
708
  					  bh->b_data, sb->s_blocksize,
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
  					  res_dir);
  		if (i == 1) {
  			OCFS2_I(dir)->ip_dir_start_lookup = block;
  			ret = bh;
  			goto cleanup_and_exit;
  		} else {
  			brelse(bh);
  			if (i < 0)
  				goto cleanup_and_exit;
  		}
  	next:
  		if (++block >= nblocks)
  			block = 0;
  	} while (block != start);
  
  	/*
  	 * If the directory has grown while we were searching, then
  	 * search the last part of the directory before giving up.
  	 */
  	block = nblocks;
  	nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
  	if (block < nblocks) {
  		start = 0;
  		goto restart;
  	}
  
  cleanup_and_exit:
  	/* Clean up the read-ahead blocks */
  	for (; ra_ptr < ra_max; ra_ptr++)
  		brelse(bh_use[ra_ptr]);
f1088d471   Tao Ma   ocfs2: Remove mlo...
739
  	trace_ocfs2_find_entry_el(ret);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
740
741
  	return ret;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
742
743
744
745
746
747
  static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
  				   struct ocfs2_extent_list *el,
  				   u32 major_hash,
  				   u32 *ret_cpos,
  				   u64 *ret_phys_blkno,
  				   unsigned int *ret_clen)
23193e513   Mark Fasheh   ocfs2: Read suppo...
748
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
749
750
751
752
  	int ret = 0, i, found;
  	struct buffer_head *eb_bh = NULL;
  	struct ocfs2_extent_block *eb;
  	struct ocfs2_extent_rec *rec = NULL;
23193e513   Mark Fasheh   ocfs2: Read suppo...
753

9b7895efa   Mark Fasheh   ocfs2: Add a name...
754
  	if (el->l_tree_depth) {
facdb77f5   Joel Becker   ocfs2: ocfs2_find...
755
756
  		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
  				      &eb_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
757
758
759
760
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
23193e513   Mark Fasheh   ocfs2: Read suppo...
761

9b7895efa   Mark Fasheh   ocfs2: Add a name...
762
763
  		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
  		el = &eb->h_list;
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
764

9b7895efa   Mark Fasheh   ocfs2: Add a name...
765
  		if (el->l_tree_depth) {
17a5b9ab3   Goldwyn Rodrigues   ocfs2: acknowledg...
766
  			ret = ocfs2_error(inode->i_sb,
7ecef14ab   Joe Perches   ocfs2: neaten do_...
767
768
769
770
  					  "Inode %lu has non zero tree depth in btree tree block %llu
  ",
  					  inode->i_ino,
  					  (unsigned long long)eb_bh->b_blocknr);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
  			goto out;
  		}
  	}
  
  	found = 0;
  	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  		rec = &el->l_recs[i];
  
  		if (le32_to_cpu(rec->e_cpos) <= major_hash) {
  			found = 1;
  			break;
  		}
  	}
  
  	if (!found) {
7ecef14ab   Joe Perches   ocfs2: neaten do_...
786
787
788
789
790
791
  		ret = ocfs2_error(inode->i_sb,
  				  "Inode %lu has bad extent record (%u, %u, 0) in btree
  ",
  				  inode->i_ino,
  				  le32_to_cpu(rec->e_cpos),
  				  ocfs2_rec_clusters(el, rec));
9b7895efa   Mark Fasheh   ocfs2: Add a name...
792
793
794
795
796
797
798
799
800
801
802
803
804
  		goto out;
  	}
  
  	if (ret_phys_blkno)
  		*ret_phys_blkno = le64_to_cpu(rec->e_blkno);
  	if (ret_cpos)
  		*ret_cpos = le32_to_cpu(rec->e_cpos);
  	if (ret_clen)
  		*ret_clen = le16_to_cpu(rec->e_leaf_clusters);
  
  out:
  	brelse(eb_bh);
  	return ret;
23193e513   Mark Fasheh   ocfs2: Read suppo...
805
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
806
  /*
9b7895efa   Mark Fasheh   ocfs2: Add a name...
807
808
   * Returns the block index, from the start of the cluster which this
   * hash belongs too.
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
809
   */
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
810
811
  static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  						   u32 minor_hash)
38760e243   Mark Fasheh   ocfs2: Rename cle...
812
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
813
814
  	return minor_hash & osb->osb_dx_mask;
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
815

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
816
817
818
819
820
  static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
  					  struct ocfs2_dx_hinfo *hinfo)
  {
  	return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
821
822
823
824
825
826
827
  static int ocfs2_dx_dir_lookup(struct inode *inode,
  			       struct ocfs2_extent_list *el,
  			       struct ocfs2_dx_hinfo *hinfo,
  			       u32 *ret_cpos,
  			       u64 *ret_phys_blkno)
  {
  	int ret = 0;
3f649ab72   Kees Cook   treewide: Remove ...
828
829
830
  	unsigned int cend, clen;
  	u32 cpos;
  	u64 blkno;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
831
  	u32 name_hash = hinfo->major_hash;
13723d00e   Joel Becker   ocfs2: Use metada...
832

9b7895efa   Mark Fasheh   ocfs2: Add a name...
833
834
  	ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
  				      &clen);
38760e243   Mark Fasheh   ocfs2: Rename cle...
835
836
837
838
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
839
840
841
842
843
844
845
846
847
848
  	cend = cpos + clen;
  	if (name_hash >= cend) {
  		/* We want the last cluster */
  		blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
  		cpos += clen - 1;
  	} else {
  		blkno += ocfs2_clusters_to_blocks(inode->i_sb,
  						  name_hash - cpos);
  		cpos = name_hash;
  	}
38760e243   Mark Fasheh   ocfs2: Rename cle...
849

9b7895efa   Mark Fasheh   ocfs2: Add a name...
850
851
852
853
854
855
856
857
858
859
860
  	/*
  	 * We now have the cluster which should hold our entry. To
  	 * find the exact block from the start of the cluster to
  	 * search, we take the lower bits of the hash.
  	 */
  	blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
  
  	if (ret_phys_blkno)
  		*ret_phys_blkno = blkno;
  	if (ret_cpos)
  		*ret_cpos = cpos;
38760e243   Mark Fasheh   ocfs2: Rename cle...
861
862
  
  out:
9b7895efa   Mark Fasheh   ocfs2: Add a name...
863

38760e243   Mark Fasheh   ocfs2: Rename cle...
864
865
  	return ret;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
866
867
  static int ocfs2_dx_dir_search(const char *name, int namelen,
  			       struct inode *dir,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
868
  			       struct ocfs2_dx_root_block *dx_root,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
869
  			       struct ocfs2_dir_lookup_result *res)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
870
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
871
  	int ret, i, found;
3f649ab72   Kees Cook   treewide: Remove ...
872
  	u64 phys;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
873
874
875
876
877
878
  	struct buffer_head *dx_leaf_bh = NULL;
  	struct ocfs2_dx_leaf *dx_leaf;
  	struct ocfs2_dx_entry *dx_entry = NULL;
  	struct buffer_head *dir_ent_bh = NULL;
  	struct ocfs2_dir_entry *dir_ent = NULL;
  	struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
879
880
  	struct ocfs2_extent_list *dr_el;
  	struct ocfs2_dx_entry_list *entry_list;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
881
882
  
  	ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
883
884
885
886
887
888
  	if (ocfs2_dx_root_inline(dx_root)) {
  		entry_list = &dx_root->dr_entries;
  		goto search;
  	}
  
  	dr_el = &dx_root->dr_list;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
889
890
891
892
893
  	ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
894

f1088d471   Tao Ma   ocfs2: Remove mlo...
895
896
897
  	trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
  				  namelen, name, hinfo->major_hash,
  				  hinfo->minor_hash, (unsigned long long)phys);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
898

9b7895efa   Mark Fasheh   ocfs2: Add a name...
899
900
901
902
903
  	ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
13723d00e   Joel Becker   ocfs2: Use metada...
904

9b7895efa   Mark Fasheh   ocfs2: Add a name...
905
  	dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
f1088d471   Tao Ma   ocfs2: Remove mlo...
906
907
908
  	trace_ocfs2_dx_dir_search_leaf_info(
  			le16_to_cpu(dx_leaf->dl_list.de_num_used),
  			le16_to_cpu(dx_leaf->dl_list.de_count));
9b7895efa   Mark Fasheh   ocfs2: Add a name...
909

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
910
911
912
  	entry_list = &dx_leaf->dl_list;
  
  search:
9b7895efa   Mark Fasheh   ocfs2: Add a name...
913
914
915
916
  	/*
  	 * Empty leaf is legal, so no need to check for that.
  	 */
  	found = 0;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
917
918
  	for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  		dx_entry = &entry_list->de_entries[i];
9b7895efa   Mark Fasheh   ocfs2: Add a name...
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
  
  		if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
  		    || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
  			continue;
  
  		/*
  		 * Search unindexed leaf block now. We're not
  		 * guaranteed to find anything.
  		 */
  		ret = ocfs2_read_dir_block_direct(dir,
  					  le64_to_cpu(dx_entry->dx_dirent_blk),
  					  &dir_ent_bh);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
934
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  
  		/*
  		 * XXX: We should check the unindexed block here,
  		 * before using it.
  		 */
  
  		found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
  					      0, dir_ent_bh->b_data,
  					      dir->i_sb->s_blocksize, &dir_ent);
  		if (found == 1)
  			break;
  
  		if (found == -1) {
  			/* This means we found a bad directory entry. */
  			ret = -EIO;
  			mlog_errno(ret);
  			goto out;
  		}
  
  		brelse(dir_ent_bh);
  		dir_ent_bh = NULL;
  	}
  
  	if (found <= 0) {
  		ret = -ENOENT;
  		goto out;
  	}
  
  	res->dl_leaf_bh = dir_ent_bh;
  	res->dl_entry = dir_ent;
  	res->dl_dx_leaf_bh = dx_leaf_bh;
  	res->dl_dx_entry = dx_entry;
  
  	ret = 0;
  out:
  	if (ret) {
  		brelse(dx_leaf_bh);
  		brelse(dir_ent_bh);
  	}
  	return ret;
  }
  
  static int ocfs2_find_entry_dx(const char *name, int namelen,
  			       struct inode *dir,
  			       struct ocfs2_dir_lookup_result *lookup)
  {
  	int ret;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_dinode *di;
  	struct buffer_head *dx_root_bh = NULL;
  	struct ocfs2_dx_root_block *dx_root;
  
  	ret = ocfs2_read_inode_block(dir, &di_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	di = (struct ocfs2_dinode *)di_bh->b_data;
  
  	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1001
  	ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1002
1003
1004
1005
1006
  	if (ret) {
  		if (ret != -ENOENT)
  			mlog_errno(ret);
  		goto out;
  	}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1007
1008
  	lookup->dl_dx_root_bh = dx_root_bh;
  	dx_root_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
  out:
  	brelse(di_bh);
  	brelse(dx_root_bh);
  	return ret;
  }
  
  /*
   * Try to find an entry of the provided name within 'dir'.
   *
   * If nothing was found, -ENOENT is returned. Otherwise, zero is
   * returned and the struct 'res' will contain information useful to
   * other directory manipulation functions.
   *
   * Caller can NOT assume anything about the contents of the
   * buffer_heads - they are passed back only so that it can be passed
   * into any one of the manipulation functions (add entry, delete
   * entry, etc). As an example, bh in the extent directory case is a
   * data block, in the inline-data case it actually points to an inode,
   * in the indexed directory case, multiple buffers are involved.
   */
  int ocfs2_find_entry(const char *name, int namelen,
  		     struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
  {
  	struct buffer_head *bh;
  	struct ocfs2_dir_entry *res_dir = NULL;
  
  	if (ocfs2_dir_indexed(dir))
  		return ocfs2_find_entry_dx(name, namelen, dir, lookup);
  
  	/*
  	 * The unindexed dir code only uses part of the lookup
  	 * structure, so there's no reason to push it down further
  	 * than this.
  	 */
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  		bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
  	else
  		bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
  
  	if (bh == NULL)
  		return -ENOENT;
  
  	lookup->dl_leaf_bh = bh;
  	lookup->dl_entry = res_dir;
  	return 0;
  }
  
  /*
   * Update inode number and type of a previously found directory entry.
   */
  int ocfs2_update_entry(struct inode *dir, handle_t *handle,
  		       struct ocfs2_dir_lookup_result *res,
  		       struct inode *new_entry_inode)
  {
  	int ret;
  	ocfs2_journal_access_func access = ocfs2_journal_access_db;
  	struct ocfs2_dir_entry *de = res->dl_entry;
  	struct buffer_head *de_bh = res->dl_leaf_bh;
  
  	/*
  	 * The same code works fine for both inline-data and extent
  	 * based directories, so no need to split this up.  The only
  	 * difference is the journal_access function.
  	 */
  
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  		access = ocfs2_journal_access_di;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1076
1077
  	ret = access(handle, INODE_CACHE(dir), de_bh,
  		     OCFS2_JOURNAL_ACCESS_WRITE);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
  	ocfs2_set_de_type(de, new_entry_inode->i_mode);
  
  	ocfs2_journal_dirty(handle, de_bh);
  
  out:
  	return ret;
  }
  
  /*
   * __ocfs2_delete_entry deletes a directory entry by merging it with the
   * previous entry
   */
  static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
  				struct ocfs2_dir_entry *de_del,
  				struct buffer_head *bh, char *first_de,
  				unsigned int bytes)
  {
  	struct ocfs2_dir_entry *de, *pde;
  	int i, status = -ENOENT;
  	ocfs2_journal_access_func access = ocfs2_journal_access_db;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  		access = ocfs2_journal_access_di;
  
  	i = 0;
  	pde = NULL;
  	de = (struct ocfs2_dir_entry *) first_de;
  	while (i < bytes) {
  		if (!ocfs2_check_dir_entry(dir, de, bh, i)) {
  			status = -EIO;
  			mlog_errno(status);
  			goto bail;
  		}
  		if (de == de_del)  {
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1117
  			status = access(handle, INODE_CACHE(dir), bh,
13723d00e   Joel Becker   ocfs2: Use metada...
1118
  					OCFS2_JOURNAL_ACCESS_WRITE);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1119
1120
1121
1122
1123
1124
  			if (status < 0) {
  				status = -EIO;
  				mlog_errno(status);
  				goto bail;
  			}
  			if (pde)
0dd3256e0   Marcin Slusarz   [PATCH] ocfs2: le...
1125
1126
  				le16_add_cpu(&pde->rec_len,
  						le16_to_cpu(de->rec_len));
829852480   Wengang Wang   ocfs2: make diren...
1127
  			de->inode = 0;
cc56c33e7   Jeff Layton   ocfs2: convert to...
1128
  			inode_inc_iversion(dir);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1129
  			ocfs2_journal_dirty(handle, bh);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1130
1131
1132
1133
1134
1135
1136
  			goto bail;
  		}
  		i += le16_to_cpu(de->rec_len);
  		pde = de;
  		de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
  	}
  bail:
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1137
1138
  	return status;
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
  {
  	unsigned int hole;
  
  	if (le64_to_cpu(de->inode) == 0)
  		hole = le16_to_cpu(de->rec_len);
  	else
  		hole = le16_to_cpu(de->rec_len) -
  			OCFS2_DIR_REC_LEN(de->name_len);
  
  	return hole;
  }
  
  static int ocfs2_find_max_rec_len(struct super_block *sb,
  				  struct buffer_head *dirblock_bh)
  {
  	int size, this_hole, largest_hole = 0;
  	char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
  	struct ocfs2_dir_entry *de;
  
  	trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
  	size = ocfs2_dir_trailer_blk_off(sb);
  	limit = start + size;
  	de_buf = start;
  	de = (struct ocfs2_dir_entry *)de_buf;
  	do {
  		if (de_buf != trailer) {
  			this_hole = ocfs2_figure_dirent_hole(de);
  			if (this_hole > largest_hole)
  				largest_hole = this_hole;
  		}
  
  		de_buf += le16_to_cpu(de->rec_len);
  		de = (struct ocfs2_dir_entry *)de_buf;
  	} while (de_buf < limit);
  
  	if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  		return largest_hole;
  	return 0;
  }
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1179
1180
  static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
  				       int index)
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1181
  {
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1182
  	int num_used = le16_to_cpu(entry_list->de_num_used);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1183
1184
1185
  
  	if (num_used == 1 || index == (num_used - 1))
  		goto clear;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1186
1187
  	memmove(&entry_list->de_entries[index],
  		&entry_list->de_entries[index + 1],
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1188
1189
1190
  		(num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
  clear:
  	num_used--;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1191
  	memset(&entry_list->de_entries[num_used], 0,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1192
  	       sizeof(struct ocfs2_dx_entry));
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1193
  	entry_list->de_num_used = cpu_to_le16(num_used);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1194
1195
1196
1197
1198
  }
  
  static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
  				 struct ocfs2_dir_lookup_result *lookup)
  {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1199
  	int ret, index, max_rec_len, add_to_free_list = 0;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1200
  	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1201
1202
1203
  	struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
  	struct ocfs2_dx_leaf *dx_leaf;
  	struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1204
  	struct ocfs2_dir_block_trailer *trailer;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1205
1206
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dx_entry_list *entry_list;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
  	/*
  	 * This function gets a bit messy because we might have to
  	 * modify the root block, regardless of whether the indexed
  	 * entries are stored inline.
  	 */
  
  	/*
  	 * *Only* set 'entry_list' here, based on where we're looking
  	 * for the indexed entries. Later, we might still want to
  	 * journal both blocks, based on free list state.
  	 */
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1218
1219
1220
1221
1222
1223
1224
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  	if (ocfs2_dx_root_inline(dx_root)) {
  		entry_list = &dx_root->dr_entries;
  	} else {
  		dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
  		entry_list = &dx_leaf->dl_list;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1225

9b7895efa   Mark Fasheh   ocfs2: Add a name...
1226
1227
  	/* Neither of these are a disk corruption - that should have
  	 * been caught by lookup, before we got here. */
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1228
1229
  	BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
  	BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1230

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1231
  	index = (char *)dx_entry - (char *)entry_list->de_entries;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1232
  	index /= sizeof(*dx_entry);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1233
  	if (index >= le16_to_cpu(entry_list->de_num_used)) {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1234
1235
  		mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)
  ",
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1236
1237
  		     (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
  		     entry_list, dx_entry);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1238
1239
  		return -EIO;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1240
  	/*
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1241
1242
1243
1244
1245
1246
1247
1248
1249
  	 * We know that removal of this dirent will leave enough room
  	 * for a new one, so add this block to the free list if it
  	 * isn't already there.
  	 */
  	trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  	if (trailer->db_free_rec_len == 0)
  		add_to_free_list = 1;
  
  	/*
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1250
1251
1252
1253
1254
1255
  	 * Add the block holding our index into the journal before
  	 * removing the unindexed entry. If we get an error return
  	 * from __ocfs2_delete_entry(), then it hasn't removed the
  	 * entry yet. Likewise, successful return means we *must*
  	 * remove the indexed entry.
  	 *
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1256
1257
1258
  	 * We're also careful to journal the root tree block here as
  	 * the entry count needs to be updated. Also, we might be
  	 * adding to the start of the free list.
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1259
  	 */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1260
  	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1261
1262
1263
1264
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1265
1266
1267
  	}
  
  	if (!ocfs2_dx_root_inline(dx_root)) {
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1268
  		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1269
1270
1271
1272
1273
1274
  					      lookup->dl_dx_leaf_bh,
  					      OCFS2_JOURNAL_ACCESS_WRITE);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1275
  	}
f1088d471   Tao Ma   ocfs2: Remove mlo...
1276
1277
  	trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
  				    index);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1278

9b7895efa   Mark Fasheh   ocfs2: Add a name...
1279
1280
1281
1282
1283
1284
  	ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
  				   leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
  	max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
  	trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  	if (add_to_free_list) {
  		trailer->db_free_next = dx_root->dr_free_blk;
  		dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
  		ocfs2_journal_dirty(handle, dx_root_bh);
  	}
  
  	/* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
  	ocfs2_journal_dirty(handle, leaf_bh);
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1295
1296
  	le32_add_cpu(&dx_root->dr_num_entries, -1);
  	ocfs2_journal_dirty(handle, dx_root_bh);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1297
  	ocfs2_dx_list_remove_entry(entry_list, index);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1298

e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1299
  	if (!ocfs2_dx_root_inline(dx_root))
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1300
  		ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1301
1302
1303
1304
  
  out:
  	return ret;
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1305
1306
1307
1308
1309
1310
1311
1312
1313
  static inline int ocfs2_delete_entry_id(handle_t *handle,
  					struct inode *dir,
  					struct ocfs2_dir_entry *de_del,
  					struct buffer_head *bh)
  {
  	int ret;
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_dinode *di;
  	struct ocfs2_inline_data *data;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1314
  	ret = ocfs2_read_inode_block(dir, &di_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	di = (struct ocfs2_dinode *)di_bh->b_data;
  	data = &di->id2.i_data;
  
  	ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
  				   i_size_read(dir));
  
  	brelse(di_bh);
  out:
  	return ret;
  }
  
  static inline int ocfs2_delete_entry_el(handle_t *handle,
  					struct inode *dir,
  					struct ocfs2_dir_entry *de_del,
  					struct buffer_head *bh)
  {
  	return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
  				    bh->b_size);
  }
  
  /*
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1341
1342
   * Delete a directory entry. Hide the details of directory
   * implementation from the caller.
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1343
1344
1345
   */
  int ocfs2_delete_entry(handle_t *handle,
  		       struct inode *dir,
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1346
  		       struct ocfs2_dir_lookup_result *res)
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1347
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1348
1349
  	if (ocfs2_dir_indexed(dir))
  		return ocfs2_delete_entry_dx(handle, dir, res);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1350
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1351
1352
  		return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
  					     res->dl_leaf_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1353

4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1354
1355
  	return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
  				     res->dl_leaf_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1356
  }
8553cf4f3   Mark Fasheh   ocfs2: Cleanup di...
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
  /*
   * Check whether 'de' has enough room to hold an entry of
   * 'new_rec_len' bytes.
   */
  static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
  					 unsigned int new_rec_len)
  {
  	unsigned int de_really_used;
  
  	/* Check whether this is an empty record with enough space */
  	if (le64_to_cpu(de->inode) == 0 &&
  	    le16_to_cpu(de->rec_len) >= new_rec_len)
  		return 1;
  
  	/*
  	 * Record might have free space at the end which we can
  	 * use.
  	 */
  	de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
  	if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
  	    return 1;
  
  	return 0;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
  					  struct ocfs2_dx_entry *dx_new_entry)
  {
  	int i;
  
  	i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  	dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
  
  	le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
  }
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
  static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
  				       struct ocfs2_dx_hinfo *hinfo,
  				       u64 dirent_blk)
  {
  	int i;
  	struct ocfs2_dx_entry *dx_entry;
  
  	i = le16_to_cpu(entry_list->de_num_used);
  	dx_entry = &entry_list->de_entries[i];
  
  	memset(dx_entry, 0, sizeof(*dx_entry));
  	dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
  	dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
  	dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
  
  	le16_add_cpu(&entry_list->de_num_used, 1);
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1408
1409
1410
1411
1412
  static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
  				      struct ocfs2_dx_hinfo *hinfo,
  				      u64 dirent_blk,
  				      struct buffer_head *dx_leaf_bh)
  {
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1413
  	int ret;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1414
  	struct ocfs2_dx_leaf *dx_leaf;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1415
  	ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1416
1417
1418
1419
1420
1421
1422
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1423
1424
  	ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
  	ocfs2_journal_dirty(handle, dx_leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1425

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1426
1427
1428
  out:
  	return ret;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1429

e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1430
1431
1432
1433
  static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
  					struct ocfs2_dx_hinfo *hinfo,
  					u64 dirent_blk,
  					struct ocfs2_dx_root_block *dx_root)
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1434
  {
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1435
1436
1437
1438
1439
1440
1441
  	ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
  }
  
  static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
  			       struct ocfs2_dir_lookup_result *lookup)
  {
  	int ret = 0;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1442
  	struct ocfs2_dx_root_block *dx_root;
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1443
  	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1444

0cf2f7632   Joel Becker   ocfs2: Pass struc...
1445
  	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1446
1447
1448
1449
1450
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
  	dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
  	if (ocfs2_dx_root_inline(dx_root)) {
  		ocfs2_dx_inline_root_insert(dir, handle,
  					    &lookup->dl_hinfo,
  					    lookup->dl_leaf_bh->b_blocknr,
  					    dx_root);
  	} else {
  		ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
  						 lookup->dl_leaf_bh->b_blocknr,
  						 lookup->dl_dx_leaf_bh);
  		if (ret)
  			goto out;
  	}
  
  	le32_add_cpu(&dx_root->dr_num_entries, 1);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1466
  	ocfs2_journal_dirty(handle, dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1467
1468
1469
1470
  
  out:
  	return ret;
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
  static void ocfs2_remove_block_from_free_list(struct inode *dir,
  				       handle_t *handle,
  				       struct ocfs2_dir_lookup_result *lookup)
  {
  	struct ocfs2_dir_block_trailer *trailer, *prev;
  	struct ocfs2_dx_root_block *dx_root;
  	struct buffer_head *bh;
  
  	trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  
  	if (ocfs2_free_list_at_root(lookup)) {
  		bh = lookup->dl_dx_root_bh;
  		dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
  		dx_root->dr_free_blk = trailer->db_free_next;
  	} else {
  		bh = lookup->dl_prev_leaf_bh;
  		prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
  		prev->db_free_next = trailer->db_free_next;
  	}
  
  	trailer->db_free_rec_len = cpu_to_le16(0);
  	trailer->db_free_next = cpu_to_le64(0);
  
  	ocfs2_journal_dirty(handle, bh);
  	ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  }
  
  /*
   * This expects that a journal write has been reserved on
   * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
   */
  static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
  				   struct ocfs2_dir_lookup_result *lookup)
  {
  	int max_rec_len;
  	struct ocfs2_dir_block_trailer *trailer;
  
  	/* Walk dl_leaf_bh to figure out what the new free rec_len is. */
  	max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
  	if (max_rec_len) {
  		/*
  		 * There's still room in this block, so no need to remove it
  		 * from the free list. In this case, we just want to update
  		 * the rec len accounting.
  		 */
  		trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
  		trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
  		ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
  	} else {
  		ocfs2_remove_block_from_free_list(dir, handle, lookup);
  	}
  }
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1523
1524
1525
  /* we don't always have a dentry for what we want to add, so people
   * like orphan dir can call this instead.
   *
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1526
1527
   * The lookup context must have been filled from
   * ocfs2_prepare_dir_for_insert.
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1528
1529
1530
1531
1532
1533
   */
  int __ocfs2_add_entry(handle_t *handle,
  		      struct inode *dir,
  		      const char *name, int namelen,
  		      struct inode *inode, u64 blkno,
  		      struct buffer_head *parent_fe_bh,
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1534
  		      struct ocfs2_dir_lookup_result *lookup)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1535
1536
1537
1538
  {
  	unsigned long offset;
  	unsigned short rec_len;
  	struct ocfs2_dir_entry *de, *de1;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1539
1540
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
  	struct super_block *sb = dir->i_sb;
2e1731524   Daeseok Youn   ocfs2: use retval...
1541
  	int retval;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1542
  	unsigned int size = sb->s_blocksize;
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1543
  	struct buffer_head *insert_bh = lookup->dl_leaf_bh;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1544
  	char *data_start = insert_bh->b_data;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1545

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1546
1547
  	if (!namelen)
  		return -EINVAL;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
  	if (ocfs2_dir_indexed(dir)) {
  		struct buffer_head *bh;
  
  		/*
  		 * An indexed dir may require that we update the free space
  		 * list. Reserve a write to the previous node in the list so
  		 * that we don't fail later.
  		 *
  		 * XXX: This can be either a dx_root_block, or an unindexed
  		 * directory tree leaf block.
  		 */
  		if (ocfs2_free_list_at_root(lookup)) {
  			bh = lookup->dl_dx_root_bh;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1561
1562
  			retval = ocfs2_journal_access_dr(handle,
  						 INODE_CACHE(dir), bh,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1563
1564
1565
  						 OCFS2_JOURNAL_ACCESS_WRITE);
  		} else {
  			bh = lookup->dl_prev_leaf_bh;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1566
1567
  			retval = ocfs2_journal_access_db(handle,
  						 INODE_CACHE(dir), bh,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1568
1569
1570
1571
1572
1573
1574
  						 OCFS2_JOURNAL_ACCESS_WRITE);
  		}
  		if (retval) {
  			mlog_errno(retval);
  			return retval;
  		}
  	} else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1575
1576
1577
1578
1579
  		data_start = di->id2.i_data.id_data;
  		size = i_size_read(dir);
  
  		BUG_ON(insert_bh != parent_fe_bh);
  	}
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1580
1581
  	rec_len = OCFS2_DIR_REC_LEN(namelen);
  	offset = 0;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1582
  	de = (struct ocfs2_dir_entry *) data_start;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1583
  	while (1) {
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
1584
  		BUG_ON((char *)de >= (size + data_start));
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
  		/* These checks should've already been passed by the
  		 * prepare function, but I guess we can leave them
  		 * here anyway. */
  		if (!ocfs2_check_dir_entry(dir, de, insert_bh, offset)) {
  			retval = -ENOENT;
  			goto bail;
  		}
  		if (ocfs2_match(namelen, name, de)) {
  			retval = -EEXIST;
  			goto bail;
  		}
8553cf4f3   Mark Fasheh   ocfs2: Cleanup di...
1596

87d35a74b   Mark Fasheh   ocfs2: Add direct...
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
  		/* We're guaranteed that we should have space, so we
  		 * can't possibly have hit the trailer...right? */
  		mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
  				"Hit dir trailer trying to insert %.*s "
  			        "(namelen %d) into directory %llu.  "
  				"offset is %lu, trailer offset is %d
  ",
  				namelen, name, namelen,
  				(unsigned long long)parent_fe_bh->b_blocknr,
  				offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
8553cf4f3   Mark Fasheh   ocfs2: Cleanup di...
1607
  		if (ocfs2_dirent_would_fit(de, rec_len)) {
078cd8279   Deepa Dinamani   fs: Replace CURRE...
1608
  			dir->i_mtime = dir->i_ctime = current_time(dir);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1609
1610
1611
1612
1613
  			retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  			if (retval < 0) {
  				mlog_errno(retval);
  				goto bail;
  			}
13723d00e   Joel Becker   ocfs2: Use metada...
1614
  			if (insert_bh == parent_fe_bh)
2e1731524   Daeseok Youn   ocfs2: use retval...
1615
  				retval = ocfs2_journal_access_di(handle,
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1616
  								 INODE_CACHE(dir),
13723d00e   Joel Becker   ocfs2: Use metada...
1617
1618
  								 insert_bh,
  								 OCFS2_JOURNAL_ACCESS_WRITE);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1619
  			else {
2e1731524   Daeseok Youn   ocfs2: use retval...
1620
  				retval = ocfs2_journal_access_db(handle,
0cf2f7632   Joel Becker   ocfs2: Pass struc...
1621
  								 INODE_CACHE(dir),
13723d00e   Joel Becker   ocfs2: Use metada...
1622
  								 insert_bh,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1623
  					      OCFS2_JOURNAL_ACCESS_WRITE);
2e1731524   Daeseok Youn   ocfs2: use retval...
1624
1625
  				if (!retval && ocfs2_dir_indexed(dir))
  					retval = ocfs2_dx_dir_insert(dir,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
1626
1627
  								handle,
  								lookup);
2e1731524   Daeseok Youn   ocfs2: use retval...
1628
1629
1630
1631
1632
  			}
  
  			if (retval) {
  				mlog_errno(retval);
  				goto bail;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1633
  			}
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
  			/* By now the buffer is marked for journaling */
  			offset += le16_to_cpu(de->rec_len);
  			if (le64_to_cpu(de->inode)) {
  				de1 = (struct ocfs2_dir_entry *)((char *) de +
  					OCFS2_DIR_REC_LEN(de->name_len));
  				de1->rec_len =
  					cpu_to_le16(le16_to_cpu(de->rec_len) -
  					OCFS2_DIR_REC_LEN(de->name_len));
  				de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  				de = de1;
  			}
9dc2108d6   Phillip Potter   ocfs2: use common...
1645
  			de->file_type = FT_UNKNOWN;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1646
1647
1648
1649
1650
1651
1652
  			if (blkno) {
  				de->inode = cpu_to_le64(blkno);
  				ocfs2_set_de_type(de, inode->i_mode);
  			} else
  				de->inode = 0;
  			de->name_len = namelen;
  			memcpy(de->name, name, namelen);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
1653
1654
  			if (ocfs2_dir_indexed(dir))
  				ocfs2_recalc_free_list(dir, handle, lookup);
cc56c33e7   Jeff Layton   ocfs2: convert to...
1655
  			inode_inc_iversion(dir);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
1656
  			ocfs2_journal_dirty(handle, insert_bh);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1657
1658
1659
  			retval = 0;
  			goto bail;
  		}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
1660

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1661
1662
1663
1664
1665
1666
1667
1668
  		offset += le16_to_cpu(de->rec_len);
  		de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
  	}
  
  	/* when you think about it, the assert above should prevent us
  	 * from ever getting here. */
  	retval = -ENOSPC;
  bail:
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1669
1670
  	if (retval)
  		mlog_errno(retval);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1671

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
1672
1673
  	return retval;
  }
23193e513   Mark Fasheh   ocfs2: Read suppo...
1674
  static int ocfs2_dir_foreach_blk_id(struct inode *inode,
2b47c3611   Mathieu Desnoyers   Fix f_version typ...
1675
  				    u64 *f_version,
3704412bd   Al Viro   [readdir] convert...
1676
  				    struct dir_context *ctx)
23193e513   Mark Fasheh   ocfs2: Read suppo...
1677
  {
3704412bd   Al Viro   [readdir] convert...
1678
1679
  	int ret, i;
  	unsigned long offset = ctx->pos;
23193e513   Mark Fasheh   ocfs2: Read suppo...
1680
1681
1682
1683
  	struct buffer_head *di_bh = NULL;
  	struct ocfs2_dinode *di;
  	struct ocfs2_inline_data *data;
  	struct ocfs2_dir_entry *de;
b657c95c1   Joel Becker   ocfs2: Wrap inode...
1684
  	ret = ocfs2_read_inode_block(inode, &di_bh);
23193e513   Mark Fasheh   ocfs2: Read suppo...
1685
1686
1687
1688
1689
1690
1691
1692
1693
  	if (ret) {
  		mlog(ML_ERROR, "Unable to read inode block for dir %llu
  ",
  		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
  		goto out;
  	}
  
  	di = (struct ocfs2_dinode *)di_bh->b_data;
  	data = &di->id2.i_data;
3704412bd   Al Viro   [readdir] convert...
1694
  	while (ctx->pos < i_size_read(inode)) {
23193e513   Mark Fasheh   ocfs2: Read suppo...
1695
1696
1697
1698
  		/* If the dir block has changed since the last call to
  		 * readdir(2), then we might be pointing to an invalid
  		 * dirent right now.  Scan from the start of the block
  		 * to make sure. */
c472c07bf   Goffredo Baroncelli   iversion: Rename ...
1699
  		if (!inode_eq_iversion(inode, *f_version)) {
23193e513   Mark Fasheh   ocfs2: Read suppo...
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
  			for (i = 0; i < i_size_read(inode) && i < offset; ) {
  				de = (struct ocfs2_dir_entry *)
  					(data->id_data + i);
  				/* It's too expensive to do a full
  				 * dirent test each time round this
  				 * loop, but we do have to test at
  				 * least that it is non-zero.  A
  				 * failure will be detected in the
  				 * dirent test below. */
  				if (le16_to_cpu(de->rec_len) <
  				    OCFS2_DIR_REC_LEN(1))
  					break;
  				i += le16_to_cpu(de->rec_len);
  			}
3704412bd   Al Viro   [readdir] convert...
1714
  			ctx->pos = offset = i;
cc56c33e7   Jeff Layton   ocfs2: convert to...
1715
  			*f_version = inode_query_iversion(inode);
23193e513   Mark Fasheh   ocfs2: Read suppo...
1716
  		}
3704412bd   Al Viro   [readdir] convert...
1717
1718
  		de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
  		if (!ocfs2_check_dir_entry(inode, de, di_bh, ctx->pos)) {
23193e513   Mark Fasheh   ocfs2: Read suppo...
1719
  			/* On error, skip the f_pos to the end. */
3704412bd   Al Viro   [readdir] convert...
1720
1721
  			ctx->pos = i_size_read(inode);
  			break;
23193e513   Mark Fasheh   ocfs2: Read suppo...
1722
1723
1724
  		}
  		offset += le16_to_cpu(de->rec_len);
  		if (le64_to_cpu(de->inode)) {
3704412bd   Al Viro   [readdir] convert...
1725
  			if (!dir_emit(ctx, de->name, de->name_len,
9dc2108d6   Phillip Potter   ocfs2: use common...
1726
1727
  				      le64_to_cpu(de->inode),
  				      fs_ftype_to_dtype(de->file_type)))
3704412bd   Al Viro   [readdir] convert...
1728
  				goto out;
23193e513   Mark Fasheh   ocfs2: Read suppo...
1729
  		}
3704412bd   Al Viro   [readdir] convert...
1730
  		ctx->pos += le16_to_cpu(de->rec_len);
23193e513   Mark Fasheh   ocfs2: Read suppo...
1731
  	}
23193e513   Mark Fasheh   ocfs2: Read suppo...
1732
1733
  out:
  	brelse(di_bh);
23193e513   Mark Fasheh   ocfs2: Read suppo...
1734
1735
  	return 0;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
1736
1737
1738
1739
  /*
   * NOTE: This function can be called against unindexed directories,
   * and indexed ones.
   */
23193e513   Mark Fasheh   ocfs2: Read suppo...
1740
  static int ocfs2_dir_foreach_blk_el(struct inode *inode,
2b47c3611   Mathieu Desnoyers   Fix f_version typ...
1741
  				    u64 *f_version,
3704412bd   Al Viro   [readdir] convert...
1742
1743
  				    struct dir_context *ctx,
  				    bool persist)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1744
  {
aa9588741   Mark Fasheh   ocfs2: implement ...
1745
  	unsigned long offset, blk, last_ra_blk = 0;
3704412bd   Al Viro   [readdir] convert...
1746
  	int i;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1747
1748
  	struct buffer_head * bh, * tmp;
  	struct ocfs2_dir_entry * de;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1749
  	struct super_block * sb = inode->i_sb;
aa9588741   Mark Fasheh   ocfs2: implement ...
1750
  	unsigned int ra_sectors = 16;
3704412bd   Al Viro   [readdir] convert...
1751
  	int stored = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1752

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1753
  	bh = NULL;
3704412bd   Al Viro   [readdir] convert...
1754
  	offset = ctx->pos & (sb->s_blocksize - 1);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1755

3704412bd   Al Viro   [readdir] convert...
1756
1757
  	while (ctx->pos < i_size_read(inode)) {
  		blk = ctx->pos >> sb->s_blocksize_bits;
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
1758
1759
  		if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
  			/* Skip the corrupt dirblock and keep trying */
3704412bd   Al Viro   [readdir] convert...
1760
  			ctx->pos += sb->s_blocksize - offset;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1761
1762
  			continue;
  		}
aa9588741   Mark Fasheh   ocfs2: implement ...
1763
1764
1765
1766
1767
1768
1769
1770
  		/* The idea here is to begin with 8k read-ahead and to stay
  		 * 4k ahead of our current position.
  		 *
  		 * TODO: Use the pagecache for this. We just need to
  		 * make sure it's cluster-safe... */
  		if (!last_ra_blk
  		    || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
  			for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1771
  			     i > 0; i--) {
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
1772
1773
1774
1775
  				tmp = NULL;
  				if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
  							  OCFS2_BH_READAHEAD))
  					brelse(tmp);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1776
  			}
aa9588741   Mark Fasheh   ocfs2: implement ...
1777
1778
  			last_ra_blk = blk;
  			ra_sectors = 8;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1779
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1780
1781
1782
1783
  		/* If the dir block has changed since the last call to
  		 * readdir(2), then we might be pointing to an invalid
  		 * dirent right now.  Scan from the start of the block
  		 * to make sure. */
c472c07bf   Goffredo Baroncelli   iversion: Rename ...
1784
  		if (!inode_eq_iversion(inode, *f_version)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
  			for (i = 0; i < sb->s_blocksize && i < offset; ) {
  				de = (struct ocfs2_dir_entry *) (bh->b_data + i);
  				/* It's too expensive to do a full
  				 * dirent test each time round this
  				 * loop, but we do have to test at
  				 * least that it is non-zero.  A
  				 * failure will be detected in the
  				 * dirent test below. */
  				if (le16_to_cpu(de->rec_len) <
  				    OCFS2_DIR_REC_LEN(1))
  					break;
  				i += le16_to_cpu(de->rec_len);
  			}
  			offset = i;
3704412bd   Al Viro   [readdir] convert...
1799
  			ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1800
  				| offset;
cc56c33e7   Jeff Layton   ocfs2: convert to...
1801
  			*f_version = inode_query_iversion(inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1802
  		}
3704412bd   Al Viro   [readdir] convert...
1803
  		while (ctx->pos < i_size_read(inode)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1804
1805
1806
1807
1808
  		       && offset < sb->s_blocksize) {
  			de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
  			if (!ocfs2_check_dir_entry(inode, de, bh, offset)) {
  				/* On error, skip the f_pos to the
  				   next block. */
3704412bd   Al Viro   [readdir] convert...
1809
  				ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
29aa30167   Changwei Ge   ocfs2: fix a misu...
1810
  				break;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1811
  			}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1812
  			if (le64_to_cpu(de->inode)) {
3704412bd   Al Viro   [readdir] convert...
1813
  				if (!dir_emit(ctx, de->name,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1814
  						de->name_len,
7e8536797   Mark Fasheh   ocfs2: Pass raw u...
1815
  						le64_to_cpu(de->inode),
9dc2108d6   Phillip Potter   ocfs2: use common...
1816
  					fs_ftype_to_dtype(de->file_type))) {
3704412bd   Al Viro   [readdir] convert...
1817
1818
  					brelse(bh);
  					return 0;
e7b340196   Mark Fasheh   ocfs2: Optionally...
1819
  				}
3704412bd   Al Viro   [readdir] convert...
1820
  				stored++;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1821
  			}
3704412bd   Al Viro   [readdir] convert...
1822
1823
  			offset += le16_to_cpu(de->rec_len);
  			ctx->pos += le16_to_cpu(de->rec_len);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1824
1825
1826
  		}
  		offset = 0;
  		brelse(bh);
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
1827
  		bh = NULL;
3704412bd   Al Viro   [readdir] convert...
1828
1829
  		if (!persist && stored)
  			break;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1830
  	}
3704412bd   Al Viro   [readdir] convert...
1831
  	return 0;
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1832
  }
2b47c3611   Mathieu Desnoyers   Fix f_version typ...
1833
  static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
3704412bd   Al Viro   [readdir] convert...
1834
1835
  				 struct dir_context *ctx,
  				 bool persist)
23193e513   Mark Fasheh   ocfs2: Read suppo...
1836
1837
  {
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
3704412bd   Al Viro   [readdir] convert...
1838
1839
  		return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
  	return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
23193e513   Mark Fasheh   ocfs2: Read suppo...
1840
  }
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1841
  /*
5eae5b96f   Mark Fasheh   ocfs2: Remove ope...
1842
1843
1844
   * This is intended to be called from inside other kernel functions,
   * so we fake some arguments.
   */
3704412bd   Al Viro   [readdir] convert...
1845
  int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
5eae5b96f   Mark Fasheh   ocfs2: Remove ope...
1846
  {
cc56c33e7   Jeff Layton   ocfs2: convert to...
1847
  	u64 version = inode_query_iversion(inode);
3704412bd   Al Viro   [readdir] convert...
1848
  	ocfs2_dir_foreach_blk(inode, &version, ctx, true);
5eae5b96f   Mark Fasheh   ocfs2: Remove ope...
1849
1850
1851
1852
  	return 0;
  }
  
  /*
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1853
1854
1855
   * ocfs2_readdir()
   *
   */
3704412bd   Al Viro   [readdir] convert...
1856
  int ocfs2_readdir(struct file *file, struct dir_context *ctx)
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1857
1858
  {
  	int error = 0;
3704412bd   Al Viro   [readdir] convert...
1859
  	struct inode *inode = file_inode(file);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1860
  	int lock_level = 0;
f1088d471   Tao Ma   ocfs2: Remove mlo...
1861
  	trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1862

c4c2416ab   Gang He   ocfs2: nowait aio...
1863
  	error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1864
1865
1866
1867
  	if (lock_level && error >= 0) {
  		/* We release EX lock which used to update atime
  		 * and get PR lock again to reduce contention
  		 * on commonly accessed directories. */
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1868
  		ocfs2_inode_unlock(inode, 1);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1869
  		lock_level = 0;
e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1870
  		error = ocfs2_inode_lock(inode, NULL, 0);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1871
1872
1873
1874
1875
1876
1877
  	}
  	if (error < 0) {
  		if (error != -ENOENT)
  			mlog_errno(error);
  		/* we haven't got any yet, so propagate the error. */
  		goto bail_nolock;
  	}
3704412bd   Al Viro   [readdir] convert...
1878
  	error = ocfs2_dir_foreach_blk(inode, &file->f_version, ctx, false);
b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1879

e63aecb65   Mark Fasheh   ocfs2: Rename ocf...
1880
  	ocfs2_inode_unlock(inode, lock_level);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
1881
1882
  	if (error)
  		mlog_errno(error);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1883

aa9588741   Mark Fasheh   ocfs2: implement ...
1884
  bail_nolock:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1885

b8bc5f4fd   Mark Fasheh   ocfs2: Abstract o...
1886
  	return error;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1887
1888
1889
  }
  
  /*
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
1890
   * NOTE: this should always be called with parent dir i_mutex taken.
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1891
1892
1893
1894
1895
   */
  int ocfs2_find_files_on_disk(const char *name,
  			     int namelen,
  			     u64 *blkno,
  			     struct inode *inode,
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1896
  			     struct ocfs2_dir_lookup_result *lookup)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1897
1898
  {
  	int status = -ENOENT;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1899

f1088d471   Tao Ma   ocfs2: Remove mlo...
1900
1901
  	trace_ocfs2_find_files_on_disk(namelen, name, blkno,
  				(unsigned long long)OCFS2_I(inode)->ip_blkno);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1902

4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1903
1904
  	status = ocfs2_find_entry(name, namelen, inode, lookup);
  	if (status)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1905
  		goto leave;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1906

4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1907
  	*blkno = le64_to_cpu(lookup->dl_entry->inode);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1908
1909
1910
  
  	status = 0;
  leave:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1911

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1912
1913
  	return status;
  }
be94d1170   Mark Fasheh   ocfs2: Provide co...
1914
1915
1916
1917
1918
1919
1920
1921
  /*
   * Convenience function for callers which just want the block number
   * mapped to a name and don't require the full dirent info, etc.
   */
  int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
  			       int namelen, u64 *blkno)
  {
  	int ret;
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1922
  	struct ocfs2_dir_lookup_result lookup = { NULL, };
be94d1170   Mark Fasheh   ocfs2: Provide co...
1923

4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1924
1925
  	ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
  	ocfs2_free_dir_lookup_result(&lookup);
be94d1170   Mark Fasheh   ocfs2: Provide co...
1926
1927
1928
  
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1929
1930
1931
1932
1933
  /* Check for a name within a directory.
   *
   * Return 0 if the name does not exist
   * Return -EEXIST if the directory contains the name
   *
1b1dcc1b5   Jes Sorensen   [PATCH] mutex sub...
1934
   * Callers should have i_mutex + a cluster lock on dir
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1935
1936
1937
1938
1939
   */
  int ocfs2_check_dir_for_entry(struct inode *dir,
  			      const char *name,
  			      int namelen)
  {
7c01ad8fe   Daeseok Youn   ocfs2: remove got...
1940
  	int ret = 0;
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1941
  	struct ocfs2_dir_lookup_result lookup = { NULL, };
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1942

f1088d471   Tao Ma   ocfs2: Remove mlo...
1943
1944
  	trace_ocfs2_check_dir_for_entry(
  		(unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1945

7c01ad8fe   Daeseok Youn   ocfs2: remove got...
1946
1947
1948
1949
  	if (ocfs2_find_entry(name, namelen, dir, &lookup) == 0) {
  		ret = -EEXIST;
  		mlog_errno(ret);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1950

4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
1951
  	ocfs2_free_dir_lookup_result(&lookup);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1952

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
1953
1954
  	return ret;
  }
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1955
  struct ocfs2_empty_dir_priv {
3704412bd   Al Viro   [readdir] convert...
1956
  	struct dir_context ctx;
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1957
1958
1959
  	unsigned seen_dot;
  	unsigned seen_dot_dot;
  	unsigned seen_other;
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1960
  	unsigned dx_dir;
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1961
  };
ac7576f4b   Miklos Szeredi   vfs: make first a...
1962
1963
1964
  static int ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
  				   int name_len, loff_t pos, u64 ino,
  				   unsigned type)
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1965
  {
ac7576f4b   Miklos Szeredi   vfs: make first a...
1966
1967
  	struct ocfs2_empty_dir_priv *p =
  		container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1968
1969
1970
1971
  
  	/*
  	 * Check the positions of "." and ".." records to be sure
  	 * they're in the correct place.
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1972
1973
1974
1975
1976
1977
1978
  	 *
  	 * Indexed directories don't need to proceed past the first
  	 * two entries, so we end the scan after seeing '..'. Despite
  	 * that, we allow the scan to proceed In the event that we
  	 * have a corrupted indexed directory (no dot or dot dot
  	 * entries). This allows us to double check for existing
  	 * entries which might not have been found in the index.
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1979
1980
1981
1982
1983
1984
1985
1986
1987
  	 */
  	if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
  		p->seen_dot = 1;
  		return 0;
  	}
  
  	if (name_len == 2 && !strncmp("..", name, 2) &&
  	    pos == OCFS2_DIR_REC_LEN(1)) {
  		p->seen_dot_dot = 1;
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1988
1989
1990
  
  		if (p->dx_dir && p->seen_dot)
  			return 1;
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
1991
1992
1993
1994
1995
1996
  		return 0;
  	}
  
  	p->seen_other = 1;
  	return 1;
  }
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
  
  static int ocfs2_empty_dir_dx(struct inode *inode,
  			      struct ocfs2_empty_dir_priv *priv)
  {
  	int ret;
  	struct buffer_head *di_bh = NULL;
  	struct buffer_head *dx_root_bh = NULL;
  	struct ocfs2_dinode *di;
  	struct ocfs2_dx_root_block *dx_root;
  
  	priv->dx_dir = 1;
  
  	ret = ocfs2_read_inode_block(inode, &di_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  	di = (struct ocfs2_dinode *)di_bh->b_data;
  
  	ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  
  	if (le32_to_cpu(dx_root->dr_num_entries) != 2)
  		priv->seen_other = 1;
  
  out:
  	brelse(di_bh);
  	brelse(dx_root_bh);
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2031
2032
  /*
   * routine to check that the specified directory is empty (for rmdir)
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
2033
2034
   *
   * Returns 1 if dir is empty, zero otherwise.
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2035
   *
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2036
   * XXX: This is a performance problem for unindexed directories.
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2037
2038
2039
   */
  int ocfs2_empty_dir(struct inode *inode)
  {
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
2040
  	int ret;
3704412bd   Al Viro   [readdir] convert...
2041
  	struct ocfs2_empty_dir_priv priv = {
d6394b590   Jeff Liu   ocfs2: fix null p...
2042
  		.ctx.actor = ocfs2_empty_dir_filldir,
3704412bd   Al Viro   [readdir] convert...
2043
  	};
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2044

e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2045
2046
2047
2048
2049
2050
2051
2052
2053
  	if (ocfs2_dir_indexed(inode)) {
  		ret = ocfs2_empty_dir_dx(inode, &priv);
  		if (ret)
  			mlog_errno(ret);
  		/*
  		 * We still run ocfs2_dir_foreach to get the checks
  		 * for "." and "..".
  		 */
  	}
3704412bd   Al Viro   [readdir] convert...
2054
  	ret = ocfs2_dir_foreach(inode, &priv.ctx);
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
2055
2056
2057
2058
2059
2060
  	if (ret)
  		mlog_errno(ret);
  
  	if (!priv.seen_dot || !priv.seen_dot_dot) {
  		mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'
  ",
b0697053f   Mark Fasheh   ocfs2: don't use ...
2061
  		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
2062
2063
2064
  		/*
  		 * XXX: Is it really safe to allow an unlink to continue?
  		 */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2065
2066
  		return 1;
  	}
0bfbbf62a   Mark Fasheh   ocfs2: Implement ...
2067
2068
  
  	return !priv.seen_other;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2069
  }
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2070
2071
2072
2073
2074
2075
2076
2077
2078
  /*
   * Fills "." and ".." dirents in a new directory block. Returns dirent for
   * "..", which might be used during creation of a directory with a trailing
   * header. It is otherwise safe to ignore the return code.
   */
  static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
  							  struct inode *parent,
  							  char *start,
  							  unsigned int size)
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
  {
  	struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
  
  	de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
  	de->name_len = 1;
  	de->rec_len =
  		cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
  	strcpy(de->name, ".");
  	ocfs2_set_de_type(de, S_IFDIR);
  
  	de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
  	de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
  	de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
  	de->name_len = 2;
  	strcpy(de->name, "..");
  	ocfs2_set_de_type(de, S_IFDIR);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2095
2096
  
  	return de;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
  }
  
  /*
   * This works together with code in ocfs2_mknod_locked() which sets
   * the inline-data flag and initializes the inline-data section.
   */
  static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
  				 handle_t *handle,
  				 struct inode *parent,
  				 struct inode *inode,
  				 struct buffer_head *di_bh)
  {
  	int ret;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  	struct ocfs2_inline_data *data = &di->id2.i_data;
  	unsigned int size = le16_to_cpu(data->id_count);
0cf2f7632   Joel Becker   ocfs2: Pass struc...
2113
  	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
2114
  				      OCFS2_JOURNAL_ACCESS_WRITE);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2115
2116
2117
2118
2119
2120
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2121
  	ocfs2_journal_dirty(handle, di_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2122
2123
  
  	i_size_write(inode, size);
bfe868486   Miklos Szeredi   filesystems: add ...
2124
  	set_nlink(inode, 2);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
  	inode->i_blocks = ocfs2_inode_sector_count(inode);
  
  	ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  	if (ret < 0)
  		mlog_errno(ret);
  
  out:
  	return ret;
  }
  
  static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
  				 handle_t *handle,
  				 struct inode *parent,
  				 struct inode *inode,
  				 struct buffer_head *fe_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2140
2141
  				 struct ocfs2_alloc_context *data_ac,
  				 struct buffer_head **ret_new_bh)
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2142
2143
  {
  	int status;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2144
  	unsigned int size = osb->sb->s_blocksize;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2145
  	struct buffer_head *new_bh = NULL;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2146
  	struct ocfs2_dir_entry *de;
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2147

e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2148
  	if (ocfs2_new_dir_wants_trailer(inode))
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2149
  		size = ocfs2_dir_trailer_blk_off(parent->i_sb);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2150
2151
2152
2153
2154
2155
  	status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
  				     data_ac, NULL, &new_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
2156
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2157

0cf2f7632   Joel Becker   ocfs2: Pass struc...
2158
  	status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
2159
  					 OCFS2_JOURNAL_ACCESS_CREATE);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2160
2161
2162
2163
2164
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	memset(new_bh->b_data, 0, osb->sb->s_blocksize);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2165
  	de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
  	if (ocfs2_new_dir_wants_trailer(inode)) {
  		int size = le16_to_cpu(de->rec_len);
  
  		/*
  		 * Figure out the size of the hole left over after
  		 * insertion of '.' and '..'. The trailer wants this
  		 * information.
  		 */
  		size -= OCFS2_DIR_REC_LEN(2);
  		size -= sizeof(struct ocfs2_dir_block_trailer);
  
  		ocfs2_init_dir_trailer(inode, new_bh, size);
  	}
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2179

ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2180
  	ocfs2_journal_dirty(handle, new_bh);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2181
2182
  
  	i_size_write(inode, inode->i_sb->s_blocksize);
bfe868486   Miklos Szeredi   filesystems: add ...
2183
  	set_nlink(inode, 2);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2184
2185
2186
2187
2188
2189
2190
2191
  	inode->i_blocks = ocfs2_inode_sector_count(inode);
  	status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	status = 0;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2192
2193
2194
2195
  	if (ret_new_bh) {
  		*ret_new_bh = new_bh;
  		new_bh = NULL;
  	}
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2196
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
2197
  	brelse(new_bh);
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2198

316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2199
2200
  	return status;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2201
2202
2203
  static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
  				     handle_t *handle, struct inode *dir,
  				     struct buffer_head *di_bh,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2204
  				     struct buffer_head *dirdata_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2205
  				     struct ocfs2_alloc_context *meta_ac,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2206
  				     int dx_inline, u32 num_entries,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2207
  				     struct buffer_head **ret_dx_root_bh)
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2208
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2209
2210
2211
  	int ret;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
  	u16 dr_suballoc_bit;
2b6cb576a   Joel Becker   ocfs2: Set suball...
2212
  	u64 suballoc_loc, dr_blkno;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2213
2214
2215
  	unsigned int num_bits;
  	struct buffer_head *dx_root_bh = NULL;
  	struct ocfs2_dx_root_block *dx_root;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2216
2217
  	struct ocfs2_dir_block_trailer *trailer =
  		ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2218

2b6cb576a   Joel Becker   ocfs2: Set suball...
2219
2220
  	ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
  				   &dr_suballoc_bit, &num_bits, &dr_blkno);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2221
2222
2223
2224
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2225

f1088d471   Tao Ma   ocfs2: Remove mlo...
2226
2227
2228
  	trace_ocfs2_dx_dir_attach_index(
  				(unsigned long long)OCFS2_I(dir)->ip_blkno,
  				(unsigned long long)dr_blkno);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2229

9b7895efa   Mark Fasheh   ocfs2: Add a name...
2230
2231
  	dx_root_bh = sb_getblk(osb->sb, dr_blkno);
  	if (dx_root_bh == NULL) {
7391a294b   Rui Xiang   ocfs2: return ENO...
2232
  		ret = -ENOMEM;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2233
2234
  		goto out;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
2235
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2236

0cf2f7632   Joel Becker   ocfs2: Pass struc...
2237
  	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2238
2239
2240
2241
2242
  				      OCFS2_JOURNAL_ACCESS_CREATE);
  	if (ret < 0) {
  		mlog_errno(ret);
  		goto out;
  	}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
2243

9b7895efa   Mark Fasheh   ocfs2: Add a name...
2244
2245
2246
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  	memset(dx_root, 0, osb->sb->s_blocksize);
  	strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
b89c54282   Tiger Yang   ocfs2: add extent...
2247
  	dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2b6cb576a   Joel Becker   ocfs2: Set suball...
2248
  	dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2249
2250
2251
2252
  	dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
  	dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
  	dx_root->dr_blkno = cpu_to_le64(dr_blkno);
  	dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2253
  	dx_root->dr_num_entries = cpu_to_le32(num_entries);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2254
2255
2256
2257
  	if (le16_to_cpu(trailer->db_free_rec_len))
  		dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
  	else
  		dx_root->dr_free_blk = cpu_to_le64(0);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2258
2259
2260
2261
2262
2263
2264
2265
2266
  
  	if (dx_inline) {
  		dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
  		dx_root->dr_entries.de_count =
  			cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
  	} else {
  		dx_root->dr_list.l_count =
  			cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  	}
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2267
  	ocfs2_journal_dirty(handle, dx_root_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2268

0cf2f7632   Joel Becker   ocfs2: Pass struc...
2269
  	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2270
2271
2272
2273
2274
2275
2276
  				      OCFS2_JOURNAL_ACCESS_CREATE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	di->i_dx_root = cpu_to_le64(dr_blkno);
8ac33dc86   Tao Ma   ocfs2: Hold ip_lo...
2277
  	spin_lock(&OCFS2_I(dir)->ip_lock);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2278
2279
  	OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
  	di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
8ac33dc86   Tao Ma   ocfs2: Hold ip_lo...
2280
  	spin_unlock(&OCFS2_I(dir)->ip_lock);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2281

ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2282
  	ocfs2_journal_dirty(handle, di_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
  
  	*ret_dx_root_bh = dx_root_bh;
  	dx_root_bh = NULL;
  
  out:
  	brelse(dx_root_bh);
  	return ret;
  }
  
  static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
  				       handle_t *handle, struct inode *dir,
  				       struct buffer_head **dx_leaves,
  				       int num_dx_leaves, u64 start_blk)
  {
  	int ret, i;
  	struct ocfs2_dx_leaf *dx_leaf;
  	struct buffer_head *bh;
  
  	for (i = 0; i < num_dx_leaves; i++) {
  		bh = sb_getblk(osb->sb, start_blk + i);
  		if (bh == NULL) {
7391a294b   Rui Xiang   ocfs2: return ENO...
2304
  			ret = -ENOMEM;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2305
2306
2307
  			goto out;
  		}
  		dx_leaves[i] = bh;
8cb471e8f   Joel Becker   ocfs2: Take the i...
2308
  		ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2309

0cf2f7632   Joel Becker   ocfs2: Pass struc...
2310
  		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
  					      OCFS2_JOURNAL_ACCESS_CREATE);
  		if (ret < 0) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
  
  		memset(dx_leaf, 0, osb->sb->s_blocksize);
  		strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
  		dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
  		dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
  		dx_leaf->dl_list.de_count =
  			cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
f1088d471   Tao Ma   ocfs2: Remove mlo...
2325
2326
2327
2328
  		trace_ocfs2_dx_dir_format_cluster(
  				(unsigned long long)OCFS2_I(dir)->ip_blkno,
  				(unsigned long long)bh->b_blocknr,
  				le16_to_cpu(dx_leaf->dl_list.de_count));
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
  
  		ocfs2_journal_dirty(handle, bh);
  	}
  
  	ret = 0;
  out:
  	return ret;
  }
  
  /*
   * Allocates and formats a new cluster for use in an indexed dir
   * leaf. This version will not do the extent insert, so that it can be
   * used by operations which need careful ordering.
   */
  static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
  				      u32 cpos, handle_t *handle,
  				      struct ocfs2_alloc_context *data_ac,
  				      struct buffer_head **dx_leaves,
  				      int num_dx_leaves, u64 *ret_phys_blkno)
  {
  	int ret;
  	u32 phys, num;
  	u64 phys_blkno;
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  
  	/*
  	 * XXX: For create, this should claim cluster for the index
  	 * *before* the unindexed insert so that we have a better
  	 * chance of contiguousness as the directory grows in number
  	 * of entries.
  	 */
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2360
  	ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	/*
  	 * Format the new cluster first. That way, we're inserting
  	 * valid data.
  	 */
  	phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
  	ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
  					  num_dx_leaves, phys_blkno);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	*ret_phys_blkno = phys_blkno;
  out:
  	return ret;
  }
  
  static int ocfs2_dx_dir_new_cluster(struct inode *dir,
  				    struct ocfs2_extent_tree *et,
  				    u32 cpos, handle_t *handle,
  				    struct ocfs2_alloc_context *data_ac,
  				    struct ocfs2_alloc_context *meta_ac,
  				    struct buffer_head **dx_leaves,
  				    int num_dx_leaves)
  {
  	int ret;
  	u64 phys_blkno;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2393
2394
2395
2396
2397
2398
2399
  
  	ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
  					 num_dx_leaves, &phys_blkno);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
cc79d8c19   Joel Becker   ocfs2: ocfs2_inse...
2400
  	ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
  				  meta_ac);
  	if (ret)
  		mlog_errno(ret);
  out:
  	return ret;
  }
  
  static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
  							int *ret_num_leaves)
  {
  	int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
  	struct buffer_head **dx_leaves;
  
  	dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
  			    GFP_NOFS);
  	if (dx_leaves && ret_num_leaves)
  		*ret_num_leaves = num_dx_leaves;
  
  	return dx_leaves;
  }
  
  static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
  				 handle_t *handle,
  				 struct inode *parent,
  				 struct inode *inode,
  				 struct buffer_head *di_bh,
  				 struct ocfs2_alloc_context *data_ac,
  				 struct ocfs2_alloc_context *meta_ac)
  {
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2430
  	int ret;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2431
2432
  	struct buffer_head *leaf_bh = NULL;
  	struct buffer_head *dx_root_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2433
  	struct ocfs2_dx_hinfo hinfo;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2434
2435
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dx_entry_list *entry_list;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2436
2437
2438
2439
2440
2441
2442
2443
  
  	/*
  	 * Our strategy is to create the directory as though it were
  	 * unindexed, then add the index block. This works with very
  	 * little complication since the state of a new directory is a
  	 * very well known quantity.
  	 *
  	 * Essentially, we have two dirents ("." and ".."), in the 1st
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2444
2445
  	 * block which need indexing. These are easily inserted into
  	 * the index block.
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2446
2447
2448
2449
2450
2451
2452
2453
  	 */
  
  	ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
  				    data_ac, &leaf_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2454
  	ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2455
  					meta_ac, 1, 2, &dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2456
2457
2458
2459
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2460
2461
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  	entry_list = &dx_root->dr_entries;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2462

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2463
  	/* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2464
  	ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2465
  	ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2466
2467
  
  	ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2468
  	ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2469
2470
  
  out:
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
  	brelse(dx_root_bh);
  	brelse(leaf_bh);
  	return ret;
  }
  
  int ocfs2_fill_new_dir(struct ocfs2_super *osb,
  		       handle_t *handle,
  		       struct inode *parent,
  		       struct inode *inode,
  		       struct buffer_head *fe_bh,
  		       struct ocfs2_alloc_context *data_ac,
  		       struct ocfs2_alloc_context *meta_ac)
  
  {
  	BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
  
  	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  		return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
  
  	if (ocfs2_supports_indexed_dirs(osb))
  		return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
  					     data_ac, meta_ac);
  
  	return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
  				     data_ac, NULL);
  }
  
  static int ocfs2_dx_dir_index_block(struct inode *dir,
  				    handle_t *handle,
  				    struct buffer_head **dx_leaves,
  				    int num_dx_leaves,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2502
  				    u32 *num_dx_entries,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2503
2504
  				    struct buffer_head *dirent_bh)
  {
0fba81374   Tao Ma   ocfs2: Fix 2 warn...
2505
  	int ret = 0, namelen, i;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
  	char *de_buf, *limit;
  	struct ocfs2_dir_entry *de;
  	struct buffer_head *dx_leaf_bh;
  	struct ocfs2_dx_hinfo hinfo;
  	u64 dirent_blk = dirent_bh->b_blocknr;
  
  	de_buf = dirent_bh->b_data;
  	limit = de_buf + dir->i_sb->s_blocksize;
  
  	while (de_buf < limit) {
  		de = (struct ocfs2_dir_entry *)de_buf;
  
  		namelen = de->name_len;
  		if (!namelen || !de->inode)
  			goto inc;
  
  		ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
  
  		i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
  		dx_leaf_bh = dx_leaves[i];
  
  		ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
  						 dirent_blk, dx_leaf_bh);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2533
  		*num_dx_entries = *num_dx_entries + 1;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2534
2535
2536
2537
2538
2539
2540
  inc:
  		de_buf += le16_to_cpu(de->rec_len);
  	}
  
  out:
  	return ret;
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2541
2542
  
  /*
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
   * XXX: This expects dx_root_bh to already be part of the transaction.
   */
  static void ocfs2_dx_dir_index_root_block(struct inode *dir,
  					 struct buffer_head *dx_root_bh,
  					 struct buffer_head *dirent_bh)
  {
  	char *de_buf, *limit;
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dir_entry *de;
  	struct ocfs2_dx_hinfo hinfo;
  	u64 dirent_blk = dirent_bh->b_blocknr;
  
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  
  	de_buf = dirent_bh->b_data;
  	limit = de_buf + dir->i_sb->s_blocksize;
  
  	while (de_buf < limit) {
  		de = (struct ocfs2_dir_entry *)de_buf;
  
  		if (!de->name_len || !de->inode)
  			goto inc;
  
  		ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
f1088d471   Tao Ma   ocfs2: Remove mlo...
2567
2568
2569
2570
2571
  		trace_ocfs2_dx_dir_index_root_block(
  				(unsigned long long)dir->i_ino,
  				hinfo.major_hash, hinfo.minor_hash,
  				de->name_len, de->name,
  				le16_to_cpu(dx_root->dr_entries.de_num_used));
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2572
2573
2574
  
  		ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
  					   dirent_blk);
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2575
2576
  
  		le32_add_cpu(&dx_root->dr_num_entries, 1);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
  inc:
  		de_buf += le16_to_cpu(de->rec_len);
  	}
  }
  
  /*
   * Count the number of inline directory entries in di_bh and compare
   * them against the number of entries we can hold in an inline dx root
   * block.
   */
  static int ocfs2_new_dx_should_be_inline(struct inode *dir,
  					 struct buffer_head *di_bh)
  {
  	int dirent_count = 0;
  	char *de_buf, *limit;
  	struct ocfs2_dir_entry *de;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  
  	de_buf = di->id2.i_data.id_data;
  	limit = de_buf + i_size_read(dir);
  
  	while (de_buf < limit) {
  		de = (struct ocfs2_dir_entry *)de_buf;
  
  		if (de->name_len && de->inode)
  			dirent_count++;
  
  		de_buf += le16_to_cpu(de->rec_len);
  	}
  
  	/* We are careful to leave room for one extra record. */
  	return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2610
2611
2612
2613
2614
2615
2616
  
  /*
   * Expand rec_len of the rightmost dirent in a directory block so that it
   * contains the end of our valid space for dirents. We do this during
   * expansion from an inline directory to one with extents. The first dir block
   * in that case is taken from the inline data portion of the inode block.
   *
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2617
2618
2619
2620
2621
2622
   * This will also return the largest amount of contiguous space for a dirent
   * in the block. That value is *not* necessarily the last dirent, even after
   * expansion. The directory indexing code wants this value for free space
   * accounting. We do this here since we're already walking the entire dir
   * block.
   *
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2623
2624
   * We add the dir trailer if this filesystem wants it.
   */
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2625
2626
  static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
  					     struct inode *dir)
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2627
  {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2628
  	struct super_block *sb = dir->i_sb;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2629
2630
2631
2632
  	struct ocfs2_dir_entry *de;
  	struct ocfs2_dir_entry *prev_de;
  	char *de_buf, *limit;
  	unsigned int new_size = sb->s_blocksize;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2633
2634
  	unsigned int bytes, this_hole;
  	unsigned int largest_hole = 0;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2635

e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2636
  	if (ocfs2_new_dir_wants_trailer(dir))
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2637
2638
2639
2640
2641
2642
2643
  		new_size = ocfs2_dir_trailer_blk_off(sb);
  
  	bytes = new_size - old_size;
  
  	limit = start + old_size;
  	de_buf = start;
  	de = (struct ocfs2_dir_entry *)de_buf;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2644
  	do {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2645
2646
2647
  		this_hole = ocfs2_figure_dirent_hole(de);
  		if (this_hole > largest_hole)
  			largest_hole = this_hole;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2648
2649
2650
2651
2652
2653
  		prev_de = de;
  		de_buf += le16_to_cpu(de->rec_len);
  		de = (struct ocfs2_dir_entry *)de_buf;
  	} while (de_buf < limit);
  
  	le16_add_cpu(&prev_de->rec_len, bytes);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
  
  	/* We need to double check this after modification of the final
  	 * dirent. */
  	this_hole = ocfs2_figure_dirent_hole(prev_de);
  	if (this_hole > largest_hole)
  		largest_hole = this_hole;
  
  	if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
  		return largest_hole;
  	return 0;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
  }
  
  /*
   * We allocate enough clusters to fulfill "blocks_wanted", but set
   * i_size to exactly one block. Ocfs2_extend_dir() will handle the
   * rest automatically for us.
   *
   * *first_block_bh is a pointer to the 1st data block allocated to the
   *  directory.
   */
  static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
  				   unsigned int blocks_wanted,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2676
  				   struct ocfs2_dir_lookup_result *lookup,
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2677
2678
  				   struct buffer_head **first_block_bh)
  {
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2679
  	u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2680
  	struct super_block *sb = dir->i_sb;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2681
  	int ret, i, num_dx_leaves = 0, dx_inline = 0,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2682
2683
2684
  		credits = ocfs2_inline_to_extents_credits(sb);
  	u64 dx_insert_blkno, blkno,
  		bytes = blocks_wanted << sb->s_blocksize_bits;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2685
2686
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  	struct ocfs2_inode_info *oi = OCFS2_I(dir);
5d44670fa   Marcus Meissner   ocfs2: Initialize...
2687
  	struct ocfs2_alloc_context *data_ac = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2688
  	struct ocfs2_alloc_context *meta_ac = NULL;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2689
  	struct buffer_head *dirdata_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2690
2691
  	struct buffer_head *dx_root_bh = NULL;
  	struct buffer_head **dx_leaves = NULL;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2692
2693
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  	handle_t *handle;
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2694
  	struct ocfs2_extent_tree et;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2695
2696
  	struct ocfs2_extent_tree dx_et;
  	int did_quota = 0, bytes_allocated = 0;
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2697

5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
2698
  	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2699
2700
  
  	alloc = ocfs2_clusters_for_bytes(sb, bytes);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2701
  	dx_alloc = 0;
edd45c084   Jan Kara   ocfs2: Correct or...
2702
  	down_write(&oi->ip_alloc_sem);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2703
  	if (ocfs2_supports_indexed_dirs(osb)) {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2704
  		credits += ocfs2_add_dir_index_credits(sb);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
  		dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
  		if (!dx_inline) {
  			/* Add one more cluster for an index leaf */
  			dx_alloc++;
  			dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
  								&num_dx_leaves);
  			if (!dx_leaves) {
  				ret = -ENOMEM;
  				mlog_errno(ret);
  				goto out;
  			}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2716
2717
2718
2719
2720
2721
2722
2723
2724
  		}
  
  		/* This gets us the dx_root */
  		ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2725
2726
  
  	/*
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2727
2728
2729
  	 * We should never need more than 2 clusters for the unindexed
  	 * tree - maximum dirent size is far less than one block. In
  	 * fact, the only time we'd need more than one cluster is if
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2730
2731
2732
2733
2734
  	 * blocksize == clustersize and the dirent won't fit in the
  	 * extra space that the expansion to a single block gives. As
  	 * of today, that only happens on 4k/4k file systems.
  	 */
  	BUG_ON(alloc > 2);
035a57112   Tao Ma   ocfs2: Reserve 1 ...
2735
  	ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2736
2737
2738
2739
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2740
  	/*
c78bad11f   Joe Perches   fs/: Spelling fixes
2741
  	 * Prepare for worst case allocation scenario of two separate
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2742
  	 * extents in the unindexed tree.
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2743
2744
2745
2746
2747
2748
2749
2750
  	 */
  	if (alloc == 2)
  		credits += OCFS2_SUBALLOC_ALLOC;
  
  	handle = ocfs2_start_trans(osb, credits);
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		mlog_errno(ret);
edd45c084   Jan Kara   ocfs2: Correct or...
2751
  		goto out;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2752
  	}
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
2753
2754
2755
  	ret = dquot_alloc_space_nodirty(dir,
  		ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
  	if (ret)
a90714c15   Jan Kara   ocfs2: Add quota ...
2756
  		goto out_commit;
a90714c15   Jan Kara   ocfs2: Add quota ...
2757
  	did_quota = 1;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2758

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2759
  	if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
  		/*
  		 * Allocate our index cluster first, to maximize the
  		 * possibility that unindexed leaves grow
  		 * contiguously.
  		 */
  		ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
  						 dx_leaves, num_dx_leaves,
  						 &dx_insert_blkno);
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
  		bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2774
2775
2776
2777
2778
  	/*
  	 * Try to claim as many clusters as the bitmap can give though
  	 * if we only get one now, that's enough to continue. The rest
  	 * will be claimed after the conversion to extents.
  	 */
83f92318f   Mark Fasheh   ocfs2: Add dir_re...
2779
2780
  	if (ocfs2_dir_resv_allowed(osb))
  		data_ac->ac_resv = &oi->ip_la_data_resv;
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2781
  	ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2782
2783
2784
2785
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2786
  	bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2787
2788
2789
2790
2791
2792
2793
2794
2795
  
  	/*
  	 * Operations are carefully ordered so that we set up the new
  	 * data block first. The conversion from inline data to
  	 * extents follows.
  	 */
  	blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
  	dirdata_bh = sb_getblk(sb, blkno);
  	if (!dirdata_bh) {
7391a294b   Rui Xiang   ocfs2: return ENO...
2796
  		ret = -ENOMEM;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2797
2798
2799
  		mlog_errno(ret);
  		goto out_commit;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
2800
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2801

0cf2f7632   Joel Becker   ocfs2: Pass struc...
2802
  	ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
2803
  				      OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2804
2805
2806
2807
2808
2809
2810
2811
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
  
  	memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
  	memset(dirdata_bh->b_data + i_size_read(dir), 0,
  	       sb->s_blocksize - i_size_read(dir));
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
  	i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
  	if (ocfs2_new_dir_wants_trailer(dir)) {
  		/*
  		 * Prepare the dir trailer up front. It will otherwise look
  		 * like a valid dirent. Even if inserting the index fails
  		 * (unlikely), then all we'll have done is given first dir
  		 * block a small amount of fragmentation.
  		 */
  		ocfs2_init_dir_trailer(dir, dirdata_bh, i);
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2822

2931cdcb4   Darrick J. Wong   ocfs2: improve fs...
2823
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2824
  	ocfs2_journal_dirty(handle, dirdata_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2825

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2826
2827
2828
2829
  	if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
  		/*
  		 * Dx dirs with an external cluster need to do this up
  		 * front. Inline dx root's get handled later, after
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2830
2831
2832
2833
  		 * we've allocated our root block. We get passed back
  		 * a total number of items so that dr_num_entries can
  		 * be correctly set once the dx_root has been
  		 * allocated.
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2834
  		 */
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2835
  		ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2836
2837
  					       num_dx_leaves, &num_dx_entries,
  					       dirdata_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2838
2839
2840
2841
2842
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2843
2844
2845
2846
2847
2848
2849
2850
  	/*
  	 * Set extent, i_size, etc on the directory. After this, the
  	 * inode should contain the same exact dirents as before and
  	 * be fully accessible from system calls.
  	 *
  	 * We let the later dirent insert modify c/mtime - to the user
  	 * the data hasn't changed.
  	 */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
2851
  	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
2852
  				      OCFS2_JOURNAL_ACCESS_CREATE);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
  
  	spin_lock(&oi->ip_lock);
  	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
  	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
  	spin_unlock(&oi->ip_lock);
  
  	ocfs2_dinode_new_extent_list(dir, di);
  
  	i_size_write(dir, sb->s_blocksize);
078cd8279   Deepa Dinamani   fs: Replace CURRE...
2866
  	dir->i_mtime = dir->i_ctime = current_time(dir);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2867
2868
2869
2870
  
  	di->i_size = cpu_to_le64(sb->s_blocksize);
  	di->i_ctime = di->i_mtime = cpu_to_le64(dir->i_ctime.tv_sec);
  	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(dir->i_ctime.tv_nsec);
6fdb702d6   Darrick J. Wong   ocfs2: call ocfs2...
2871
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2872
2873
2874
2875
2876
  
  	/*
  	 * This should never fail as our extent list is empty and all
  	 * related blocks have been journaled already.
  	 */
cc79d8c19   Joel Becker   ocfs2: ocfs2_inse...
2877
  	ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2878
  				  0, NULL);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2879
2880
  	if (ret) {
  		mlog_errno(ret);
83cab5338   Tao Ma   ocfs2: Jump to co...
2881
  		goto out_commit;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2882
  	}
9780eb6cf   Mark Fasheh   ocfs2: correctly ...
2883
2884
2885
2886
2887
  	/*
  	 * Set i_blocks after the extent insert for the most up to
  	 * date ip_clusters value.
  	 */
  	dir->i_blocks = ocfs2_inode_sector_count(dir);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
2888
  	ocfs2_journal_dirty(handle, di_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2889

9b7895efa   Mark Fasheh   ocfs2: Add a name...
2890
2891
  	if (ocfs2_supports_indexed_dirs(osb)) {
  		ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
2892
  						dirdata_bh, meta_ac, dx_inline,
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
2893
  						num_dx_entries, &dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2894
2895
2896
2897
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2898
2899
2900
2901
  		if (dx_inline) {
  			ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
  						      dirdata_bh);
  		} else {
5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
2902
2903
2904
  			ocfs2_init_dx_root_extent_tree(&dx_et,
  						       INODE_CACHE(dir),
  						       dx_root_bh);
cc79d8c19   Joel Becker   ocfs2: ocfs2_inse...
2905
  			ret = ocfs2_insert_extent(handle, &dx_et, 0,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2906
2907
2908
2909
  						  dx_insert_blkno, 1, 0, NULL);
  			if (ret)
  				mlog_errno(ret);
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2910
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2911
2912
2913
2914
2915
  	/*
  	 * We asked for two clusters, but only got one in the 1st
  	 * pass. Claim the 2nd cluster as a separate extent.
  	 */
  	if (alloc > len) {
1ed9b777f   Joel Becker   ocfs2: ocfs2_clai...
2916
  		ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2917
2918
2919
2920
2921
2922
  					   &len);
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
  		blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
cc79d8c19   Joel Becker   ocfs2: ocfs2_inse...
2923
  		ret = ocfs2_insert_extent(handle, &et, 1,
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
2924
  					  blkno, len, 0, NULL);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2925
2926
  		if (ret) {
  			mlog_errno(ret);
83cab5338   Tao Ma   ocfs2: Jump to co...
2927
  			goto out_commit;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2928
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2929
  		bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2930
2931
2932
2933
  	}
  
  	*first_block_bh = dirdata_bh;
  	dirdata_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2934
2935
  	if (ocfs2_supports_indexed_dirs(osb)) {
  		unsigned int off;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2936
2937
2938
2939
2940
  		if (!dx_inline) {
  			/*
  			 * We need to return the correct block within the
  			 * cluster which should hold our entry.
  			 */
1119d3c06   piaojun   ocfs2: use 'osb' ...
2941
  			off = ocfs2_dx_dir_hash_idx(osb,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
2942
2943
2944
2945
2946
2947
  						    &lookup->dl_hinfo);
  			get_bh(dx_leaves[off]);
  			lookup->dl_dx_leaf_bh = dx_leaves[off];
  		}
  		lookup->dl_dx_root_bh = dx_root_bh;
  		dx_root_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2948
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2949
2950
  
  out_commit:
a90714c15   Jan Kara   ocfs2: Add quota ...
2951
  	if (ret < 0 && did_quota)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
2952
  		dquot_free_space_nodirty(dir, bytes_allocated);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2953

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2954
  	ocfs2_commit_trans(osb, handle);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2955
  out:
edd45c084   Jan Kara   ocfs2: Correct or...
2956
  	up_write(&oi->ip_alloc_sem);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2957
2958
  	if (data_ac)
  		ocfs2_free_alloc_context(data_ac);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2959
2960
2961
2962
2963
2964
2965
2966
  	if (meta_ac)
  		ocfs2_free_alloc_context(meta_ac);
  
  	if (dx_leaves) {
  		for (i = 0; i < num_dx_leaves; i++)
  			brelse(dx_leaves[i]);
  		kfree(dx_leaves);
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2967
2968
  
  	brelse(dirdata_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
2969
  	brelse(dx_root_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
2970
2971
2972
  
  	return ret;
  }
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2973
  /* returns a bh of the 1st new block in the allocation. */
316f4b9f9   Mark Fasheh   ocfs2: Move direc...
2974
2975
2976
2977
2978
2979
2980
  static int ocfs2_do_extend_dir(struct super_block *sb,
  			       handle_t *handle,
  			       struct inode *dir,
  			       struct buffer_head *parent_fe_bh,
  			       struct ocfs2_alloc_context *data_ac,
  			       struct ocfs2_alloc_context *meta_ac,
  			       struct buffer_head **new_bh)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2981
2982
  {
  	int status;
a90714c15   Jan Kara   ocfs2: Add quota ...
2983
  	int extend, did_quota = 0;
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
2984
  	u64 p_blkno, v_blkno;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
2985
2986
2987
2988
2989
2990
  
  	spin_lock(&OCFS2_I(dir)->ip_lock);
  	extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
  	spin_unlock(&OCFS2_I(dir)->ip_lock);
  
  	if (extend) {
dcd0538ff   Mark Fasheh   ocfs2: sparse b-t...
2991
  		u32 offset = OCFS2_I(dir)->ip_clusters;
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
2992
2993
2994
  		status = dquot_alloc_space_nodirty(dir,
  					ocfs2_clusters_to_bytes(sb, 1));
  		if (status)
a90714c15   Jan Kara   ocfs2: Add quota ...
2995
  			goto bail;
a90714c15   Jan Kara   ocfs2: Add quota ...
2996
  		did_quota = 1;
0eb8d47e6   Tao Ma   ocfs2: Make high ...
2997
2998
2999
  		status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
  					      1, 0, parent_fe_bh, handle,
  					      data_ac, meta_ac, NULL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3000
3001
3002
3003
3004
3005
  		BUG_ON(status == -EAGAIN);
  		if (status < 0) {
  			mlog_errno(status);
  			goto bail;
  		}
  	}
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
3006
3007
  	v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
  	status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3008
3009
3010
3011
3012
3013
3014
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  
  	*new_bh = sb_getblk(sb, p_blkno);
  	if (!*new_bh) {
7391a294b   Rui Xiang   ocfs2: return ENO...
3015
  		status = -ENOMEM;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3016
3017
3018
3019
3020
  		mlog_errno(status);
  		goto bail;
  	}
  	status = 0;
  bail:
a90714c15   Jan Kara   ocfs2: Add quota ...
3021
  	if (did_quota && status < 0)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
3022
  		dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3023
3024
  	return status;
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3025
3026
3027
3028
3029
3030
3031
  /*
   * Assumes you already have a cluster lock on the directory.
   *
   * 'blocks_wanted' is only used if we have an inline directory which
   * is to be turned into an extent based one. The size of the dirent to
   * insert might be larger than the space gained by growing to just one
   * block, so we may have to grow the inode by two blocks in that case.
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3032
3033
   *
   * If the directory is already indexed, dx_root_bh must be provided.
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3034
   */
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3035
3036
3037
  static int ocfs2_extend_dir(struct ocfs2_super *osb,
  			    struct inode *dir,
  			    struct buffer_head *parent_fe_bh,
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3038
  			    unsigned int blocks_wanted,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3039
  			    struct ocfs2_dir_lookup_result *lookup,
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3040
3041
3042
  			    struct buffer_head **new_de_bh)
  {
  	int status = 0;
ee19a7795   Joel Becker   ocfs2: Wrap acces...
3043
  	int credits, num_free_extents, drop_alloc_sem = 0;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3044
3045
  	loff_t dir_i_size;
  	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
811f933df   Tao Ma   ocfs2: Use ocfs2_...
3046
  	struct ocfs2_extent_list *el = &fe->id2.i_list;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3047
3048
  	struct ocfs2_alloc_context *data_ac = NULL;
  	struct ocfs2_alloc_context *meta_ac = NULL;
1fabe1481   Mark Fasheh   ocfs2: Remove str...
3049
  	handle_t *handle = NULL;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3050
3051
3052
  	struct buffer_head *new_bh = NULL;
  	struct ocfs2_dir_entry * de;
  	struct super_block *sb = osb->sb;
f99b9b7cc   Joel Becker   ocfs2: Make ocfs2...
3053
  	struct ocfs2_extent_tree et;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3054
  	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3055

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3056
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3057
3058
3059
3060
3061
  		/*
  		 * This would be a code error as an inline directory should
  		 * never have an index root.
  		 */
  		BUG_ON(dx_root_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3062
  		status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3063
3064
  						 blocks_wanted, lookup,
  						 &new_bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3065
3066
3067
3068
  		if (status) {
  			mlog_errno(status);
  			goto bail;
  		}
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3069
3070
3071
  		/* Expansion from inline to an indexed directory will
  		 * have given us this. */
  		dx_root_bh = lookup->dl_dx_root_bh;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
  		if (blocks_wanted == 1) {
  			/*
  			 * If the new dirent will fit inside the space
  			 * created by pushing out to one block, then
  			 * we can complete the operation
  			 * here. Otherwise we have to expand i_size
  			 * and format the 2nd block below.
  			 */
  			BUG_ON(new_bh == NULL);
  			goto bail_bh;
  		}
  
  		/*
  		 * Get rid of 'new_bh' - we want to format the 2nd
  		 * data block and return that instead.
  		 */
  		brelse(new_bh);
  		new_bh = NULL;
edd45c084   Jan Kara   ocfs2: Correct or...
3090
3091
  		down_write(&OCFS2_I(dir)->ip_alloc_sem);
  		drop_alloc_sem = 1;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3092
3093
3094
3095
  		dir_i_size = i_size_read(dir);
  		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  		goto do_extend;
  	}
edd45c084   Jan Kara   ocfs2: Correct or...
3096
3097
  	down_write(&OCFS2_I(dir)->ip_alloc_sem);
  	drop_alloc_sem = 1;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3098
  	dir_i_size = i_size_read(dir);
f1088d471   Tao Ma   ocfs2: Remove mlo...
3099
3100
  	trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
  			       dir_i_size);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3101

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3102
3103
3104
3105
  	/* dir->i_size is always block aligned. */
  	spin_lock(&OCFS2_I(dir)->ip_lock);
  	if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
  		spin_unlock(&OCFS2_I(dir)->ip_lock);
5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
3106
3107
  		ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
  					      parent_fe_bh);
964f14a0d   Jun Piao   ocfs2: clean up s...
3108
  		num_free_extents = ocfs2_num_free_extents(&et);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3109
3110
3111
3112
3113
3114
3115
  		if (num_free_extents < 0) {
  			status = num_free_extents;
  			mlog_errno(status);
  			goto bail;
  		}
  
  		if (!num_free_extents) {
811f933df   Tao Ma   ocfs2: Use ocfs2_...
3116
  			status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3117
3118
3119
3120
3121
3122
  			if (status < 0) {
  				if (status != -ENOSPC)
  					mlog_errno(status);
  				goto bail;
  			}
  		}
da5cbf2f9   Mark Fasheh   ocfs2: don't use ...
3123
  		status = ocfs2_reserve_clusters(osb, 1, &data_ac);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3124
3125
3126
3127
3128
  		if (status < 0) {
  			if (status != -ENOSPC)
  				mlog_errno(status);
  			goto bail;
  		}
83f92318f   Mark Fasheh   ocfs2: Add dir_re...
3129
3130
  		if (ocfs2_dir_resv_allowed(osb))
  			data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
e3b4a97db   Mark Fasheh   ocfs2: use alloca...
3131

06f9da6e8   Goldwyn Rodrigues   fs/ocfs2: remove ...
3132
  		credits = ocfs2_calc_extend_credits(sb, el);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3133
3134
3135
3136
  	} else {
  		spin_unlock(&OCFS2_I(dir)->ip_lock);
  		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3137
  do_extend:
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3138
3139
3140
  	if (ocfs2_dir_indexed(dir))
  		credits++; /* For attaching the new dirent block to the
  			    * dx_root */
65eff9ccf   Mark Fasheh   ocfs2: remove han...
3141
  	handle = ocfs2_start_trans(osb, credits);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
  	if (IS_ERR(handle)) {
  		status = PTR_ERR(handle);
  		handle = NULL;
  		mlog_errno(status);
  		goto bail;
  	}
  
  	status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
  				     data_ac, meta_ac, &new_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
3155
  	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3156

0cf2f7632   Joel Becker   ocfs2: Pass struc...
3157
  	status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
13723d00e   Joel Becker   ocfs2: Use metada...
3158
  					 OCFS2_JOURNAL_ACCESS_CREATE);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3159
3160
3161
3162
3163
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
  	memset(new_bh->b_data, 0, sb->s_blocksize);
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3164

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3165
3166
  	de = (struct ocfs2_dir_entry *) new_bh->b_data;
  	de->inode = 0;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3167
  	if (ocfs2_supports_dir_trailer(dir)) {
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3168
  		de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
  
  		ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
  
  		if (ocfs2_dir_indexed(dir)) {
  			status = ocfs2_dx_dir_link_trailer(dir, handle,
  							   dx_root_bh, new_bh);
  			if (status) {
  				mlog_errno(status);
  				goto bail;
  			}
  		}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3180
3181
3182
  	} else {
  		de->rec_len = cpu_to_le16(sb->s_blocksize);
  	}
2931cdcb4   Darrick J. Wong   ocfs2: improve fs...
3183
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
3184
  	ocfs2_journal_dirty(handle, new_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3185
3186
3187
  
  	dir_i_size += dir->i_sb->s_blocksize;
  	i_size_write(dir, dir_i_size);
8110b073a   Mark Fasheh   ocfs2: Fix up i_b...
3188
  	dir->i_blocks = ocfs2_inode_sector_count(dir);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3189
3190
3191
3192
3193
  	status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
  	if (status < 0) {
  		mlog_errno(status);
  		goto bail;
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3194
  bail_bh:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3195
3196
3197
3198
  	*new_de_bh = new_bh;
  	get_bh(*new_de_bh);
  bail:
  	if (handle)
02dc1af44   Mark Fasheh   ocfs2: pass ocfs2...
3199
  		ocfs2_commit_trans(osb, handle);
edd45c084   Jan Kara   ocfs2: Correct or...
3200
3201
  	if (drop_alloc_sem)
  		up_write(&OCFS2_I(dir)->ip_alloc_sem);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3202
3203
3204
3205
3206
  
  	if (data_ac)
  		ocfs2_free_alloc_context(data_ac);
  	if (meta_ac)
  		ocfs2_free_alloc_context(meta_ac);
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
3207
  	brelse(new_bh);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3208

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3209
3210
  	return status;
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3211
3212
3213
3214
  static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
  				   const char *name, int namelen,
  				   struct buffer_head **ret_de_bh,
  				   unsigned int *blocks_wanted)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3215
  {
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3216
  	int ret;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3217
  	struct super_block *sb = dir->i_sb;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3218
3219
3220
3221
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  	struct ocfs2_dir_entry *de, *last_de = NULL;
  	char *de_buf, *limit;
  	unsigned long offset = 0;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3222
3223
3224
3225
3226
3227
  	unsigned int rec_len, new_rec_len, free_space = dir->i_sb->s_blocksize;
  
  	/*
  	 * This calculates how many free bytes we'd have in block zero, should
  	 * this function force expansion to an extent tree.
  	 */
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3228
  	if (ocfs2_new_dir_wants_trailer(dir))
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3229
3230
3231
  		free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
  	else
  		free_space = dir->i_sb->s_blocksize - i_size_read(dir);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3232
3233
3234
3235
  
  	de_buf = di->id2.i_data.id_data;
  	limit = de_buf + i_size_read(dir);
  	rec_len = OCFS2_DIR_REC_LEN(namelen);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3236

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3237
3238
  	while (de_buf < limit) {
  		de = (struct ocfs2_dir_entry *)de_buf;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3239

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3240
3241
3242
3243
3244
3245
3246
3247
  		if (!ocfs2_check_dir_entry(dir, de, di_bh, offset)) {
  			ret = -ENOENT;
  			goto out;
  		}
  		if (ocfs2_match(namelen, name, de)) {
  			ret = -EEXIST;
  			goto out;
  		}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3248
3249
3250
3251
  		/*
  		 * No need to check for a trailing dirent record here as
  		 * they're not used for inline dirs.
  		 */
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3252
3253
3254
3255
3256
3257
3258
3259
  		if (ocfs2_dirent_would_fit(de, rec_len)) {
  			/* Ok, we found a spot. Return this bh and let
  			 * the caller actually fill it in. */
  			*ret_de_bh = di_bh;
  			get_bh(*ret_de_bh);
  			ret = 0;
  			goto out;
  		}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3260

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3261
3262
3263
3264
  		last_de = de;
  		de_buf += le16_to_cpu(de->rec_len);
  		offset += le16_to_cpu(de->rec_len);
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3265

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3266
3267
3268
3269
3270
3271
  	/*
  	 * We're going to require expansion of the directory - figure
  	 * out how many blocks we'll need so that a place for the
  	 * dirent can be found.
  	 */
  	*blocks_wanted = 1;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3272
  	new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3273
3274
  	if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
  		*blocks_wanted = 2;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3275

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
  	ret = -ENOSPC;
  out:
  	return ret;
  }
  
  static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
  				   int namelen, struct buffer_head **ret_de_bh)
  {
  	unsigned long offset;
  	struct buffer_head *bh = NULL;
  	unsigned short rec_len;
  	struct ocfs2_dir_entry *de;
  	struct super_block *sb = dir->i_sb;
  	int status;
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3290
  	int blocksize = dir->i_sb->s_blocksize;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3291

a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
3292
  	status = ocfs2_read_dir_block(dir, 0, &bh, 0);
9b5726917   Daeseok Youn   ocfs2: removes ml...
3293
  	if (status)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3294
  		goto bail;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
  
  	rec_len = OCFS2_DIR_REC_LEN(namelen);
  	offset = 0;
  	de = (struct ocfs2_dir_entry *) bh->b_data;
  	while (1) {
  		if ((char *)de >= sb->s_blocksize + bh->b_data) {
  			brelse(bh);
  			bh = NULL;
  
  			if (i_size_read(dir) <= offset) {
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3305
3306
3307
3308
3309
  				/*
  				 * Caller will have to expand this
  				 * directory.
  				 */
  				status = -ENOSPC;
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3310
3311
  				goto bail;
  			}
a22305cc6   Joel Becker   ocfs2: Wrap dirbl...
3312
3313
3314
  			status = ocfs2_read_dir_block(dir,
  					     offset >> sb->s_blocksize_bits,
  					     &bh, 0);
9b5726917   Daeseok Youn   ocfs2: removes ml...
3315
  			if (status)
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3316
  				goto bail;
9b5726917   Daeseok Youn   ocfs2: removes ml...
3317

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
  			/* move to next block */
  			de = (struct ocfs2_dir_entry *) bh->b_data;
  		}
  		if (!ocfs2_check_dir_entry(dir, de, bh, offset)) {
  			status = -ENOENT;
  			goto bail;
  		}
  		if (ocfs2_match(namelen, name, de)) {
  			status = -EEXIST;
  			goto bail;
  		}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3329
3330
3331
3332
  
  		if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
  					   blocksize))
  			goto next;
8553cf4f3   Mark Fasheh   ocfs2: Cleanup di...
3333
  		if (ocfs2_dirent_would_fit(de, rec_len)) {
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3334
3335
3336
3337
3338
3339
3340
  			/* Ok, we found a spot. Return this bh and let
  			 * the caller actually fill it in. */
  			*ret_de_bh = bh;
  			get_bh(*ret_de_bh);
  			status = 0;
  			goto bail;
  		}
87d35a74b   Mark Fasheh   ocfs2: Add direct...
3341
  next:
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3342
3343
3344
  		offset += le16_to_cpu(de->rec_len);
  		de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
  	}
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3345
  bail:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
3346
  	brelse(bh);
c1e8d35ef   Tao Ma   ocfs2: Remove EXI...
3347
3348
  	if (status)
  		mlog_errno(status);
ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3349

ccd979bdb   Mark Fasheh   [PATCH] OCFS2: Th...
3350
3351
  	return status;
  }
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3352

9b7895efa   Mark Fasheh   ocfs2: Add a name...
3353
  static int dx_leaf_sort_cmp(const void *a, const void *b)
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
3354
  {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
  	const struct ocfs2_dx_entry *entry1 = a;
  	const struct ocfs2_dx_entry *entry2 = b;
  	u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
  	u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
  	u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
  	u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
  
  	if (major_hash1 > major_hash2)
  		return 1;
  	if (major_hash1 < major_hash2)
  		return -1;
  
  	/*
  	 * It is not strictly necessary to sort by minor
  	 */
  	if (minor_hash1 > minor_hash2)
  		return 1;
  	if (minor_hash1 < minor_hash2)
  		return -1;
  	return 0;
  }
  
  static void dx_leaf_sort_swap(void *a, void *b, int size)
  {
  	struct ocfs2_dx_entry *entry1 = a;
  	struct ocfs2_dx_entry *entry2 = b;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3381
3382
  
  	BUG_ON(size != sizeof(*entry1));
2a28f98c4   Fabian Frederick   ocfs2: use swap()...
3383
  	swap(*entry1, *entry2);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
  }
  
  static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
  {
  	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  	int i, num = le16_to_cpu(dl_list->de_num_used);
  
  	for (i = 0; i < (num - 1); i++) {
  		if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
  		    le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
  			return 0;
  	}
  
  	return 1;
  }
  
  /*
   * Find the optimal value to split this leaf on. This expects the leaf
   * entries to be in sorted order.
   *
   * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
   * the hash we want to insert.
   *
   * This function is only concerned with the major hash - that which
   * determines which cluster an item belongs to.
   */
  static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
  					u32 leaf_cpos, u32 insert_hash,
  					u32 *split_hash)
  {
  	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
  	int i, num_used = le16_to_cpu(dl_list->de_num_used);
  	int allsame;
  
  	/*
  	 * There's a couple rare, but nasty corner cases we have to
  	 * check for here. All of them involve a leaf where all value
  	 * have the same hash, which is what we look for first.
  	 *
  	 * Most of the time, all of the above is false, and we simply
  	 * pick the median value for a split.
  	 */
  	allsame = ocfs2_dx_leaf_same_major(dx_leaf);
  	if (allsame) {
  		u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
  
  		if (val == insert_hash) {
  			/*
  			 * No matter where we would choose to split,
  			 * the new entry would want to occupy the same
  			 * block as these. Since there's no space left
  			 * in their existing block, we know there
  			 * won't be space after the split.
  			 */
  			return -ENOSPC;
  		}
  
  		if (val == leaf_cpos) {
  			/*
  			 * Because val is the same as leaf_cpos (which
  			 * is the smallest value this leaf can have),
  			 * yet is not equal to insert_hash, then we
  			 * know that insert_hash *must* be larger than
  			 * val (and leaf_cpos). At least cpos+1 in value.
  			 *
  			 * We also know then, that there cannot be an
  			 * adjacent extent (otherwise we'd be looking
  			 * at it). Choosing this value gives us a
  			 * chance to get some contiguousness.
  			 */
  			*split_hash = leaf_cpos + 1;
  			return 0;
  		}
  
  		if (val > insert_hash) {
  			/*
  			 * val can not be the same as insert hash, and
  			 * also must be larger than leaf_cpos. Also,
  			 * we know that there can't be a leaf between
  			 * cpos and val, otherwise the entries with
  			 * hash 'val' would be there.
  			 */
  			*split_hash = val;
  			return 0;
  		}
  
  		*split_hash = insert_hash;
  		return 0;
  	}
  
  	/*
  	 * Since the records are sorted and the checks above
  	 * guaranteed that not all records in this block are the same,
  	 * we simple travel forward, from the median, and pick the 1st
  	 * record whose value is larger than leaf_cpos.
  	 */
  	for (i = (num_used / 2); i < num_used; i++)
  		if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
  		    leaf_cpos)
  			break;
  
  	BUG_ON(i == num_used); /* Should be impossible */
  	*split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
  	return 0;
  }
  
  /*
   * Transfer all entries in orig_dx_leaves whose major hash is equal to or
   * larger than split_hash into new_dx_leaves. We use a temporary
   * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
   *
   * Since the block offset inside a leaf (cluster) is a constant mask
   * of minor_hash, we can optimize - an item at block offset X within
   * the original cluster, will be at offset X within the new cluster.
   */
  static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
  				       handle_t *handle,
  				       struct ocfs2_dx_leaf *tmp_dx_leaf,
  				       struct buffer_head **orig_dx_leaves,
  				       struct buffer_head **new_dx_leaves,
  				       int num_dx_leaves)
  {
  	int i, j, num_used;
  	u32 major_hash;
  	struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
77461ba1d   zhengbin   fs/ocfs2/dir.c: r...
3509
  	struct ocfs2_dx_entry_list *orig_list, *tmp_list;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3510
3511
3512
3513
3514
3515
3516
3517
  	struct ocfs2_dx_entry *dx_entry;
  
  	tmp_list = &tmp_dx_leaf->dl_list;
  
  	for (i = 0; i < num_dx_leaves; i++) {
  		orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
  		orig_list = &orig_dx_leaf->dl_list;
  		new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
  
  		num_used = le16_to_cpu(orig_list->de_num_used);
  
  		memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
  		tmp_list->de_num_used = cpu_to_le16(0);
  		memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
  
  		for (j = 0; j < num_used; j++) {
  			dx_entry = &orig_list->de_entries[j];
  			major_hash = le32_to_cpu(dx_entry->dx_major_hash);
  			if (major_hash >= split_hash)
  				ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
  							      dx_entry);
  			else
  				ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
  							      dx_entry);
  		}
  		memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
  
  		ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
  		ocfs2_journal_dirty(handle, new_dx_leaves[i]);
  	}
  }
  
  static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
  					  struct ocfs2_dx_root_block *dx_root)
  {
d006c71f8   Junxiao Bi   ocfs2: fix not en...
3545
  	int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3546

06f9da6e8   Goldwyn Rodrigues   fs/ocfs2: remove ...
3547
  	credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
  	credits += ocfs2_quota_trans_credits(osb->sb);
  	return credits;
  }
  
  /*
   * Find the median value in dx_leaf_bh and allocate a new leaf to move
   * half our entries into.
   */
  static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
  				  struct buffer_head *dx_root_bh,
  				  struct buffer_head *dx_leaf_bh,
  				  struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
  				  u64 leaf_blkno)
  {
  	struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  	int credits, ret, i, num_used, did_quota = 0;
  	u32 cpos, split_hash, insert_hash = hinfo->major_hash;
  	u64 orig_leaves_start;
  	int num_dx_leaves;
  	struct buffer_head **orig_dx_leaves = NULL;
  	struct buffer_head **new_dx_leaves = NULL;
  	struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
  	struct ocfs2_extent_tree et;
  	handle_t *handle = NULL;
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
f1088d471   Tao Ma   ocfs2: Remove mlo...
3574
3575
3576
  	trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
  				     (unsigned long long)leaf_blkno,
  				     insert_hash);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3577

5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
3578
  	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
  
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  	/*
  	 * XXX: This is a rather large limit. We should use a more
  	 * realistic value.
  	 */
  	if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
  		return -ENOSPC;
  
  	num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
  	if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
  		mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
  		     "%llu, %d
  ", (unsigned long long)OCFS2_I(dir)->ip_blkno,
  		     (unsigned long long)leaf_blkno, num_used);
  		ret = -EIO;
  		goto out;
  	}
  
  	orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  	if (!orig_dx_leaves) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
  	if (!new_dx_leaves) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
  	if (ret) {
  		if (ret != -ENOSPC)
  			mlog_errno(ret);
  		goto out;
  	}
  
  	credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
  	handle = ocfs2_start_trans(osb, credits);
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		handle = NULL;
  		mlog_errno(ret);
  		goto out;
  	}
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
3627
3628
3629
  	ret = dquot_alloc_space_nodirty(dir,
  				       ocfs2_clusters_to_bytes(dir->i_sb, 1));
  	if (ret)
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3630
  		goto out_commit;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3631
  	did_quota = 1;
0cf2f7632   Joel Becker   ocfs2: Pass struc...
3632
  	ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
  
  	/*
  	 * This block is changing anyway, so we can sort it in place.
  	 */
  	sort(dx_leaf->dl_list.de_entries, num_used,
  	     sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
  	     dx_leaf_sort_swap);
ec20cec7a   Joel Becker   ocfs2: Make ocfs2...
3645
  	ocfs2_journal_dirty(handle, dx_leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3646
3647
3648
3649
3650
3651
3652
  
  	ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
  					   &split_hash);
  	if (ret) {
  		mlog_errno(ret);
  		goto  out_commit;
  	}
f1088d471   Tao Ma   ocfs2: Remove mlo...
3653
  	trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
  
  	/*
  	 * We have to carefully order operations here. There are items
  	 * which want to be in the new cluster before insert, but in
  	 * order to put those items in the new cluster, we alter the
  	 * old cluster. A failure to insert gets nasty.
  	 *
  	 * So, start by reserving writes to the old
  	 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
  	 * the new cluster for us, before inserting it. The insert
  	 * won't happen if there's an error before that. Once the
  	 * insert is done then, we can transfer from one leaf into the
  	 * other without fear of hitting any error.
  	 */
  
  	/*
  	 * The leaf transfer wants some scratch space so that we don't
  	 * wind up doing a bunch of expensive memmove().
  	 */
  	tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
  	if (!tmp_dx_leaf) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out_commit;
  	}
1d46dc08d   Mark Fasheh   ocfs2: fix leaf s...
3679
  	orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3680
3681
3682
3683
3684
3685
  	ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
  				   orig_dx_leaves);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
0f4da216b   Tristan Ye   Ocfs2: Re-access ...
3686
3687
3688
3689
3690
3691
3692
3693
  	cpos = split_hash;
  	ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
  				       data_ac, meta_ac, new_dx_leaves,
  				       num_dx_leaves);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3694
  	for (i = 0; i < num_dx_leaves; i++) {
0cf2f7632   Joel Becker   ocfs2: Pass struc...
3695
3696
  		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  					      orig_dx_leaves[i],
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3697
3698
3699
3700
3701
  					      OCFS2_JOURNAL_ACCESS_WRITE);
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3702

0f4da216b   Tristan Ye   Ocfs2: Re-access ...
3703
3704
3705
3706
3707
3708
3709
  		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
  					      new_dx_leaves[i],
  					      OCFS2_JOURNAL_ACCESS_WRITE);
  		if (ret) {
  			mlog_errno(ret);
  			goto out_commit;
  		}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3710
3711
3712
3713
3714
3715
3716
  	}
  
  	ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
  				   orig_dx_leaves, new_dx_leaves, num_dx_leaves);
  
  out_commit:
  	if (ret < 0 && did_quota)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
3717
  		dquot_free_space_nodirty(dir,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3718
  				ocfs2_clusters_to_bytes(dir->i_sb, 1));
2931cdcb4   Darrick J. Wong   ocfs2: improve fs...
3719
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
  	ocfs2_commit_trans(osb, handle);
  
  out:
  	if (orig_dx_leaves || new_dx_leaves) {
  		for (i = 0; i < num_dx_leaves; i++) {
  			if (orig_dx_leaves)
  				brelse(orig_dx_leaves[i]);
  			if (new_dx_leaves)
  				brelse(new_dx_leaves[i]);
  		}
  		kfree(orig_dx_leaves);
  		kfree(new_dx_leaves);
  	}
  
  	if (meta_ac)
  		ocfs2_free_alloc_context(meta_ac);
  	if (data_ac)
  		ocfs2_free_alloc_context(data_ac);
  
  	kfree(tmp_dx_leaf);
  	return ret;
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
  static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
  				   struct buffer_head *di_bh,
  				   struct buffer_head *dx_root_bh,
  				   const char *name, int namelen,
  				   struct ocfs2_dir_lookup_result *lookup)
  {
  	int ret, rebalanced = 0;
  	struct ocfs2_dx_root_block *dx_root;
  	struct buffer_head *dx_leaf_bh = NULL;
  	struct ocfs2_dx_leaf *dx_leaf;
  	u64 blkno;
  	u32 leaf_cpos;
  
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  
  restart_search:
  	ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
  				  &leaf_cpos, &blkno);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
  
  	if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
  	    le16_to_cpu(dx_leaf->dl_list.de_count)) {
  		if (rebalanced) {
  			/*
  			 * Rebalancing should have provided us with
  			 * space in an appropriate leaf.
  			 *
  			 * XXX: Is this an abnormal condition then?
  			 * Should we print a message here?
  			 */
  			ret = -ENOSPC;
  			goto out;
  		}
  
  		ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
  					     &lookup->dl_hinfo, leaf_cpos,
  					     blkno);
  		if (ret) {
  			if (ret != -ENOSPC)
  				mlog_errno(ret);
  			goto out;
  		}
  
  		/*
  		 * Restart the lookup. The rebalance might have
  		 * changed which block our item fits into. Mark our
  		 * progress, so we only execute this once.
  		 */
  		brelse(dx_leaf_bh);
  		dx_leaf_bh = NULL;
  		rebalanced = 1;
  		goto restart_search;
  	}
  
  	lookup->dl_dx_leaf_bh = dx_leaf_bh;
  	dx_leaf_bh = NULL;
  
  out:
  	brelse(dx_leaf_bh);
  	return ret;
  }
  
  static int ocfs2_search_dx_free_list(struct inode *dir,
  				     struct buffer_head *dx_root_bh,
  				     int namelen,
  				     struct ocfs2_dir_lookup_result *lookup)
  {
  	int ret = -ENOSPC;
  	struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
  	struct ocfs2_dir_block_trailer *db;
  	u64 next_block;
  	int rec_len = OCFS2_DIR_REC_LEN(namelen);
  	struct ocfs2_dx_root_block *dx_root;
  
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
  	next_block = le64_to_cpu(dx_root->dr_free_blk);
  
  	while (next_block) {
  		brelse(prev_leaf_bh);
  		prev_leaf_bh = leaf_bh;
  		leaf_bh = NULL;
  
  		ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
  		if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
  			lookup->dl_leaf_bh = leaf_bh;
  			lookup->dl_prev_leaf_bh = prev_leaf_bh;
  			leaf_bh = NULL;
  			prev_leaf_bh = NULL;
  			break;
  		}
  
  		next_block = le64_to_cpu(db->db_free_next);
  	}
  
  	if (!next_block)
  		ret = -ENOSPC;
  
  out:
  
  	brelse(leaf_bh);
  	brelse(prev_leaf_bh);
  	return ret;
  }
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
  static int ocfs2_expand_inline_dx_root(struct inode *dir,
  				       struct buffer_head *dx_root_bh)
  {
  	int ret, num_dx_leaves, i, j, did_quota = 0;
  	struct buffer_head **dx_leaves = NULL;
  	struct ocfs2_extent_tree et;
  	u64 insert_blkno;
  	struct ocfs2_alloc_context *data_ac = NULL;
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  	handle_t *handle = NULL;
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dx_entry_list *entry_list;
  	struct ocfs2_dx_entry *dx_entry;
  	struct ocfs2_dx_leaf *target_leaf;
  
  	ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
  	if (!dx_leaves) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out;
  	}
  
  	handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		mlog_errno(ret);
  		goto out;
  	}
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
3896
3897
3898
  	ret = dquot_alloc_space_nodirty(dir,
  				       ocfs2_clusters_to_bytes(osb->sb, 1));
  	if (ret)
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3899
  		goto out_commit;
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3900
3901
3902
3903
3904
3905
3906
  	did_quota = 1;
  
  	/*
  	 * We do this up front, before the allocation, so that a
  	 * failure to add the dx_root_bh to the journal won't result
  	 * us losing clusters.
  	 */
0cf2f7632   Joel Becker   ocfs2: Pass struc...
3907
  	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
  
  	ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
  					 num_dx_leaves, &insert_blkno);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
  
  	/*
  	 * Transfer the entries from our dx_root into the appropriate
  	 * block
  	 */
  	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  	entry_list = &dx_root->dr_entries;
  
  	for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
  		dx_entry = &entry_list->de_entries[i];
  
  		j = __ocfs2_dx_dir_hash_idx(osb,
  					    le32_to_cpu(dx_entry->dx_minor_hash));
  		target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
  
  		ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
  
  		/* Each leaf has been passed to the journal already
  		 * via __ocfs2_dx_dir_new_cluster() */
  	}
  
  	dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
  	memset(&dx_root->dr_list, 0, osb->sb->s_blocksize -
  	       offsetof(struct ocfs2_dx_root_block, dr_list));
  	dx_root->dr_list.l_count =
  		cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
  
  	/* This should never fail considering we start with an empty
  	 * dx_root. */
5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
3949
  	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
cc79d8c19   Joel Becker   ocfs2: ocfs2_inse...
3950
  	ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3951
3952
3953
  	if (ret)
  		mlog_errno(ret);
  	did_quota = 0;
2931cdcb4   Darrick J. Wong   ocfs2: improve fs...
3954
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3955
3956
3957
3958
  	ocfs2_journal_dirty(handle, dx_root_bh);
  
  out_commit:
  	if (ret < 0 && did_quota)
5dd4056db   Christoph Hellwig   dquot: cleanup sp...
3959
  		dquot_free_space_nodirty(dir,
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
  					  ocfs2_clusters_to_bytes(dir->i_sb, 1));
  
  	ocfs2_commit_trans(osb, handle);
  
  out:
  	if (data_ac)
  		ocfs2_free_alloc_context(data_ac);
  
  	if (dx_leaves) {
  		for (i = 0; i < num_dx_leaves; i++)
  			brelse(dx_leaves[i]);
  		kfree(dx_leaves);
  	}
  	return ret;
  }
  
  static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
  {
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dx_entry_list *entry_list;
  
  	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  	entry_list = &dx_root->dr_entries;
  
  	if (le16_to_cpu(entry_list->de_num_used) >=
  	    le16_to_cpu(entry_list->de_count))
  		return -ENOSPC;
  
  	return 0;
  }
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3990
3991
3992
3993
3994
  static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
  					   struct buffer_head *di_bh,
  					   const char *name,
  					   int namelen,
  					   struct ocfs2_dir_lookup_result *lookup)
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3995
  {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3996
3997
  	int ret, free_dx_root = 1;
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
3998
  	struct buffer_head *dx_root_bh = NULL;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
3999
  	struct buffer_head *leaf_bh = NULL;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4000
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4001
  	struct ocfs2_dx_root_block *dx_root;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4002
4003
4004
4005
4006
4007
4008
4009
  
  	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
e3a93c2db   Mark Fasheh   ocfs2: Add total ...
4010
4011
4012
4013
4014
  	if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
  		ret = -ENOSPC;
  		mlog_errno(ret);
  		goto out;
  	}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
  	if (ocfs2_dx_root_inline(dx_root)) {
  		ret = ocfs2_inline_dx_has_space(dx_root_bh);
  
  		if (ret == 0)
  			goto search_el;
  
  		/*
  		 * We ran out of room in the root block. Expand it to
  		 * an extent, then allow ocfs2_find_dir_space_dx to do
  		 * the rest.
  		 */
  		ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4032

e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4033
4034
4035
4036
4037
4038
4039
4040
  	/*
  	 * Insert preparation for an indexed directory is split into two
  	 * steps. The call to find_dir_space_dx reserves room in the index for
  	 * an additional item. If we run out of space there, it's a real error
  	 * we can't continue on.
  	 */
  	ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
  				      namelen, lookup);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4041
4042
4043
4044
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4045
4046
4047
4048
4049
4050
4051
4052
4053
  search_el:
  	/*
  	 * Next, we need to find space in the unindexed tree. This call
  	 * searches using the free space linked list. If the unindexed tree
  	 * lacks sufficient space, we'll expand it below. The expansion code
  	 * is smart enough to add any new blocks to the free space list.
  	 */
  	ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
  	if (ret && ret != -ENOSPC) {
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4054
4055
4056
  		mlog_errno(ret);
  		goto out;
  	}
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4057
4058
4059
  	/* Do this up here - ocfs2_extend_dir might need the dx_root */
  	lookup->dl_dx_root_bh = dx_root_bh;
  	free_dx_root = 0;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4060

e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4061
4062
  	if (ret == -ENOSPC) {
  		ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4063

9b7895efa   Mark Fasheh   ocfs2: Add a name...
4064
  		if (ret) {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4065
  			mlog_errno(ret);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4066
4067
4068
4069
  			goto out;
  		}
  
  		/*
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4070
4071
  		 * We make the assumption here that new leaf blocks are added
  		 * to the front of our free list.
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4072
  		 */
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4073
4074
  		lookup->dl_prev_leaf_bh = NULL;
  		lookup->dl_leaf_bh = leaf_bh;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4075
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4076
  out:
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4077
4078
  	if (free_dx_root)
  		brelse(dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
  	return ret;
  }
  
  /*
   * Get a directory ready for insert. Any directory allocation required
   * happens here. Success returns zero, and enough context in the dir
   * lookup result that ocfs2_add_entry() will be able complete the task
   * with minimal performance impact.
   */
  int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
  				 struct inode *dir,
  				 struct buffer_head *parent_fe_bh,
  				 const char *name,
  				 int namelen,
  				 struct ocfs2_dir_lookup_result *lookup)
  {
  	int ret;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4096
4097
  	unsigned int blocks_wanted = 1;
  	struct buffer_head *bh = NULL;
f1088d471   Tao Ma   ocfs2: Remove mlo...
4098
4099
  	trace_ocfs2_prepare_dir_for_insert(
  		(unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4100

5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4101
4102
4103
4104
4105
  	if (!namelen) {
  		ret = -EINVAL;
  		mlog_errno(ret);
  		goto out;
  	}
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
  	/*
  	 * Do this up front to reduce confusion.
  	 *
  	 * The directory might start inline, then be turned into an
  	 * indexed one, in which case we'd need to hash deep inside
  	 * ocfs2_find_dir_space_id(). Since
  	 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
  	 * done, there seems no point in spreading out the calls. We
  	 * can optimize away the case where the file system doesn't
  	 * support indexing.
  	 */
  	if (ocfs2_supports_indexed_dirs(osb))
  		ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
  
  	if (ocfs2_dir_indexed(dir)) {
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4121
4122
4123
  		ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
  						      name, namelen, lookup);
  		if (ret)
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4124
  			mlog_errno(ret);
e7c17e430   Mark Fasheh   ocfs2: Introduce ...
4125
  		goto out;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4126
  	}
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
  	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  		ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
  					      namelen, &bh, &blocks_wanted);
  	} else
  		ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
  
  	if (ret && ret != -ENOSPC) {
  		mlog_errno(ret);
  		goto out;
  	}
  
  	if (ret == -ENOSPC) {
  		/*
  		 * We have to expand the directory to add this name.
  		 */
  		BUG_ON(bh);
  
  		ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4145
  				       lookup, &bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4146
4147
4148
4149
4150
4151
4152
4153
  		if (ret) {
  			if (ret != -ENOSPC)
  				mlog_errno(ret);
  			goto out;
  		}
  
  		BUG_ON(!bh);
  	}
4a12ca3a0   Mark Fasheh   ocfs2: Introduce ...
4154
  	lookup->dl_leaf_bh = bh;
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4155
4156
  	bh = NULL;
  out:
a81cb88b6   Mark Fasheh   ocfs2: Don't chec...
4157
  	brelse(bh);
5b6a3a2b4   Mark Fasheh   ocfs2: Write supp...
4158
4159
  	return ret;
  }
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
  
  static int ocfs2_dx_dir_remove_index(struct inode *dir,
  				     struct buffer_head *di_bh,
  				     struct buffer_head *dx_root_bh)
  {
  	int ret;
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  	struct ocfs2_dx_root_block *dx_root;
  	struct inode *dx_alloc_inode = NULL;
  	struct buffer_head *dx_alloc_bh = NULL;
  	handle_t *handle;
  	u64 blk;
  	u16 bit;
  	u64 bg_blkno;
  
  	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
  
  	dx_alloc_inode = ocfs2_get_system_file_inode(osb,
  					EXTENT_ALLOC_SYSTEM_INODE,
  					le16_to_cpu(dx_root->dr_suballoc_slot));
  	if (!dx_alloc_inode) {
  		ret = -ENOMEM;
  		mlog_errno(ret);
  		goto out;
  	}
5955102c9   Al Viro   wrappers for ->i_...
4186
  	inode_lock(dx_alloc_inode);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
  
  	ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_mutex;
  	}
  
  	handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
  	if (IS_ERR(handle)) {
  		ret = PTR_ERR(handle);
  		mlog_errno(ret);
  		goto out_unlock;
  	}
0cf2f7632   Joel Becker   ocfs2: Pass struc...
4200
  	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4201
4202
4203
4204
4205
  				      OCFS2_JOURNAL_ACCESS_WRITE);
  	if (ret) {
  		mlog_errno(ret);
  		goto out_commit;
  	}
8ac33dc86   Tao Ma   ocfs2: Hold ip_lo...
4206
  	spin_lock(&OCFS2_I(dir)->ip_lock);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4207
4208
  	OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
  	di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
8ac33dc86   Tao Ma   ocfs2: Hold ip_lo...
4209
  	spin_unlock(&OCFS2_I(dir)->ip_lock);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4210
  	di->i_dx_root = cpu_to_le64(0ULL);
6fdb702d6   Darrick J. Wong   ocfs2: call ocfs2...
4211
  	ocfs2_update_inode_fsync_trans(handle, dir, 1);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4212
4213
4214
4215
4216
  
  	ocfs2_journal_dirty(handle, di_bh);
  
  	blk = le64_to_cpu(dx_root->dr_blkno);
  	bit = le16_to_cpu(dx_root->dr_suballoc_bit);
74380c479   Tao Ma   ocfs2: Free block...
4217
4218
4219
4220
  	if (dx_root->dr_suballoc_loc)
  		bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
  	else
  		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
  	ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
  				       bit, bg_blkno, 1);
  	if (ret)
  		mlog_errno(ret);
  
  out_commit:
  	ocfs2_commit_trans(osb, handle);
  
  out_unlock:
  	ocfs2_inode_unlock(dx_alloc_inode, 1);
  
  out_mutex:
5955102c9   Al Viro   wrappers for ->i_...
4233
  	inode_unlock(dx_alloc_inode);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4234
4235
4236
4237
4238
4239
4240
4241
4242
  	brelse(dx_alloc_bh);
  out:
  	iput(dx_alloc_inode);
  	return ret;
  }
  
  int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
  {
  	int ret;
3f649ab72   Kees Cook   treewide: Remove ...
4243
4244
4245
  	unsigned int clen;
  	u32 major_hash = UINT_MAX, p_cpos, cpos;
  	u64 blkno;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
  	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
  	struct buffer_head *dx_root_bh = NULL;
  	struct ocfs2_dx_root_block *dx_root;
  	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  	struct ocfs2_cached_dealloc_ctxt dealloc;
  	struct ocfs2_extent_tree et;
  
  	ocfs2_init_dealloc_ctxt(&dealloc);
  
  	if (!ocfs2_dir_indexed(dir))
  		return 0;
  
  	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
4263
  	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4264

4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
4265
4266
  	if (ocfs2_dx_root_inline(dx_root))
  		goto remove_index;
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4267

5e404e9ed   Joel Becker   ocfs2: Pass ocfs2...
4268
  	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
  
  	/* XXX: What if dr_clusters is too large? */
  	while (le32_to_cpu(dx_root->dr_clusters)) {
  		ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
  					      major_hash, &cpos, &blkno, &clen);
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
78f94673d   Tristan Ye   Ocfs2: Optimize o...
4280
  		ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
f62f12b3a   Junxiao Bi   ocfs2: reflink: f...
4281
  					       &dealloc, 0, false);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
  		if (ret) {
  			mlog_errno(ret);
  			goto out;
  		}
  
  		if (cpos == 0)
  			break;
  
  		major_hash = cpos - 1;
  	}
4ed8a6bb0   Mark Fasheh   ocfs2: Store dir ...
4292
  remove_index:
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4293
4294
4295
4296
4297
  	ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
  	if (ret) {
  		mlog_errno(ret);
  		goto out;
  	}
8cb471e8f   Joel Becker   ocfs2: Take the i...
4298
  	ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
9b7895efa   Mark Fasheh   ocfs2: Add a name...
4299
4300
4301
4302
4303
4304
4305
  out:
  	ocfs2_schedule_truncate_log_flush(osb, 1);
  	ocfs2_run_deallocs(osb, &dealloc);
  
  	brelse(dx_root_bh);
  	return ret;
  }