xref: /linux/fs/ocfs2/dir.c (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * dir.c
4  *
5  * Creates, reads, walks and deletes directory-nodes
6  *
7  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
8  *
9  *  Portions of this code from linux/fs/ext3/dir.c
10  *
11  *  Copyright (C) 1992, 1993, 1994, 1995
12  *  Remy Card (card@masi.ibp.fr)
13  *  Laboratoire MASI - Institut Blaise pascal
14  *  Universite Pierre et Marie Curie (Paris VI)
15  *
16  *   from
17  *
18  *   linux/fs/minix/dir.c
19  *
20  *   Copyright (C) 1991, 1992 Linus Torvalds
21  */
22 
23 #include <linux/fs.h>
24 #include <linux/types.h>
25 #include <linux/slab.h>
26 #include <linux/highmem.h>
27 #include <linux/quotaops.h>
28 #include <linux/sort.h>
29 #include <linux/iversion.h>
30 
31 #include <cluster/masklog.h>
32 
33 #include "ocfs2.h"
34 
35 #include "alloc.h"
36 #include "blockcheck.h"
37 #include "dir.h"
38 #include "dlmglue.h"
39 #include "extent_map.h"
40 #include "file.h"
41 #include "inode.h"
42 #include "journal.h"
43 #include "namei.h"
44 #include "suballoc.h"
45 #include "super.h"
46 #include "sysfile.h"
47 #include "uptodate.h"
48 #include "ocfs2_trace.h"
49 
50 #include "buffer_head_io.h"
51 
52 #define NAMEI_RA_CHUNKS  2
53 #define NAMEI_RA_BLOCKS  4
54 #define NAMEI_RA_SIZE        (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS)
55 
56 static int ocfs2_do_extend_dir(struct super_block *sb,
57 			       handle_t *handle,
58 			       struct inode *dir,
59 			       struct buffer_head *parent_fe_bh,
60 			       struct ocfs2_alloc_context *data_ac,
61 			       struct ocfs2_alloc_context *meta_ac,
62 			       struct buffer_head **new_bh);
63 static int ocfs2_dir_indexed(struct inode *inode);
64 
65 /*
66  * These are distinct checks because future versions of the file system will
67  * want to have a trailing dirent structure independent of indexing.
68  */
ocfs2_supports_dir_trailer(struct inode * dir)69 static int ocfs2_supports_dir_trailer(struct inode *dir)
70 {
71 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
72 
73 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
74 		return 0;
75 
76 	return ocfs2_meta_ecc(osb) || ocfs2_dir_indexed(dir);
77 }
78 
79 /*
80  * "new' here refers to the point at which we're creating a new
81  * directory via "mkdir()", but also when we're expanding an inline
82  * directory. In either case, we don't yet have the indexing bit set
83  * on the directory, so the standard checks will fail in when metaecc
84  * is turned off. Only directory-initialization type functions should
85  * use this then. Everything else wants ocfs2_supports_dir_trailer()
86  */
ocfs2_new_dir_wants_trailer(struct inode * dir)87 static int ocfs2_new_dir_wants_trailer(struct inode *dir)
88 {
89 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
90 
91 	return ocfs2_meta_ecc(osb) ||
92 		ocfs2_supports_indexed_dirs(osb);
93 }
94 
ocfs2_dir_trailer_blk_off(struct super_block * sb)95 static inline unsigned int ocfs2_dir_trailer_blk_off(struct super_block *sb)
96 {
97 	return sb->s_blocksize - sizeof(struct ocfs2_dir_block_trailer);
98 }
99 
100 #define ocfs2_trailer_from_bh(_bh, _sb) ((struct ocfs2_dir_block_trailer *) ((_bh)->b_data + ocfs2_dir_trailer_blk_off((_sb))))
101 
102 /* XXX ocfs2_block_dqtrailer() is similar but not quite - can we make
103  * them more consistent? */
ocfs2_dir_trailer_from_size(int blocksize,void * data)104 struct ocfs2_dir_block_trailer *ocfs2_dir_trailer_from_size(int blocksize,
105 							    void *data)
106 {
107 	char *p = data;
108 
109 	p += blocksize - sizeof(struct ocfs2_dir_block_trailer);
110 	return (struct ocfs2_dir_block_trailer *)p;
111 }
112 
113 /*
114  * XXX: This is executed once on every dirent. We should consider optimizing
115  * it.
116  */
ocfs2_skip_dir_trailer(struct inode * dir,struct ocfs2_dir_entry * de,unsigned long offset,unsigned long blklen)117 static int ocfs2_skip_dir_trailer(struct inode *dir,
118 				  struct ocfs2_dir_entry *de,
119 				  unsigned long offset,
120 				  unsigned long blklen)
121 {
122 	unsigned long toff = blklen - sizeof(struct ocfs2_dir_block_trailer);
123 
124 	if (!ocfs2_supports_dir_trailer(dir))
125 		return 0;
126 
127 	if (offset != toff)
128 		return 0;
129 
130 	return 1;
131 }
132 
ocfs2_init_dir_trailer(struct inode * inode,struct buffer_head * bh,u16 rec_len)133 static void ocfs2_init_dir_trailer(struct inode *inode,
134 				   struct buffer_head *bh, u16 rec_len)
135 {
136 	struct ocfs2_dir_block_trailer *trailer;
137 
138 	trailer = ocfs2_trailer_from_bh(bh, inode->i_sb);
139 	strcpy(trailer->db_signature, OCFS2_DIR_TRAILER_SIGNATURE);
140 	trailer->db_compat_rec_len =
141 			cpu_to_le16(sizeof(struct ocfs2_dir_block_trailer));
142 	trailer->db_parent_dinode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
143 	trailer->db_blkno = cpu_to_le64(bh->b_blocknr);
144 	trailer->db_free_rec_len = cpu_to_le16(rec_len);
145 }
146 /*
147  * Link an unindexed block with a dir trailer structure into the index free
148  * list. This function will modify dirdata_bh, but assumes you've already
149  * passed it to the journal.
150  */
ocfs2_dx_dir_link_trailer(struct inode * dir,handle_t * handle,struct buffer_head * dx_root_bh,struct buffer_head * dirdata_bh)151 static int ocfs2_dx_dir_link_trailer(struct inode *dir, handle_t *handle,
152 				     struct buffer_head *dx_root_bh,
153 				     struct buffer_head *dirdata_bh)
154 {
155 	int ret;
156 	struct ocfs2_dx_root_block *dx_root;
157 	struct ocfs2_dir_block_trailer *trailer;
158 
159 	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
160 				      OCFS2_JOURNAL_ACCESS_WRITE);
161 	if (ret) {
162 		mlog_errno(ret);
163 		goto out;
164 	}
165 	trailer = ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
166 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
167 
168 	trailer->db_free_next = dx_root->dr_free_blk;
169 	dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
170 
171 	ocfs2_journal_dirty(handle, dx_root_bh);
172 
173 out:
174 	return ret;
175 }
176 
ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result * res)177 static int ocfs2_free_list_at_root(struct ocfs2_dir_lookup_result *res)
178 {
179 	return res->dl_prev_leaf_bh == NULL;
180 }
181 
ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result * res)182 void ocfs2_free_dir_lookup_result(struct ocfs2_dir_lookup_result *res)
183 {
184 	brelse(res->dl_dx_root_bh);
185 	brelse(res->dl_leaf_bh);
186 	brelse(res->dl_dx_leaf_bh);
187 	brelse(res->dl_prev_leaf_bh);
188 }
189 
ocfs2_dir_indexed(struct inode * inode)190 static int ocfs2_dir_indexed(struct inode *inode)
191 {
192 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INDEXED_DIR_FL)
193 		return 1;
194 	return 0;
195 }
196 
ocfs2_dx_root_inline(struct ocfs2_dx_root_block * dx_root)197 static inline int ocfs2_dx_root_inline(struct ocfs2_dx_root_block *dx_root)
198 {
199 	return dx_root->dr_flags & OCFS2_DX_FLAG_INLINE;
200 }
201 
202 /*
203  * Hashing code adapted from ext3
204  */
205 #define DELTA 0x9E3779B9
206 
TEA_transform(__u32 buf[4],__u32 const in[])207 static void TEA_transform(__u32 buf[4], __u32 const in[])
208 {
209 	__u32	sum = 0;
210 	__u32	b0 = buf[0], b1 = buf[1];
211 	__u32	a = in[0], b = in[1], c = in[2], d = in[3];
212 	int	n = 16;
213 
214 	do {
215 		sum += DELTA;
216 		b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
217 		b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
218 	} while (--n);
219 
220 	buf[0] += b0;
221 	buf[1] += b1;
222 }
223 
str2hashbuf(const char * msg,int len,__u32 * buf,int num)224 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
225 {
226 	__u32	pad, val;
227 	int	i;
228 
229 	pad = (__u32)len | ((__u32)len << 8);
230 	pad |= pad << 16;
231 
232 	val = pad;
233 	if (len > num*4)
234 		len = num * 4;
235 	for (i = 0; i < len; i++) {
236 		if ((i % 4) == 0)
237 			val = pad;
238 		val = msg[i] + (val << 8);
239 		if ((i % 4) == 3) {
240 			*buf++ = val;
241 			val = pad;
242 			num--;
243 		}
244 	}
245 	if (--num >= 0)
246 		*buf++ = val;
247 	while (--num >= 0)
248 		*buf++ = pad;
249 }
250 
ocfs2_dx_dir_name_hash(struct inode * dir,const char * name,int len,struct ocfs2_dx_hinfo * hinfo)251 static void ocfs2_dx_dir_name_hash(struct inode *dir, const char *name, int len,
252 				   struct ocfs2_dx_hinfo *hinfo)
253 {
254 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
255 	const char	*p;
256 	__u32		in[8], buf[4];
257 
258 	/*
259 	 * XXX: Is this really necessary, if the index is never looked
260 	 * at by readdir? Is a hash value of '0' a bad idea?
261 	 */
262 	if ((len == 1 && !strncmp(".", name, 1)) ||
263 	    (len == 2 && !strncmp("..", name, 2))) {
264 		buf[0] = buf[1] = 0;
265 		goto out;
266 	}
267 
268 #ifdef OCFS2_DEBUG_DX_DIRS
269 	/*
270 	 * This makes it very easy to debug indexing problems. We
271 	 * should never allow this to be selected without hand editing
272 	 * this file though.
273 	 */
274 	buf[0] = buf[1] = len;
275 	goto out;
276 #endif
277 
278 	memcpy(buf, osb->osb_dx_seed, sizeof(buf));
279 
280 	p = name;
281 	while (len > 0) {
282 		str2hashbuf(p, len, in, 4);
283 		TEA_transform(buf, in);
284 		len -= 16;
285 		p += 16;
286 	}
287 
288 out:
289 	hinfo->major_hash = buf[0];
290 	hinfo->minor_hash = buf[1];
291 }
292 
293 /*
294  * bh passed here can be an inode block or a dir data block, depending
295  * on the inode inline data flag.
296  */
ocfs2_check_dir_entry(struct inode * dir,struct ocfs2_dir_entry * de,struct buffer_head * bh,char * buf,unsigned int size,unsigned long offset)297 static int ocfs2_check_dir_entry(struct inode *dir,
298 				 struct ocfs2_dir_entry *de,
299 				 struct buffer_head *bh,
300 				 char *buf,
301 				 unsigned int size,
302 				 unsigned long offset)
303 {
304 	const char *error_msg = NULL;
305 	unsigned long next_offset;
306 	int rlen;
307 
308 	if (offset > size - OCFS2_DIR_REC_LEN(1)) {
309 		/* Dirent is (maybe partially) beyond the buffer
310 		 * boundaries so touching 'de' members is unsafe.
311 		 */
312 		mlog(ML_ERROR, "directory entry (#%llu: offset=%lu) "
313 		     "too close to end or out-of-bounds",
314 		     (unsigned long long)OCFS2_I(dir)->ip_blkno, offset);
315 		return 0;
316 	}
317 
318 	rlen = le16_to_cpu(de->rec_len);
319 	next_offset = ((char *) de - buf) + rlen;
320 
321 	if (unlikely(rlen < OCFS2_DIR_REC_LEN(1)))
322 		error_msg = "rec_len is smaller than minimal";
323 	else if (unlikely(rlen % 4 != 0))
324 		error_msg = "rec_len % 4 != 0";
325 	else if (unlikely(rlen < OCFS2_DIR_REC_LEN(de->name_len)))
326 		error_msg = "rec_len is too small for name_len";
327 	else if (unlikely(next_offset > size))
328 		error_msg = "directory entry overrun";
329 	else if (unlikely(next_offset > size - OCFS2_DIR_REC_LEN(1)) &&
330 		 next_offset != size)
331 		error_msg = "directory entry too close to end";
332 
333 	if (unlikely(error_msg != NULL))
334 		mlog(ML_ERROR, "bad entry in directory #%llu: %s - "
335 		     "offset=%lu, inode=%llu, rec_len=%d, name_len=%d\n",
336 		     (unsigned long long)OCFS2_I(dir)->ip_blkno, error_msg,
337 		     offset, (unsigned long long)le64_to_cpu(de->inode), rlen,
338 		     de->name_len);
339 
340 	return error_msg == NULL ? 1 : 0;
341 }
342 
ocfs2_match(int len,const char * const name,struct ocfs2_dir_entry * de)343 static inline int ocfs2_match(int len,
344 			      const char * const name,
345 			      struct ocfs2_dir_entry *de)
346 {
347 	if (len != de->name_len)
348 		return 0;
349 	if (!de->inode)
350 		return 0;
351 	return !memcmp(name, de->name, len);
352 }
353 
354 /*
355  * Returns 0 if not found, -1 on failure, and 1 on success
356  */
ocfs2_search_dirblock(struct buffer_head * bh,struct inode * dir,const char * name,int namelen,unsigned long offset,char * first_de,unsigned int bytes,struct ocfs2_dir_entry ** res_dir)357 static inline int ocfs2_search_dirblock(struct buffer_head *bh,
358 					struct inode *dir,
359 					const char *name, int namelen,
360 					unsigned long offset,
361 					char *first_de,
362 					unsigned int bytes,
363 					struct ocfs2_dir_entry **res_dir)
364 {
365 	struct ocfs2_dir_entry *de;
366 	char *dlimit, *de_buf;
367 	int de_len;
368 	int ret = 0;
369 
370 	de_buf = first_de;
371 	dlimit = de_buf + bytes;
372 
373 	while (de_buf < dlimit - OCFS2_DIR_MEMBER_LEN) {
374 		/* this code is executed quadratically often */
375 		/* do minimal checking `by hand' */
376 
377 		de = (struct ocfs2_dir_entry *) de_buf;
378 
379 		if (de->name + namelen <= dlimit &&
380 		    ocfs2_match(namelen, name, de)) {
381 			/* found a match - just to be sure, do a full check */
382 			if (!ocfs2_check_dir_entry(dir, de, bh, first_de,
383 						   bytes, offset)) {
384 				ret = -1;
385 				goto bail;
386 			}
387 			*res_dir = de;
388 			ret = 1;
389 			goto bail;
390 		}
391 
392 		/* prevent looping on a bad block */
393 		de_len = le16_to_cpu(de->rec_len);
394 		if (de_len <= 0) {
395 			ret = -1;
396 			goto bail;
397 		}
398 
399 		de_buf += de_len;
400 		offset += de_len;
401 	}
402 
403 bail:
404 	trace_ocfs2_search_dirblock(ret);
405 	return ret;
406 }
407 
ocfs2_find_entry_id(const char * name,int namelen,struct inode * dir,struct ocfs2_dir_entry ** res_dir)408 static struct buffer_head *ocfs2_find_entry_id(const char *name,
409 					       int namelen,
410 					       struct inode *dir,
411 					       struct ocfs2_dir_entry **res_dir)
412 {
413 	int ret, found;
414 	struct buffer_head *di_bh = NULL;
415 	struct ocfs2_dinode *di;
416 	struct ocfs2_inline_data *data;
417 
418 	ret = ocfs2_read_inode_block(dir, &di_bh);
419 	if (ret) {
420 		mlog_errno(ret);
421 		goto out;
422 	}
423 
424 	di = (struct ocfs2_dinode *)di_bh->b_data;
425 	data = &di->id2.i_data;
426 
427 	found = ocfs2_search_dirblock(di_bh, dir, name, namelen, 0,
428 				      data->id_data, i_size_read(dir), res_dir);
429 	if (found == 1)
430 		return di_bh;
431 
432 	brelse(di_bh);
433 out:
434 	return NULL;
435 }
436 
ocfs2_validate_dir_block(struct super_block * sb,struct buffer_head * bh)437 static int ocfs2_validate_dir_block(struct super_block *sb,
438 				    struct buffer_head *bh)
439 {
440 	int rc;
441 	struct ocfs2_dir_block_trailer *trailer =
442 		ocfs2_trailer_from_bh(bh, sb);
443 
444 
445 	/*
446 	 * We don't validate dirents here, that's handled
447 	 * in-place when the code walks them.
448 	 */
449 	trace_ocfs2_validate_dir_block((unsigned long long)bh->b_blocknr);
450 
451 	BUG_ON(!buffer_uptodate(bh));
452 
453 	/*
454 	 * If the ecc fails, we return the error but otherwise
455 	 * leave the filesystem running.  We know any error is
456 	 * local to this block.
457 	 *
458 	 * Note that we are safe to call this even if the directory
459 	 * doesn't have a trailer.  Filesystems without metaecc will do
460 	 * nothing, and filesystems with it will have one.
461 	 */
462 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &trailer->db_check);
463 	if (rc)
464 		mlog(ML_ERROR, "Checksum failed for dinode %llu\n",
465 		     (unsigned long long)bh->b_blocknr);
466 
467 	return rc;
468 }
469 
470 /*
471  * Validate a directory trailer.
472  *
473  * We check the trailer here rather than in ocfs2_validate_dir_block()
474  * because that function doesn't have the inode to test.
475  */
ocfs2_check_dir_trailer(struct inode * dir,struct buffer_head * bh)476 static int ocfs2_check_dir_trailer(struct inode *dir, struct buffer_head *bh)
477 {
478 	int rc = 0;
479 	struct ocfs2_dir_block_trailer *trailer;
480 
481 	trailer = ocfs2_trailer_from_bh(bh, dir->i_sb);
482 	if (!OCFS2_IS_VALID_DIR_TRAILER(trailer)) {
483 		rc = ocfs2_error(dir->i_sb,
484 				 "Invalid dirblock #%llu: signature = %.*s\n",
485 				 (unsigned long long)bh->b_blocknr, 7,
486 				 trailer->db_signature);
487 		goto out;
488 	}
489 	if (le64_to_cpu(trailer->db_blkno) != bh->b_blocknr) {
490 		rc = ocfs2_error(dir->i_sb,
491 				 "Directory block #%llu has an invalid db_blkno of %llu\n",
492 				 (unsigned long long)bh->b_blocknr,
493 				 (unsigned long long)le64_to_cpu(trailer->db_blkno));
494 		goto out;
495 	}
496 	if (le64_to_cpu(trailer->db_parent_dinode) !=
497 	    OCFS2_I(dir)->ip_blkno) {
498 		rc = ocfs2_error(dir->i_sb,
499 				 "Directory block #%llu on dinode #%llu has an invalid parent_dinode of %llu\n",
500 				 (unsigned long long)bh->b_blocknr,
501 				 (unsigned long long)OCFS2_I(dir)->ip_blkno,
502 				 (unsigned long long)le64_to_cpu(trailer->db_blkno));
503 		goto out;
504 	}
505 out:
506 	return rc;
507 }
508 
509 /*
510  * This function forces all errors to -EIO for consistency with its
511  * predecessor, ocfs2_bread().  We haven't audited what returning the
512  * real error codes would do to callers.  We log the real codes with
513  * mlog_errno() before we squash them.
514  */
ocfs2_read_dir_block(struct inode * inode,u64 v_block,struct buffer_head ** bh,int flags)515 static int ocfs2_read_dir_block(struct inode *inode, u64 v_block,
516 				struct buffer_head **bh, int flags)
517 {
518 	int rc = 0;
519 	struct buffer_head *tmp = *bh;
520 
521 	rc = ocfs2_read_virt_blocks(inode, v_block, 1, &tmp, flags,
522 				    ocfs2_validate_dir_block);
523 	if (rc) {
524 		mlog_errno(rc);
525 		goto out;
526 	}
527 
528 	if (!(flags & OCFS2_BH_READAHEAD) &&
529 	    ocfs2_supports_dir_trailer(inode)) {
530 		rc = ocfs2_check_dir_trailer(inode, tmp);
531 		if (rc) {
532 			if (!*bh)
533 				brelse(tmp);
534 			mlog_errno(rc);
535 			goto out;
536 		}
537 	}
538 
539 	/* If ocfs2_read_virt_blocks() got us a new bh, pass it up. */
540 	if (!*bh)
541 		*bh = tmp;
542 
543 out:
544 	return rc ? -EIO : 0;
545 }
546 
547 /*
548  * Read the block at 'phys' which belongs to this directory
549  * inode. This function does no virtual->physical block translation -
550  * what's passed in is assumed to be a valid directory block.
551  */
ocfs2_read_dir_block_direct(struct inode * dir,u64 phys,struct buffer_head ** bh)552 static int ocfs2_read_dir_block_direct(struct inode *dir, u64 phys,
553 				       struct buffer_head **bh)
554 {
555 	int ret;
556 	struct buffer_head *tmp = *bh;
557 
558 	ret = ocfs2_read_block(INODE_CACHE(dir), phys, &tmp,
559 			       ocfs2_validate_dir_block);
560 	if (ret) {
561 		mlog_errno(ret);
562 		goto out;
563 	}
564 
565 	if (ocfs2_supports_dir_trailer(dir)) {
566 		ret = ocfs2_check_dir_trailer(dir, tmp);
567 		if (ret) {
568 			if (!*bh)
569 				brelse(tmp);
570 			mlog_errno(ret);
571 			goto out;
572 		}
573 	}
574 
575 	if (!ret && !*bh)
576 		*bh = tmp;
577 out:
578 	return ret;
579 }
580 
ocfs2_validate_dx_root(struct super_block * sb,struct buffer_head * bh)581 static int ocfs2_validate_dx_root(struct super_block *sb,
582 				  struct buffer_head *bh)
583 {
584 	int ret;
585 	struct ocfs2_dx_root_block *dx_root;
586 
587 	BUG_ON(!buffer_uptodate(bh));
588 
589 	dx_root = (struct ocfs2_dx_root_block *) bh->b_data;
590 
591 	ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_root->dr_check);
592 	if (ret) {
593 		mlog(ML_ERROR,
594 		     "Checksum failed for dir index root block %llu\n",
595 		     (unsigned long long)bh->b_blocknr);
596 		return ret;
597 	}
598 
599 	if (!OCFS2_IS_VALID_DX_ROOT(dx_root)) {
600 		ret = ocfs2_error(sb,
601 				  "Dir Index Root # %llu has bad signature %.*s\n",
602 				  (unsigned long long)le64_to_cpu(dx_root->dr_blkno),
603 				  7, dx_root->dr_signature);
604 	}
605 
606 	return ret;
607 }
608 
ocfs2_read_dx_root(struct inode * dir,struct ocfs2_dinode * di,struct buffer_head ** dx_root_bh)609 static int ocfs2_read_dx_root(struct inode *dir, struct ocfs2_dinode *di,
610 			      struct buffer_head **dx_root_bh)
611 {
612 	int ret;
613 	u64 blkno = le64_to_cpu(di->i_dx_root);
614 	struct buffer_head *tmp = *dx_root_bh;
615 
616 	ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
617 			       ocfs2_validate_dx_root);
618 
619 	/* If ocfs2_read_block() got us a new bh, pass it up. */
620 	if (!ret && !*dx_root_bh)
621 		*dx_root_bh = tmp;
622 
623 	return ret;
624 }
625 
ocfs2_validate_dx_leaf(struct super_block * sb,struct buffer_head * bh)626 static int ocfs2_validate_dx_leaf(struct super_block *sb,
627 				  struct buffer_head *bh)
628 {
629 	int ret;
630 	struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)bh->b_data;
631 
632 	BUG_ON(!buffer_uptodate(bh));
633 
634 	ret = ocfs2_validate_meta_ecc(sb, bh->b_data, &dx_leaf->dl_check);
635 	if (ret) {
636 		mlog(ML_ERROR,
637 		     "Checksum failed for dir index leaf block %llu\n",
638 		     (unsigned long long)bh->b_blocknr);
639 		return ret;
640 	}
641 
642 	if (!OCFS2_IS_VALID_DX_LEAF(dx_leaf)) {
643 		ret = ocfs2_error(sb, "Dir Index Leaf has bad signature %.*s\n",
644 				  7, dx_leaf->dl_signature);
645 	}
646 
647 	return ret;
648 }
649 
ocfs2_read_dx_leaf(struct inode * dir,u64 blkno,struct buffer_head ** dx_leaf_bh)650 static int ocfs2_read_dx_leaf(struct inode *dir, u64 blkno,
651 			      struct buffer_head **dx_leaf_bh)
652 {
653 	int ret;
654 	struct buffer_head *tmp = *dx_leaf_bh;
655 
656 	ret = ocfs2_read_block(INODE_CACHE(dir), blkno, &tmp,
657 			       ocfs2_validate_dx_leaf);
658 
659 	/* If ocfs2_read_block() got us a new bh, pass it up. */
660 	if (!ret && !*dx_leaf_bh)
661 		*dx_leaf_bh = tmp;
662 
663 	return ret;
664 }
665 
666 /*
667  * Read a series of dx_leaf blocks. This expects all buffer_head
668  * pointers to be NULL on function entry.
669  */
ocfs2_read_dx_leaves(struct inode * dir,u64 start,int num,struct buffer_head ** dx_leaf_bhs)670 static int ocfs2_read_dx_leaves(struct inode *dir, u64 start, int num,
671 				struct buffer_head **dx_leaf_bhs)
672 {
673 	int ret;
674 
675 	ret = ocfs2_read_blocks(INODE_CACHE(dir), start, num, dx_leaf_bhs, 0,
676 				ocfs2_validate_dx_leaf);
677 	if (ret)
678 		mlog_errno(ret);
679 
680 	return ret;
681 }
682 
ocfs2_find_entry_el(const char * name,int namelen,struct inode * dir,struct ocfs2_dir_entry ** res_dir)683 static struct buffer_head *ocfs2_find_entry_el(const char *name, int namelen,
684 					       struct inode *dir,
685 					       struct ocfs2_dir_entry **res_dir)
686 {
687 	struct super_block *sb;
688 	struct buffer_head *bh_use[NAMEI_RA_SIZE];
689 	struct buffer_head *bh, *ret = NULL;
690 	unsigned long start, block, b;
691 	int ra_max = 0;		/* Number of bh's in the readahead
692 				   buffer, bh_use[] */
693 	int ra_ptr = 0;		/* Current index into readahead
694 				   buffer */
695 	int num = 0;
696 	int nblocks, i;
697 
698 	sb = dir->i_sb;
699 
700 	nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
701 	start = OCFS2_I(dir)->ip_dir_start_lookup;
702 	if (start >= nblocks)
703 		start = 0;
704 	block = start;
705 
706 restart:
707 	do {
708 		/*
709 		 * We deal with the read-ahead logic here.
710 		 */
711 		if (ra_ptr >= ra_max) {
712 			/* Refill the readahead buffer */
713 			ra_ptr = 0;
714 			b = block;
715 			for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) {
716 				/*
717 				 * Terminate if we reach the end of the
718 				 * directory and must wrap, or if our
719 				 * search has finished at this block.
720 				 */
721 				if (b >= nblocks || (num && block == start)) {
722 					bh_use[ra_max] = NULL;
723 					break;
724 				}
725 				num++;
726 
727 				bh = NULL;
728 				ocfs2_read_dir_block(dir, b++, &bh,
729 							   OCFS2_BH_READAHEAD);
730 				bh_use[ra_max] = bh;
731 			}
732 		}
733 		if ((bh = bh_use[ra_ptr++]) == NULL)
734 			goto next;
735 		if (ocfs2_read_dir_block(dir, block, &bh, 0)) {
736 			/* read error, skip block & hope for the best.
737 			 * ocfs2_read_dir_block() has released the bh. */
738 			mlog(ML_ERROR, "reading directory %llu, "
739 				    "offset %lu\n",
740 				    (unsigned long long)OCFS2_I(dir)->ip_blkno,
741 				    block);
742 			goto next;
743 		}
744 		i = ocfs2_search_dirblock(bh, dir, name, namelen,
745 					  block << sb->s_blocksize_bits,
746 					  bh->b_data, sb->s_blocksize,
747 					  res_dir);
748 		if (i == 1) {
749 			OCFS2_I(dir)->ip_dir_start_lookup = block;
750 			ret = bh;
751 			goto cleanup_and_exit;
752 		} else {
753 			brelse(bh);
754 			if (i < 0)
755 				goto cleanup_and_exit;
756 		}
757 	next:
758 		if (++block >= nblocks)
759 			block = 0;
760 	} while (block != start);
761 
762 	/*
763 	 * If the directory has grown while we were searching, then
764 	 * search the last part of the directory before giving up.
765 	 */
766 	block = nblocks;
767 	nblocks = i_size_read(dir) >> sb->s_blocksize_bits;
768 	if (block < nblocks) {
769 		start = 0;
770 		goto restart;
771 	}
772 
773 cleanup_and_exit:
774 	/* Clean up the read-ahead blocks */
775 	for (; ra_ptr < ra_max; ra_ptr++)
776 		brelse(bh_use[ra_ptr]);
777 
778 	trace_ocfs2_find_entry_el(ret);
779 	return ret;
780 }
781 
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)782 static int ocfs2_dx_dir_lookup_rec(struct inode *inode,
783 				   struct ocfs2_extent_list *el,
784 				   u32 major_hash,
785 				   u32 *ret_cpos,
786 				   u64 *ret_phys_blkno,
787 				   unsigned int *ret_clen)
788 {
789 	int ret = 0, i, found;
790 	struct buffer_head *eb_bh = NULL;
791 	struct ocfs2_extent_block *eb;
792 	struct ocfs2_extent_rec *rec = NULL;
793 
794 	if (le16_to_cpu(el->l_count) !=
795 	    ocfs2_extent_recs_per_dx_root(inode->i_sb)) {
796 		ret = ocfs2_error(inode->i_sb,
797 				  "Inode %lu has invalid extent list length %u\n",
798 				  inode->i_ino, le16_to_cpu(el->l_count));
799 		goto out;
800 	}
801 
802 	if (el->l_tree_depth) {
803 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, major_hash,
804 				      &eb_bh);
805 		if (ret) {
806 			mlog_errno(ret);
807 			goto out;
808 		}
809 
810 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
811 		el = &eb->h_list;
812 
813 		if (el->l_tree_depth) {
814 			ret = ocfs2_error(inode->i_sb,
815 					  "Inode %lu has non zero tree depth in btree tree block %llu\n",
816 					  inode->i_ino,
817 					  (unsigned long long)eb_bh->b_blocknr);
818 			goto out;
819 		}
820 	}
821 
822 	if (le16_to_cpu(el->l_next_free_rec) == 0) {
823 		ret = ocfs2_error(inode->i_sb,
824 				  "Inode %lu has empty extent list at depth %u\n",
825 				  inode->i_ino,
826 				  le16_to_cpu(el->l_tree_depth));
827 		goto out;
828 	}
829 
830 	found = 0;
831 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
832 		rec = &el->l_recs[i];
833 
834 		if (le32_to_cpu(rec->e_cpos) <= major_hash) {
835 			found = 1;
836 			break;
837 		}
838 	}
839 
840 	if (!found) {
841 		ret = ocfs2_error(inode->i_sb,
842 				  "Inode %lu has bad extent record (%u, %u, 0) in btree\n",
843 				  inode->i_ino,
844 				  le32_to_cpu(rec->e_cpos),
845 				  ocfs2_rec_clusters(el, rec));
846 		goto out;
847 	}
848 
849 	if (ret_phys_blkno)
850 		*ret_phys_blkno = le64_to_cpu(rec->e_blkno);
851 	if (ret_cpos)
852 		*ret_cpos = le32_to_cpu(rec->e_cpos);
853 	if (ret_clen)
854 		*ret_clen = le16_to_cpu(rec->e_leaf_clusters);
855 
856 out:
857 	brelse(eb_bh);
858 	return ret;
859 }
860 
861 /*
862  * Returns the block index, from the start of the cluster which this
863  * hash belongs too.
864  */
__ocfs2_dx_dir_hash_idx(struct ocfs2_super * osb,u32 minor_hash)865 static inline unsigned int __ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
866 						   u32 minor_hash)
867 {
868 	return minor_hash & osb->osb_dx_mask;
869 }
870 
ocfs2_dx_dir_hash_idx(struct ocfs2_super * osb,struct ocfs2_dx_hinfo * hinfo)871 static inline unsigned int ocfs2_dx_dir_hash_idx(struct ocfs2_super *osb,
872 					  struct ocfs2_dx_hinfo *hinfo)
873 {
874 	return __ocfs2_dx_dir_hash_idx(osb, hinfo->minor_hash);
875 }
876 
ocfs2_dx_dir_lookup(struct inode * inode,struct ocfs2_extent_list * el,struct ocfs2_dx_hinfo * hinfo,u32 * ret_cpos,u64 * ret_phys_blkno)877 static int ocfs2_dx_dir_lookup(struct inode *inode,
878 			       struct ocfs2_extent_list *el,
879 			       struct ocfs2_dx_hinfo *hinfo,
880 			       u32 *ret_cpos,
881 			       u64 *ret_phys_blkno)
882 {
883 	int ret = 0;
884 	unsigned int cend, clen;
885 	u32 cpos;
886 	u64 blkno;
887 	u32 name_hash = hinfo->major_hash;
888 
889 	ret = ocfs2_dx_dir_lookup_rec(inode, el, name_hash, &cpos, &blkno,
890 				      &clen);
891 	if (ret) {
892 		mlog_errno(ret);
893 		goto out;
894 	}
895 
896 	cend = cpos + clen;
897 	if (name_hash >= cend) {
898 		/* We want the last cluster */
899 		blkno += ocfs2_clusters_to_blocks(inode->i_sb, clen - 1);
900 		cpos += clen - 1;
901 	} else {
902 		blkno += ocfs2_clusters_to_blocks(inode->i_sb,
903 						  name_hash - cpos);
904 		cpos = name_hash;
905 	}
906 
907 	/*
908 	 * We now have the cluster which should hold our entry. To
909 	 * find the exact block from the start of the cluster to
910 	 * search, we take the lower bits of the hash.
911 	 */
912 	blkno += ocfs2_dx_dir_hash_idx(OCFS2_SB(inode->i_sb), hinfo);
913 
914 	if (ret_phys_blkno)
915 		*ret_phys_blkno = blkno;
916 	if (ret_cpos)
917 		*ret_cpos = cpos;
918 
919 out:
920 
921 	return ret;
922 }
923 
ocfs2_dx_dir_search(const char * name,int namelen,struct inode * dir,struct ocfs2_dx_root_block * dx_root,struct ocfs2_dir_lookup_result * res)924 static int ocfs2_dx_dir_search(const char *name, int namelen,
925 			       struct inode *dir,
926 			       struct ocfs2_dx_root_block *dx_root,
927 			       struct ocfs2_dir_lookup_result *res)
928 {
929 	int ret, i, found;
930 	u64 phys;
931 	struct buffer_head *dx_leaf_bh = NULL;
932 	struct ocfs2_dx_leaf *dx_leaf;
933 	struct ocfs2_dx_entry *dx_entry = NULL;
934 	struct buffer_head *dir_ent_bh = NULL;
935 	struct ocfs2_dir_entry *dir_ent = NULL;
936 	struct ocfs2_dx_hinfo *hinfo = &res->dl_hinfo;
937 	struct ocfs2_extent_list *dr_el;
938 	struct ocfs2_dx_entry_list *entry_list;
939 
940 	ocfs2_dx_dir_name_hash(dir, name, namelen, &res->dl_hinfo);
941 
942 	if (ocfs2_dx_root_inline(dx_root)) {
943 		entry_list = &dx_root->dr_entries;
944 		goto search;
945 	}
946 
947 	dr_el = &dx_root->dr_list;
948 
949 	ret = ocfs2_dx_dir_lookup(dir, dr_el, hinfo, NULL, &phys);
950 	if (ret) {
951 		mlog_errno(ret);
952 		goto out;
953 	}
954 
955 	trace_ocfs2_dx_dir_search((unsigned long long)OCFS2_I(dir)->ip_blkno,
956 				  namelen, name, hinfo->major_hash,
957 				  hinfo->minor_hash, (unsigned long long)phys);
958 
959 	ret = ocfs2_read_dx_leaf(dir, phys, &dx_leaf_bh);
960 	if (ret) {
961 		mlog_errno(ret);
962 		goto out;
963 	}
964 
965 	dx_leaf = (struct ocfs2_dx_leaf *) dx_leaf_bh->b_data;
966 
967 	trace_ocfs2_dx_dir_search_leaf_info(
968 			le16_to_cpu(dx_leaf->dl_list.de_num_used),
969 			le16_to_cpu(dx_leaf->dl_list.de_count));
970 
971 	entry_list = &dx_leaf->dl_list;
972 
973 search:
974 	/*
975 	 * Empty leaf is legal, so no need to check for that.
976 	 */
977 	found = 0;
978 	for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
979 		dx_entry = &entry_list->de_entries[i];
980 
981 		if (hinfo->major_hash != le32_to_cpu(dx_entry->dx_major_hash)
982 		    || hinfo->minor_hash != le32_to_cpu(dx_entry->dx_minor_hash))
983 			continue;
984 
985 		/*
986 		 * Search unindexed leaf block now. We're not
987 		 * guaranteed to find anything.
988 		 */
989 		ret = ocfs2_read_dir_block_direct(dir,
990 					  le64_to_cpu(dx_entry->dx_dirent_blk),
991 					  &dir_ent_bh);
992 		if (ret) {
993 			mlog_errno(ret);
994 			goto out;
995 		}
996 
997 		/*
998 		 * XXX: We should check the unindexed block here,
999 		 * before using it.
1000 		 */
1001 
1002 		found = ocfs2_search_dirblock(dir_ent_bh, dir, name, namelen,
1003 					      0, dir_ent_bh->b_data,
1004 					      dir->i_sb->s_blocksize, &dir_ent);
1005 		if (found == 1)
1006 			break;
1007 
1008 		if (found == -1) {
1009 			/* This means we found a bad directory entry. */
1010 			ret = -EIO;
1011 			mlog_errno(ret);
1012 			goto out;
1013 		}
1014 
1015 		brelse(dir_ent_bh);
1016 		dir_ent_bh = NULL;
1017 	}
1018 
1019 	if (found <= 0) {
1020 		ret = -ENOENT;
1021 		goto out;
1022 	}
1023 
1024 	res->dl_leaf_bh = dir_ent_bh;
1025 	res->dl_entry = dir_ent;
1026 	res->dl_dx_leaf_bh = dx_leaf_bh;
1027 	res->dl_dx_entry = dx_entry;
1028 
1029 	ret = 0;
1030 out:
1031 	if (ret) {
1032 		brelse(dx_leaf_bh);
1033 		brelse(dir_ent_bh);
1034 	}
1035 	return ret;
1036 }
1037 
ocfs2_find_entry_dx(const char * name,int namelen,struct inode * dir,struct ocfs2_dir_lookup_result * lookup)1038 static int ocfs2_find_entry_dx(const char *name, int namelen,
1039 			       struct inode *dir,
1040 			       struct ocfs2_dir_lookup_result *lookup)
1041 {
1042 	int ret;
1043 	struct buffer_head *di_bh = NULL;
1044 	struct ocfs2_dinode *di;
1045 	struct buffer_head *dx_root_bh = NULL;
1046 	struct ocfs2_dx_root_block *dx_root;
1047 
1048 	ret = ocfs2_read_inode_block(dir, &di_bh);
1049 	if (ret) {
1050 		mlog_errno(ret);
1051 		goto out;
1052 	}
1053 
1054 	di = (struct ocfs2_dinode *)di_bh->b_data;
1055 
1056 	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
1057 	if (ret) {
1058 		mlog_errno(ret);
1059 		goto out;
1060 	}
1061 	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
1062 
1063 	ret = ocfs2_dx_dir_search(name, namelen, dir, dx_root, lookup);
1064 	if (ret) {
1065 		if (ret != -ENOENT)
1066 			mlog_errno(ret);
1067 		goto out;
1068 	}
1069 
1070 	lookup->dl_dx_root_bh = dx_root_bh;
1071 	dx_root_bh = NULL;
1072 out:
1073 	brelse(di_bh);
1074 	brelse(dx_root_bh);
1075 	return ret;
1076 }
1077 
1078 /*
1079  * Try to find an entry of the provided name within 'dir'.
1080  *
1081  * If nothing was found, -ENOENT is returned. Otherwise, zero is
1082  * returned and the struct 'res' will contain information useful to
1083  * other directory manipulation functions.
1084  *
1085  * Caller can NOT assume anything about the contents of the
1086  * buffer_heads - they are passed back only so that it can be passed
1087  * into any one of the manipulation functions (add entry, delete
1088  * entry, etc). As an example, bh in the extent directory case is a
1089  * data block, in the inline-data case it actually points to an inode,
1090  * in the indexed directory case, multiple buffers are involved.
1091  */
ocfs2_find_entry(const char * name,int namelen,struct inode * dir,struct ocfs2_dir_lookup_result * lookup)1092 int ocfs2_find_entry(const char *name, int namelen,
1093 		     struct inode *dir, struct ocfs2_dir_lookup_result *lookup)
1094 {
1095 	struct buffer_head *bh;
1096 	struct ocfs2_dir_entry *res_dir = NULL;
1097 	int ret = 0;
1098 
1099 	if (ocfs2_dir_indexed(dir))
1100 		return ocfs2_find_entry_dx(name, namelen, dir, lookup);
1101 
1102 	if (unlikely(i_size_read(dir) <= 0)) {
1103 		ret = -EFSCORRUPTED;
1104 		mlog_errno(ret);
1105 		goto out;
1106 	}
1107 	/*
1108 	 * The unindexed dir code only uses part of the lookup
1109 	 * structure, so there's no reason to push it down further
1110 	 * than this.
1111 	 */
1112 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1113 		if (unlikely(i_size_read(dir) > dir->i_sb->s_blocksize)) {
1114 			ret = -EFSCORRUPTED;
1115 			mlog_errno(ret);
1116 			goto out;
1117 		}
1118 		bh = ocfs2_find_entry_id(name, namelen, dir, &res_dir);
1119 	} else {
1120 		bh = ocfs2_find_entry_el(name, namelen, dir, &res_dir);
1121 	}
1122 
1123 	if (bh == NULL)
1124 		return -ENOENT;
1125 
1126 	lookup->dl_leaf_bh = bh;
1127 	lookup->dl_entry = res_dir;
1128 out:
1129 	return ret;
1130 }
1131 
1132 /*
1133  * Update inode number and type of a previously found directory entry.
1134  */
ocfs2_update_entry(struct inode * dir,handle_t * handle,struct ocfs2_dir_lookup_result * res,struct inode * new_entry_inode)1135 int ocfs2_update_entry(struct inode *dir, handle_t *handle,
1136 		       struct ocfs2_dir_lookup_result *res,
1137 		       struct inode *new_entry_inode)
1138 {
1139 	int ret;
1140 	ocfs2_journal_access_func access = ocfs2_journal_access_db;
1141 	struct ocfs2_dir_entry *de = res->dl_entry;
1142 	struct buffer_head *de_bh = res->dl_leaf_bh;
1143 
1144 	/*
1145 	 * The same code works fine for both inline-data and extent
1146 	 * based directories, so no need to split this up.  The only
1147 	 * difference is the journal_access function.
1148 	 */
1149 
1150 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1151 		access = ocfs2_journal_access_di;
1152 
1153 	ret = access(handle, INODE_CACHE(dir), de_bh,
1154 		     OCFS2_JOURNAL_ACCESS_WRITE);
1155 	if (ret) {
1156 		mlog_errno(ret);
1157 		goto out;
1158 	}
1159 
1160 	de->inode = cpu_to_le64(OCFS2_I(new_entry_inode)->ip_blkno);
1161 	ocfs2_set_de_type(de, new_entry_inode->i_mode);
1162 
1163 	ocfs2_journal_dirty(handle, de_bh);
1164 
1165 out:
1166 	return ret;
1167 }
1168 
1169 /*
1170  * __ocfs2_delete_entry deletes a directory entry by merging it with the
1171  * previous entry
1172  */
__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)1173 static int __ocfs2_delete_entry(handle_t *handle, struct inode *dir,
1174 				struct ocfs2_dir_entry *de_del,
1175 				struct buffer_head *bh, char *first_de,
1176 				unsigned int bytes)
1177 {
1178 	struct ocfs2_dir_entry *de, *pde;
1179 	int i, status = -ENOENT;
1180 	ocfs2_journal_access_func access = ocfs2_journal_access_db;
1181 
1182 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1183 		access = ocfs2_journal_access_di;
1184 
1185 	i = 0;
1186 	pde = NULL;
1187 	de = (struct ocfs2_dir_entry *) first_de;
1188 	while (i < bytes) {
1189 		if (!ocfs2_check_dir_entry(dir, de, bh, first_de, bytes, i)) {
1190 			status = -EIO;
1191 			mlog_errno(status);
1192 			goto bail;
1193 		}
1194 		if (de == de_del)  {
1195 			status = access(handle, INODE_CACHE(dir), bh,
1196 					OCFS2_JOURNAL_ACCESS_WRITE);
1197 			if (status < 0) {
1198 				status = -EIO;
1199 				mlog_errno(status);
1200 				goto bail;
1201 			}
1202 			if (pde)
1203 				le16_add_cpu(&pde->rec_len,
1204 						le16_to_cpu(de->rec_len));
1205 			de->inode = 0;
1206 			inode_inc_iversion(dir);
1207 			ocfs2_journal_dirty(handle, bh);
1208 			goto bail;
1209 		}
1210 		i += le16_to_cpu(de->rec_len);
1211 		pde = de;
1212 		de = (struct ocfs2_dir_entry *)((char *)de + le16_to_cpu(de->rec_len));
1213 	}
1214 bail:
1215 	return status;
1216 }
1217 
ocfs2_figure_dirent_hole(struct ocfs2_dir_entry * de)1218 static unsigned int ocfs2_figure_dirent_hole(struct ocfs2_dir_entry *de)
1219 {
1220 	unsigned int hole;
1221 
1222 	if (le64_to_cpu(de->inode) == 0)
1223 		hole = le16_to_cpu(de->rec_len);
1224 	else
1225 		hole = le16_to_cpu(de->rec_len) -
1226 			OCFS2_DIR_REC_LEN(de->name_len);
1227 
1228 	return hole;
1229 }
1230 
ocfs2_find_max_rec_len(struct super_block * sb,struct buffer_head * dirblock_bh)1231 static int ocfs2_find_max_rec_len(struct super_block *sb,
1232 				  struct buffer_head *dirblock_bh)
1233 {
1234 	int size, this_hole, largest_hole = 0;
1235 	char *trailer, *de_buf, *limit, *start = dirblock_bh->b_data;
1236 	struct ocfs2_dir_entry *de;
1237 
1238 	trailer = (char *)ocfs2_trailer_from_bh(dirblock_bh, sb);
1239 	size = ocfs2_dir_trailer_blk_off(sb);
1240 	limit = start + size;
1241 	de_buf = start;
1242 	de = (struct ocfs2_dir_entry *)de_buf;
1243 	do {
1244 		if (de_buf != trailer) {
1245 			this_hole = ocfs2_figure_dirent_hole(de);
1246 			if (this_hole > largest_hole)
1247 				largest_hole = this_hole;
1248 		}
1249 
1250 		de_buf += le16_to_cpu(de->rec_len);
1251 		de = (struct ocfs2_dir_entry *)de_buf;
1252 	} while (de_buf < limit);
1253 
1254 	if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
1255 		return largest_hole;
1256 	return 0;
1257 }
1258 
ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list * entry_list,int index)1259 static void ocfs2_dx_list_remove_entry(struct ocfs2_dx_entry_list *entry_list,
1260 				       int index)
1261 {
1262 	int num_used = le16_to_cpu(entry_list->de_num_used);
1263 
1264 	if (num_used == 1 || index == (num_used - 1))
1265 		goto clear;
1266 
1267 	memmove(&entry_list->de_entries[index],
1268 		&entry_list->de_entries[index + 1],
1269 		(num_used - index - 1)*sizeof(struct ocfs2_dx_entry));
1270 clear:
1271 	num_used--;
1272 	memset(&entry_list->de_entries[num_used], 0,
1273 	       sizeof(struct ocfs2_dx_entry));
1274 	entry_list->de_num_used = cpu_to_le16(num_used);
1275 }
1276 
ocfs2_delete_entry_dx(handle_t * handle,struct inode * dir,struct ocfs2_dir_lookup_result * lookup)1277 static int ocfs2_delete_entry_dx(handle_t *handle, struct inode *dir,
1278 				 struct ocfs2_dir_lookup_result *lookup)
1279 {
1280 	int ret, index, max_rec_len, add_to_free_list = 0;
1281 	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1282 	struct buffer_head *leaf_bh = lookup->dl_leaf_bh;
1283 	struct ocfs2_dx_leaf *dx_leaf;
1284 	struct ocfs2_dx_entry *dx_entry = lookup->dl_dx_entry;
1285 	struct ocfs2_dir_block_trailer *trailer;
1286 	struct ocfs2_dx_root_block *dx_root;
1287 	struct ocfs2_dx_entry_list *entry_list;
1288 
1289 	/*
1290 	 * This function gets a bit messy because we might have to
1291 	 * modify the root block, regardless of whether the indexed
1292 	 * entries are stored inline.
1293 	 */
1294 
1295 	/*
1296 	 * *Only* set 'entry_list' here, based on where we're looking
1297 	 * for the indexed entries. Later, we might still want to
1298 	 * journal both blocks, based on free list state.
1299 	 */
1300 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
1301 	if (ocfs2_dx_root_inline(dx_root)) {
1302 		entry_list = &dx_root->dr_entries;
1303 	} else {
1304 		dx_leaf = (struct ocfs2_dx_leaf *) lookup->dl_dx_leaf_bh->b_data;
1305 		entry_list = &dx_leaf->dl_list;
1306 	}
1307 
1308 	/* Neither of these are a disk corruption - that should have
1309 	 * been caught by lookup, before we got here. */
1310 	BUG_ON(le16_to_cpu(entry_list->de_count) <= 0);
1311 	BUG_ON(le16_to_cpu(entry_list->de_num_used) <= 0);
1312 
1313 	index = (char *)dx_entry - (char *)entry_list->de_entries;
1314 	index /= sizeof(*dx_entry);
1315 
1316 	if (index >= le16_to_cpu(entry_list->de_num_used)) {
1317 		mlog(ML_ERROR, "Dir %llu: Bad dx_entry ptr idx %d, (%p, %p)\n",
1318 		     (unsigned long long)OCFS2_I(dir)->ip_blkno, index,
1319 		     entry_list, dx_entry);
1320 		return -EIO;
1321 	}
1322 
1323 	/*
1324 	 * We know that removal of this dirent will leave enough room
1325 	 * for a new one, so add this block to the free list if it
1326 	 * isn't already there.
1327 	 */
1328 	trailer = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
1329 	if (trailer->db_free_rec_len == 0)
1330 		add_to_free_list = 1;
1331 
1332 	/*
1333 	 * Add the block holding our index into the journal before
1334 	 * removing the unindexed entry. If we get an error return
1335 	 * from __ocfs2_delete_entry(), then it hasn't removed the
1336 	 * entry yet. Likewise, successful return means we *must*
1337 	 * remove the indexed entry.
1338 	 *
1339 	 * We're also careful to journal the root tree block here as
1340 	 * the entry count needs to be updated. Also, we might be
1341 	 * adding to the start of the free list.
1342 	 */
1343 	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1344 				      OCFS2_JOURNAL_ACCESS_WRITE);
1345 	if (ret) {
1346 		mlog_errno(ret);
1347 		goto out;
1348 	}
1349 
1350 	if (!ocfs2_dx_root_inline(dx_root)) {
1351 		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
1352 					      lookup->dl_dx_leaf_bh,
1353 					      OCFS2_JOURNAL_ACCESS_WRITE);
1354 		if (ret) {
1355 			mlog_errno(ret);
1356 			goto out;
1357 		}
1358 	}
1359 
1360 	trace_ocfs2_delete_entry_dx((unsigned long long)OCFS2_I(dir)->ip_blkno,
1361 				    index);
1362 
1363 	ret = __ocfs2_delete_entry(handle, dir, lookup->dl_entry,
1364 				   leaf_bh, leaf_bh->b_data, leaf_bh->b_size);
1365 	if (ret) {
1366 		mlog_errno(ret);
1367 		goto out;
1368 	}
1369 
1370 	max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, leaf_bh);
1371 	trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1372 	if (add_to_free_list) {
1373 		trailer->db_free_next = dx_root->dr_free_blk;
1374 		dx_root->dr_free_blk = cpu_to_le64(leaf_bh->b_blocknr);
1375 		ocfs2_journal_dirty(handle, dx_root_bh);
1376 	}
1377 
1378 	/* leaf_bh was journal_accessed for us in __ocfs2_delete_entry */
1379 	ocfs2_journal_dirty(handle, leaf_bh);
1380 
1381 	le32_add_cpu(&dx_root->dr_num_entries, -1);
1382 	ocfs2_journal_dirty(handle, dx_root_bh);
1383 
1384 	ocfs2_dx_list_remove_entry(entry_list, index);
1385 
1386 	if (!ocfs2_dx_root_inline(dx_root))
1387 		ocfs2_journal_dirty(handle, lookup->dl_dx_leaf_bh);
1388 
1389 out:
1390 	return ret;
1391 }
1392 
ocfs2_delete_entry_id(handle_t * handle,struct inode * dir,struct ocfs2_dir_entry * de_del,struct buffer_head * bh)1393 static inline int ocfs2_delete_entry_id(handle_t *handle,
1394 					struct inode *dir,
1395 					struct ocfs2_dir_entry *de_del,
1396 					struct buffer_head *bh)
1397 {
1398 	int ret;
1399 	struct buffer_head *di_bh = NULL;
1400 	struct ocfs2_dinode *di;
1401 	struct ocfs2_inline_data *data;
1402 
1403 	ret = ocfs2_read_inode_block(dir, &di_bh);
1404 	if (ret) {
1405 		mlog_errno(ret);
1406 		goto out;
1407 	}
1408 
1409 	di = (struct ocfs2_dinode *)di_bh->b_data;
1410 	data = &di->id2.i_data;
1411 
1412 	ret = __ocfs2_delete_entry(handle, dir, de_del, bh, data->id_data,
1413 				   i_size_read(dir));
1414 
1415 	brelse(di_bh);
1416 out:
1417 	return ret;
1418 }
1419 
ocfs2_delete_entry_el(handle_t * handle,struct inode * dir,struct ocfs2_dir_entry * de_del,struct buffer_head * bh)1420 static inline int ocfs2_delete_entry_el(handle_t *handle,
1421 					struct inode *dir,
1422 					struct ocfs2_dir_entry *de_del,
1423 					struct buffer_head *bh)
1424 {
1425 	return __ocfs2_delete_entry(handle, dir, de_del, bh, bh->b_data,
1426 				    bh->b_size);
1427 }
1428 
1429 /*
1430  * Delete a directory entry. Hide the details of directory
1431  * implementation from the caller.
1432  */
ocfs2_delete_entry(handle_t * handle,struct inode * dir,struct ocfs2_dir_lookup_result * res)1433 int ocfs2_delete_entry(handle_t *handle,
1434 		       struct inode *dir,
1435 		       struct ocfs2_dir_lookup_result *res)
1436 {
1437 	if (ocfs2_dir_indexed(dir))
1438 		return ocfs2_delete_entry_dx(handle, dir, res);
1439 
1440 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1441 		return ocfs2_delete_entry_id(handle, dir, res->dl_entry,
1442 					     res->dl_leaf_bh);
1443 
1444 	return ocfs2_delete_entry_el(handle, dir, res->dl_entry,
1445 				     res->dl_leaf_bh);
1446 }
1447 
1448 /*
1449  * Check whether 'de' has enough room to hold an entry of
1450  * 'new_rec_len' bytes.
1451  */
ocfs2_dirent_would_fit(struct ocfs2_dir_entry * de,unsigned int new_rec_len)1452 static inline int ocfs2_dirent_would_fit(struct ocfs2_dir_entry *de,
1453 					 unsigned int new_rec_len)
1454 {
1455 	unsigned int de_really_used;
1456 
1457 	/* Check whether this is an empty record with enough space */
1458 	if (le64_to_cpu(de->inode) == 0 &&
1459 	    le16_to_cpu(de->rec_len) >= new_rec_len)
1460 		return 1;
1461 
1462 	/*
1463 	 * Record might have free space at the end which we can
1464 	 * use.
1465 	 */
1466 	de_really_used = OCFS2_DIR_REC_LEN(de->name_len);
1467 	if (le16_to_cpu(de->rec_len) >= (de_really_used + new_rec_len))
1468 	    return 1;
1469 
1470 	return 0;
1471 }
1472 
ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf * dx_leaf,struct ocfs2_dx_entry * dx_new_entry)1473 static void ocfs2_dx_dir_leaf_insert_tail(struct ocfs2_dx_leaf *dx_leaf,
1474 					  struct ocfs2_dx_entry *dx_new_entry)
1475 {
1476 	int i;
1477 
1478 	i = le16_to_cpu(dx_leaf->dl_list.de_num_used);
1479 	dx_leaf->dl_list.de_entries[i] = *dx_new_entry;
1480 
1481 	le16_add_cpu(&dx_leaf->dl_list.de_num_used, 1);
1482 }
1483 
ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list * entry_list,struct ocfs2_dx_hinfo * hinfo,u64 dirent_blk)1484 static void ocfs2_dx_entry_list_insert(struct ocfs2_dx_entry_list *entry_list,
1485 				       struct ocfs2_dx_hinfo *hinfo,
1486 				       u64 dirent_blk)
1487 {
1488 	int i;
1489 	struct ocfs2_dx_entry *dx_entry;
1490 
1491 	i = le16_to_cpu(entry_list->de_num_used);
1492 	dx_entry = &entry_list->de_entries[i];
1493 
1494 	memset(dx_entry, 0, sizeof(*dx_entry));
1495 	dx_entry->dx_major_hash = cpu_to_le32(hinfo->major_hash);
1496 	dx_entry->dx_minor_hash = cpu_to_le32(hinfo->minor_hash);
1497 	dx_entry->dx_dirent_blk = cpu_to_le64(dirent_blk);
1498 
1499 	le16_add_cpu(&entry_list->de_num_used, 1);
1500 }
1501 
__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)1502 static int __ocfs2_dx_dir_leaf_insert(struct inode *dir, handle_t *handle,
1503 				      struct ocfs2_dx_hinfo *hinfo,
1504 				      u64 dirent_blk,
1505 				      struct buffer_head *dx_leaf_bh)
1506 {
1507 	int ret;
1508 	struct ocfs2_dx_leaf *dx_leaf;
1509 
1510 	ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
1511 				      OCFS2_JOURNAL_ACCESS_WRITE);
1512 	if (ret) {
1513 		mlog_errno(ret);
1514 		goto out;
1515 	}
1516 
1517 	dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
1518 	ocfs2_dx_entry_list_insert(&dx_leaf->dl_list, hinfo, dirent_blk);
1519 	ocfs2_journal_dirty(handle, dx_leaf_bh);
1520 
1521 out:
1522 	return ret;
1523 }
1524 
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)1525 static void ocfs2_dx_inline_root_insert(struct inode *dir, handle_t *handle,
1526 					struct ocfs2_dx_hinfo *hinfo,
1527 					u64 dirent_blk,
1528 					struct ocfs2_dx_root_block *dx_root)
1529 {
1530 	ocfs2_dx_entry_list_insert(&dx_root->dr_entries, hinfo, dirent_blk);
1531 }
1532 
ocfs2_dx_dir_insert(struct inode * dir,handle_t * handle,struct ocfs2_dir_lookup_result * lookup)1533 static int ocfs2_dx_dir_insert(struct inode *dir, handle_t *handle,
1534 			       struct ocfs2_dir_lookup_result *lookup)
1535 {
1536 	int ret = 0;
1537 	struct ocfs2_dx_root_block *dx_root;
1538 	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
1539 
1540 	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
1541 				      OCFS2_JOURNAL_ACCESS_WRITE);
1542 	if (ret) {
1543 		mlog_errno(ret);
1544 		goto out;
1545 	}
1546 
1547 	dx_root = (struct ocfs2_dx_root_block *)lookup->dl_dx_root_bh->b_data;
1548 	if (ocfs2_dx_root_inline(dx_root)) {
1549 		ocfs2_dx_inline_root_insert(dir, handle,
1550 					    &lookup->dl_hinfo,
1551 					    lookup->dl_leaf_bh->b_blocknr,
1552 					    dx_root);
1553 	} else {
1554 		ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &lookup->dl_hinfo,
1555 						 lookup->dl_leaf_bh->b_blocknr,
1556 						 lookup->dl_dx_leaf_bh);
1557 		if (ret)
1558 			goto out;
1559 	}
1560 
1561 	le32_add_cpu(&dx_root->dr_num_entries, 1);
1562 	ocfs2_journal_dirty(handle, dx_root_bh);
1563 
1564 out:
1565 	return ret;
1566 }
1567 
ocfs2_remove_block_from_free_list(struct inode * dir,handle_t * handle,struct ocfs2_dir_lookup_result * lookup)1568 static void ocfs2_remove_block_from_free_list(struct inode *dir,
1569 				       handle_t *handle,
1570 				       struct ocfs2_dir_lookup_result *lookup)
1571 {
1572 	struct ocfs2_dir_block_trailer *trailer, *prev;
1573 	struct ocfs2_dx_root_block *dx_root;
1574 	struct buffer_head *bh;
1575 
1576 	trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1577 
1578 	if (ocfs2_free_list_at_root(lookup)) {
1579 		bh = lookup->dl_dx_root_bh;
1580 		dx_root = (struct ocfs2_dx_root_block *)bh->b_data;
1581 		dx_root->dr_free_blk = trailer->db_free_next;
1582 	} else {
1583 		bh = lookup->dl_prev_leaf_bh;
1584 		prev = ocfs2_trailer_from_bh(bh, dir->i_sb);
1585 		prev->db_free_next = trailer->db_free_next;
1586 	}
1587 
1588 	trailer->db_free_rec_len = cpu_to_le16(0);
1589 	trailer->db_free_next = cpu_to_le64(0);
1590 
1591 	ocfs2_journal_dirty(handle, bh);
1592 	ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1593 }
1594 
1595 /*
1596  * This expects that a journal write has been reserved on
1597  * lookup->dl_prev_leaf_bh or lookup->dl_dx_root_bh
1598  */
ocfs2_recalc_free_list(struct inode * dir,handle_t * handle,struct ocfs2_dir_lookup_result * lookup)1599 static void ocfs2_recalc_free_list(struct inode *dir, handle_t *handle,
1600 				   struct ocfs2_dir_lookup_result *lookup)
1601 {
1602 	int max_rec_len;
1603 	struct ocfs2_dir_block_trailer *trailer;
1604 
1605 	/* Walk dl_leaf_bh to figure out what the new free rec_len is. */
1606 	max_rec_len = ocfs2_find_max_rec_len(dir->i_sb, lookup->dl_leaf_bh);
1607 	if (max_rec_len) {
1608 		/*
1609 		 * There's still room in this block, so no need to remove it
1610 		 * from the free list. In this case, we just want to update
1611 		 * the rec len accounting.
1612 		 */
1613 		trailer = ocfs2_trailer_from_bh(lookup->dl_leaf_bh, dir->i_sb);
1614 		trailer->db_free_rec_len = cpu_to_le16(max_rec_len);
1615 		ocfs2_journal_dirty(handle, lookup->dl_leaf_bh);
1616 	} else {
1617 		ocfs2_remove_block_from_free_list(dir, handle, lookup);
1618 	}
1619 }
1620 
1621 /* we don't always have a dentry for what we want to add, so people
1622  * like orphan dir can call this instead.
1623  *
1624  * The lookup context must have been filled from
1625  * ocfs2_prepare_dir_for_insert.
1626  */
__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,struct ocfs2_dir_lookup_result * lookup)1627 int __ocfs2_add_entry(handle_t *handle,
1628 		      struct inode *dir,
1629 		      const char *name, int namelen,
1630 		      struct inode *inode, u64 blkno,
1631 		      struct buffer_head *parent_fe_bh,
1632 		      struct ocfs2_dir_lookup_result *lookup)
1633 {
1634 	unsigned long offset;
1635 	unsigned short rec_len;
1636 	struct ocfs2_dir_entry *de, *de1;
1637 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)parent_fe_bh->b_data;
1638 	struct super_block *sb = dir->i_sb;
1639 	int retval;
1640 	unsigned int size = sb->s_blocksize;
1641 	struct buffer_head *insert_bh = lookup->dl_leaf_bh;
1642 	char *data_start = insert_bh->b_data;
1643 
1644 	if (ocfs2_dir_indexed(dir)) {
1645 		struct buffer_head *bh;
1646 
1647 		/*
1648 		 * An indexed dir may require that we update the free space
1649 		 * list. Reserve a write to the previous node in the list so
1650 		 * that we don't fail later.
1651 		 *
1652 		 * XXX: This can be either a dx_root_block, or an unindexed
1653 		 * directory tree leaf block.
1654 		 */
1655 		if (ocfs2_free_list_at_root(lookup)) {
1656 			bh = lookup->dl_dx_root_bh;
1657 			retval = ocfs2_journal_access_dr(handle,
1658 						 INODE_CACHE(dir), bh,
1659 						 OCFS2_JOURNAL_ACCESS_WRITE);
1660 		} else {
1661 			bh = lookup->dl_prev_leaf_bh;
1662 			retval = ocfs2_journal_access_db(handle,
1663 						 INODE_CACHE(dir), bh,
1664 						 OCFS2_JOURNAL_ACCESS_WRITE);
1665 		}
1666 		if (retval) {
1667 			mlog_errno(retval);
1668 			return retval;
1669 		}
1670 	} else if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1671 		data_start = di->id2.i_data.id_data;
1672 		size = i_size_read(dir);
1673 
1674 		BUG_ON(insert_bh != parent_fe_bh);
1675 	}
1676 
1677 	rec_len = OCFS2_DIR_REC_LEN(namelen);
1678 	offset = 0;
1679 	de = (struct ocfs2_dir_entry *) data_start;
1680 	while (1) {
1681 		BUG_ON((char *)de >= (size + data_start));
1682 
1683 		/* These checks should've already been passed by the
1684 		 * prepare function, but I guess we can leave them
1685 		 * here anyway. */
1686 		if (!ocfs2_check_dir_entry(dir, de, insert_bh, data_start,
1687 					   size, offset)) {
1688 			retval = -ENOENT;
1689 			goto bail;
1690 		}
1691 		if (ocfs2_match(namelen, name, de)) {
1692 			retval = -EEXIST;
1693 			goto bail;
1694 		}
1695 
1696 		/* We're guaranteed that we should have space, so we
1697 		 * can't possibly have hit the trailer...right? */
1698 		mlog_bug_on_msg(ocfs2_skip_dir_trailer(dir, de, offset, size),
1699 				"Hit dir trailer trying to insert %.*s "
1700 			        "(namelen %d) into directory %llu.  "
1701 				"offset is %lu, trailer offset is %d\n",
1702 				namelen, name, namelen,
1703 				(unsigned long long)parent_fe_bh->b_blocknr,
1704 				offset, ocfs2_dir_trailer_blk_off(dir->i_sb));
1705 
1706 		if (ocfs2_dirent_would_fit(de, rec_len)) {
1707 			inode_set_mtime_to_ts(dir,
1708 					      inode_set_ctime_current(dir));
1709 			retval = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
1710 			if (retval < 0) {
1711 				mlog_errno(retval);
1712 				goto bail;
1713 			}
1714 
1715 			if (insert_bh == parent_fe_bh)
1716 				retval = ocfs2_journal_access_di(handle,
1717 								 INODE_CACHE(dir),
1718 								 insert_bh,
1719 								 OCFS2_JOURNAL_ACCESS_WRITE);
1720 			else {
1721 				retval = ocfs2_journal_access_db(handle,
1722 								 INODE_CACHE(dir),
1723 								 insert_bh,
1724 					      OCFS2_JOURNAL_ACCESS_WRITE);
1725 
1726 				if (!retval && ocfs2_dir_indexed(dir))
1727 					retval = ocfs2_dx_dir_insert(dir,
1728 								handle,
1729 								lookup);
1730 			}
1731 
1732 			if (retval) {
1733 				mlog_errno(retval);
1734 				goto bail;
1735 			}
1736 
1737 			/* By now the buffer is marked for journaling */
1738 			offset += le16_to_cpu(de->rec_len);
1739 			if (le64_to_cpu(de->inode)) {
1740 				de1 = (struct ocfs2_dir_entry *)((char *) de +
1741 					OCFS2_DIR_REC_LEN(de->name_len));
1742 				de1->rec_len =
1743 					cpu_to_le16(le16_to_cpu(de->rec_len) -
1744 					OCFS2_DIR_REC_LEN(de->name_len));
1745 				de->rec_len = cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
1746 				de = de1;
1747 			}
1748 			de->file_type = FT_UNKNOWN;
1749 			if (blkno) {
1750 				de->inode = cpu_to_le64(blkno);
1751 				ocfs2_set_de_type(de, inode->i_mode);
1752 			} else
1753 				de->inode = 0;
1754 			de->name_len = namelen;
1755 			memcpy(de->name, name, namelen);
1756 
1757 			if (ocfs2_dir_indexed(dir))
1758 				ocfs2_recalc_free_list(dir, handle, lookup);
1759 
1760 			inode_inc_iversion(dir);
1761 			ocfs2_journal_dirty(handle, insert_bh);
1762 			retval = 0;
1763 			goto bail;
1764 		}
1765 
1766 		offset += le16_to_cpu(de->rec_len);
1767 		de = (struct ocfs2_dir_entry *) ((char *) de + le16_to_cpu(de->rec_len));
1768 	}
1769 
1770 	/* when you think about it, the assert above should prevent us
1771 	 * from ever getting here. */
1772 	retval = -ENOSPC;
1773 bail:
1774 	if (retval)
1775 		mlog_errno(retval);
1776 
1777 	return retval;
1778 }
1779 
ocfs2_dir_foreach_blk_id(struct inode * inode,u64 * f_version,struct dir_context * ctx)1780 static int ocfs2_dir_foreach_blk_id(struct inode *inode,
1781 				    u64 *f_version,
1782 				    struct dir_context *ctx)
1783 {
1784 	int ret, i;
1785 	unsigned long offset = ctx->pos;
1786 	struct buffer_head *di_bh = NULL;
1787 	struct ocfs2_dinode *di;
1788 	struct ocfs2_inline_data *data;
1789 	struct ocfs2_dir_entry *de;
1790 
1791 	ret = ocfs2_read_inode_block(inode, &di_bh);
1792 	if (ret) {
1793 		mlog(ML_ERROR, "Unable to read inode block for dir %llu\n",
1794 		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
1795 		goto out;
1796 	}
1797 
1798 	di = (struct ocfs2_dinode *)di_bh->b_data;
1799 	data = &di->id2.i_data;
1800 
1801 	while (ctx->pos < i_size_read(inode)) {
1802 		/* If the dir block has changed since the last call to
1803 		 * readdir(2), then we might be pointing to an invalid
1804 		 * dirent right now.  Scan from the start of the block
1805 		 * to make sure. */
1806 		if (!inode_eq_iversion(inode, *f_version)) {
1807 			for (i = 0; i < i_size_read(inode) && i < offset; ) {
1808 				de = (struct ocfs2_dir_entry *)
1809 					(data->id_data + i);
1810 				/* It's too expensive to do a full
1811 				 * dirent test each time round this
1812 				 * loop, but we do have to test at
1813 				 * least that it is non-zero.  A
1814 				 * failure will be detected in the
1815 				 * dirent test below. */
1816 				if (le16_to_cpu(de->rec_len) <
1817 				    OCFS2_DIR_REC_LEN(1))
1818 					break;
1819 				i += le16_to_cpu(de->rec_len);
1820 			}
1821 			ctx->pos = offset = i;
1822 			*f_version = inode_query_iversion(inode);
1823 		}
1824 
1825 		de = (struct ocfs2_dir_entry *) (data->id_data + ctx->pos);
1826 		if (!ocfs2_check_dir_entry(inode, de, di_bh, (char *)data->id_data,
1827 					   i_size_read(inode), ctx->pos)) {
1828 			/* On error, skip the f_pos to the end. */
1829 			ctx->pos = i_size_read(inode);
1830 			break;
1831 		}
1832 		offset += le16_to_cpu(de->rec_len);
1833 		if (le64_to_cpu(de->inode)) {
1834 			if (!dir_emit(ctx, de->name, de->name_len,
1835 				      le64_to_cpu(de->inode),
1836 				      fs_ftype_to_dtype(de->file_type)))
1837 				goto out;
1838 		}
1839 		ctx->pos += le16_to_cpu(de->rec_len);
1840 	}
1841 out:
1842 	brelse(di_bh);
1843 	return 0;
1844 }
1845 
1846 /*
1847  * NOTE: This function can be called against unindexed directories,
1848  * and indexed ones.
1849  */
ocfs2_dir_foreach_blk_el(struct inode * inode,u64 * f_version,struct dir_context * ctx,bool persist)1850 static int ocfs2_dir_foreach_blk_el(struct inode *inode,
1851 				    u64 *f_version,
1852 				    struct dir_context *ctx,
1853 				    bool persist)
1854 {
1855 	unsigned long offset, blk, last_ra_blk = 0;
1856 	int i;
1857 	struct buffer_head * bh, * tmp;
1858 	struct ocfs2_dir_entry * de;
1859 	struct super_block * sb = inode->i_sb;
1860 	unsigned int ra_sectors = 16;
1861 	int stored = 0;
1862 
1863 	bh = NULL;
1864 
1865 	offset = ctx->pos & (sb->s_blocksize - 1);
1866 
1867 	while (ctx->pos < i_size_read(inode)) {
1868 		blk = ctx->pos >> sb->s_blocksize_bits;
1869 		if (ocfs2_read_dir_block(inode, blk, &bh, 0)) {
1870 			/* Skip the corrupt dirblock and keep trying */
1871 			ctx->pos += sb->s_blocksize - offset;
1872 			continue;
1873 		}
1874 
1875 		/* The idea here is to begin with 8k read-ahead and to stay
1876 		 * 4k ahead of our current position.
1877 		 *
1878 		 * TODO: Use the pagecache for this. We just need to
1879 		 * make sure it's cluster-safe... */
1880 		if (!last_ra_blk
1881 		    || (((last_ra_blk - blk) << 9) <= (ra_sectors / 2))) {
1882 			for (i = ra_sectors >> (sb->s_blocksize_bits - 9);
1883 			     i > 0; i--) {
1884 				tmp = NULL;
1885 				if (!ocfs2_read_dir_block(inode, ++blk, &tmp,
1886 							  OCFS2_BH_READAHEAD))
1887 					brelse(tmp);
1888 			}
1889 			last_ra_blk = blk;
1890 			ra_sectors = 8;
1891 		}
1892 
1893 		/* If the dir block has changed since the last call to
1894 		 * readdir(2), then we might be pointing to an invalid
1895 		 * dirent right now.  Scan from the start of the block
1896 		 * to make sure. */
1897 		if (!inode_eq_iversion(inode, *f_version)) {
1898 			for (i = 0; i < sb->s_blocksize && i < offset; ) {
1899 				de = (struct ocfs2_dir_entry *) (bh->b_data + i);
1900 				/* It's too expensive to do a full
1901 				 * dirent test each time round this
1902 				 * loop, but we do have to test at
1903 				 * least that it is non-zero.  A
1904 				 * failure will be detected in the
1905 				 * dirent test below. */
1906 				if (le16_to_cpu(de->rec_len) <
1907 				    OCFS2_DIR_REC_LEN(1))
1908 					break;
1909 				i += le16_to_cpu(de->rec_len);
1910 			}
1911 			offset = i;
1912 			ctx->pos = (ctx->pos & ~(sb->s_blocksize - 1))
1913 				| offset;
1914 			*f_version = inode_query_iversion(inode);
1915 		}
1916 
1917 		while (ctx->pos < i_size_read(inode)
1918 		       && offset < sb->s_blocksize) {
1919 			de = (struct ocfs2_dir_entry *) (bh->b_data + offset);
1920 			if (!ocfs2_check_dir_entry(inode, de, bh, bh->b_data,
1921 						   sb->s_blocksize, offset)) {
1922 				/* On error, skip the f_pos to the
1923 				   next block. */
1924 				ctx->pos = (ctx->pos | (sb->s_blocksize - 1)) + 1;
1925 				break;
1926 			}
1927 			if (le64_to_cpu(de->inode)) {
1928 				if (!dir_emit(ctx, de->name,
1929 						de->name_len,
1930 						le64_to_cpu(de->inode),
1931 					fs_ftype_to_dtype(de->file_type))) {
1932 					brelse(bh);
1933 					return 0;
1934 				}
1935 				stored++;
1936 			}
1937 			offset += le16_to_cpu(de->rec_len);
1938 			ctx->pos += le16_to_cpu(de->rec_len);
1939 		}
1940 		offset = 0;
1941 		brelse(bh);
1942 		bh = NULL;
1943 		if (!persist && stored)
1944 			break;
1945 	}
1946 	return 0;
1947 }
1948 
ocfs2_dir_foreach_blk(struct inode * inode,u64 * f_version,struct dir_context * ctx,bool persist)1949 static int ocfs2_dir_foreach_blk(struct inode *inode, u64 *f_version,
1950 				 struct dir_context *ctx,
1951 				 bool persist)
1952 {
1953 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
1954 		return ocfs2_dir_foreach_blk_id(inode, f_version, ctx);
1955 	return ocfs2_dir_foreach_blk_el(inode, f_version, ctx, persist);
1956 }
1957 
1958 /*
1959  * This is intended to be called from inside other kernel functions,
1960  * so we fake some arguments.
1961  */
ocfs2_dir_foreach(struct inode * inode,struct dir_context * ctx)1962 int ocfs2_dir_foreach(struct inode *inode, struct dir_context *ctx)
1963 {
1964 	u64 version = inode_query_iversion(inode);
1965 	ocfs2_dir_foreach_blk(inode, &version, ctx, true);
1966 	return 0;
1967 }
1968 
1969 /*
1970  * ocfs2_readdir()
1971  *
1972  */
ocfs2_readdir(struct file * file,struct dir_context * ctx)1973 int ocfs2_readdir(struct file *file, struct dir_context *ctx)
1974 {
1975 	int error = 0;
1976 	struct inode *inode = file_inode(file);
1977 	struct ocfs2_file_private *fp = file->private_data;
1978 	int lock_level = 0;
1979 
1980 	trace_ocfs2_readdir((unsigned long long)OCFS2_I(inode)->ip_blkno);
1981 
1982 	error = ocfs2_inode_lock_atime(inode, file->f_path.mnt, &lock_level, 1);
1983 	if (lock_level && error >= 0) {
1984 		/* We release EX lock which used to update atime
1985 		 * and get PR lock again to reduce contention
1986 		 * on commonly accessed directories. */
1987 		ocfs2_inode_unlock(inode, 1);
1988 		lock_level = 0;
1989 		error = ocfs2_inode_lock(inode, NULL, 0);
1990 	}
1991 	if (error < 0) {
1992 		if (error != -ENOENT)
1993 			mlog_errno(error);
1994 		/* we haven't got any yet, so propagate the error. */
1995 		goto bail_nolock;
1996 	}
1997 
1998 	error = ocfs2_dir_foreach_blk(inode, &fp->cookie, ctx, false);
1999 
2000 	ocfs2_inode_unlock(inode, lock_level);
2001 	if (error)
2002 		mlog_errno(error);
2003 
2004 bail_nolock:
2005 
2006 	return error;
2007 }
2008 
2009 /*
2010  * NOTE: this should always be called with parent dir i_rwsem taken.
2011  */
ocfs2_find_files_on_disk(const char * name,int namelen,u64 * blkno,struct inode * inode,struct ocfs2_dir_lookup_result * lookup)2012 int ocfs2_find_files_on_disk(const char *name,
2013 			     int namelen,
2014 			     u64 *blkno,
2015 			     struct inode *inode,
2016 			     struct ocfs2_dir_lookup_result *lookup)
2017 {
2018 	int status = -ENOENT;
2019 
2020 	trace_ocfs2_find_files_on_disk(namelen, name, blkno,
2021 				(unsigned long long)OCFS2_I(inode)->ip_blkno);
2022 
2023 	status = ocfs2_find_entry(name, namelen, inode, lookup);
2024 	if (status)
2025 		goto leave;
2026 
2027 	*blkno = le64_to_cpu(lookup->dl_entry->inode);
2028 
2029 	status = 0;
2030 leave:
2031 
2032 	return status;
2033 }
2034 
2035 /*
2036  * Convenience function for callers which just want the block number
2037  * mapped to a name and don't require the full dirent info, etc.
2038  */
ocfs2_lookup_ino_from_name(struct inode * dir,const char * name,int namelen,u64 * blkno)2039 int ocfs2_lookup_ino_from_name(struct inode *dir, const char *name,
2040 			       int namelen, u64 *blkno)
2041 {
2042 	int ret;
2043 	struct ocfs2_dir_lookup_result lookup = { NULL, };
2044 
2045 	ret = ocfs2_find_files_on_disk(name, namelen, blkno, dir, &lookup);
2046 	ocfs2_free_dir_lookup_result(&lookup);
2047 
2048 	return ret;
2049 }
2050 
2051 /* Check for a name within a directory.
2052  *
2053  * Return 0 if the name does not exist
2054  * Return -EEXIST if the directory contains the name
2055  * Return -EFSCORRUPTED if found corruption
2056  *
2057  * Callers should have i_rwsem + a cluster lock on dir
2058  */
ocfs2_check_dir_for_entry(struct inode * dir,const char * name,int namelen)2059 int ocfs2_check_dir_for_entry(struct inode *dir,
2060 			      const char *name,
2061 			      int namelen)
2062 {
2063 	int ret = 0;
2064 	struct ocfs2_dir_lookup_result lookup = { NULL, };
2065 
2066 	trace_ocfs2_check_dir_for_entry(
2067 		(unsigned long long)OCFS2_I(dir)->ip_blkno, namelen, name);
2068 
2069 	ret = ocfs2_find_entry(name, namelen, dir, &lookup);
2070 	if (ret == 0) {
2071 		ret = -EEXIST;
2072 		mlog_errno(ret);
2073 	} else if (ret == -ENOENT) {
2074 		ret = 0;
2075 	}
2076 
2077 	ocfs2_free_dir_lookup_result(&lookup);
2078 
2079 	return ret;
2080 }
2081 
2082 struct ocfs2_empty_dir_priv {
2083 	struct dir_context ctx;
2084 	unsigned seen_dot;
2085 	unsigned seen_dot_dot;
2086 	unsigned seen_other;
2087 	unsigned dx_dir;
2088 };
ocfs2_empty_dir_filldir(struct dir_context * ctx,const char * name,int name_len,loff_t pos,u64 ino,unsigned type)2089 static bool ocfs2_empty_dir_filldir(struct dir_context *ctx, const char *name,
2090 				   int name_len, loff_t pos, u64 ino,
2091 				   unsigned type)
2092 {
2093 	struct ocfs2_empty_dir_priv *p =
2094 		container_of(ctx, struct ocfs2_empty_dir_priv, ctx);
2095 
2096 	/*
2097 	 * Check the positions of "." and ".." records to be sure
2098 	 * they're in the correct place.
2099 	 *
2100 	 * Indexed directories don't need to proceed past the first
2101 	 * two entries, so we end the scan after seeing '..'. Despite
2102 	 * that, we allow the scan to proceed In the event that we
2103 	 * have a corrupted indexed directory (no dot or dot dot
2104 	 * entries). This allows us to double check for existing
2105 	 * entries which might not have been found in the index.
2106 	 */
2107 	if (name_len == 1 && !strncmp(".", name, 1) && pos == 0) {
2108 		p->seen_dot = 1;
2109 		return true;
2110 	}
2111 
2112 	if (name_len == 2 && !strncmp("..", name, 2) &&
2113 	    pos == OCFS2_DIR_REC_LEN(1)) {
2114 		p->seen_dot_dot = 1;
2115 
2116 		if (p->dx_dir && p->seen_dot)
2117 			return false;
2118 
2119 		return true;
2120 	}
2121 
2122 	p->seen_other = 1;
2123 	return false;
2124 }
2125 
ocfs2_empty_dir_dx(struct inode * inode,struct ocfs2_empty_dir_priv * priv)2126 static int ocfs2_empty_dir_dx(struct inode *inode,
2127 			      struct ocfs2_empty_dir_priv *priv)
2128 {
2129 	int ret;
2130 	struct buffer_head *di_bh = NULL;
2131 	struct buffer_head *dx_root_bh = NULL;
2132 	struct ocfs2_dinode *di;
2133 	struct ocfs2_dx_root_block *dx_root;
2134 
2135 	priv->dx_dir = 1;
2136 
2137 	ret = ocfs2_read_inode_block(inode, &di_bh);
2138 	if (ret) {
2139 		mlog_errno(ret);
2140 		goto out;
2141 	}
2142 	di = (struct ocfs2_dinode *)di_bh->b_data;
2143 
2144 	ret = ocfs2_read_dx_root(inode, di, &dx_root_bh);
2145 	if (ret) {
2146 		mlog_errno(ret);
2147 		goto out;
2148 	}
2149 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2150 
2151 	if (le32_to_cpu(dx_root->dr_num_entries) != 2)
2152 		priv->seen_other = 1;
2153 
2154 out:
2155 	brelse(di_bh);
2156 	brelse(dx_root_bh);
2157 	return ret;
2158 }
2159 
2160 /*
2161  * routine to check that the specified directory is empty (for rmdir)
2162  *
2163  * Returns 1 if dir is empty, zero otherwise.
2164  *
2165  * XXX: This is a performance problem for unindexed directories.
2166  */
ocfs2_empty_dir(struct inode * inode)2167 int ocfs2_empty_dir(struct inode *inode)
2168 {
2169 	int ret;
2170 	struct ocfs2_empty_dir_priv priv = {
2171 		.ctx.actor = ocfs2_empty_dir_filldir,
2172 	};
2173 
2174 	if (ocfs2_dir_indexed(inode)) {
2175 		ret = ocfs2_empty_dir_dx(inode, &priv);
2176 		if (ret)
2177 			mlog_errno(ret);
2178 		/*
2179 		 * We still run ocfs2_dir_foreach to get the checks
2180 		 * for "." and "..".
2181 		 */
2182 	}
2183 
2184 	ret = ocfs2_dir_foreach(inode, &priv.ctx);
2185 	if (ret)
2186 		mlog_errno(ret);
2187 
2188 	if (!priv.seen_dot || !priv.seen_dot_dot) {
2189 		mlog(ML_ERROR, "bad directory (dir #%llu) - no `.' or `..'\n",
2190 		     (unsigned long long)OCFS2_I(inode)->ip_blkno);
2191 		/*
2192 		 * XXX: Is it really safe to allow an unlink to continue?
2193 		 */
2194 		return 1;
2195 	}
2196 
2197 	return !priv.seen_other;
2198 }
2199 
2200 /*
2201  * Fills "." and ".." dirents in a new directory block. Returns dirent for
2202  * "..", which might be used during creation of a directory with a trailing
2203  * header. It is otherwise safe to ignore the return code.
2204  */
ocfs2_fill_initial_dirents(struct inode * inode,struct inode * parent,char * start,unsigned int size)2205 static struct ocfs2_dir_entry *ocfs2_fill_initial_dirents(struct inode *inode,
2206 							  struct inode *parent,
2207 							  char *start,
2208 							  unsigned int size)
2209 {
2210 	struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)start;
2211 
2212 	de->inode = cpu_to_le64(OCFS2_I(inode)->ip_blkno);
2213 	de->name_len = 1;
2214 	de->rec_len =
2215 		cpu_to_le16(OCFS2_DIR_REC_LEN(de->name_len));
2216 	strcpy(de->name, ".");
2217 	ocfs2_set_de_type(de, S_IFDIR);
2218 
2219 	de = (struct ocfs2_dir_entry *) ((char *)de + le16_to_cpu(de->rec_len));
2220 	de->inode = cpu_to_le64(OCFS2_I(parent)->ip_blkno);
2221 	de->rec_len = cpu_to_le16(size - OCFS2_DIR_REC_LEN(1));
2222 	de->name_len = 2;
2223 	strcpy(de->name, "..");
2224 	ocfs2_set_de_type(de, S_IFDIR);
2225 
2226 	return de;
2227 }
2228 
2229 /*
2230  * This works together with code in ocfs2_mknod_locked() which sets
2231  * the inline-data flag and initializes the inline-data section.
2232  */
ocfs2_fill_new_dir_id(struct ocfs2_super * osb,handle_t * handle,struct inode * parent,struct inode * inode,struct buffer_head * di_bh)2233 static int ocfs2_fill_new_dir_id(struct ocfs2_super *osb,
2234 				 handle_t *handle,
2235 				 struct inode *parent,
2236 				 struct inode *inode,
2237 				 struct buffer_head *di_bh)
2238 {
2239 	int ret;
2240 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2241 	struct ocfs2_inline_data *data = &di->id2.i_data;
2242 	unsigned int size = le16_to_cpu(data->id_count);
2243 
2244 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2245 				      OCFS2_JOURNAL_ACCESS_WRITE);
2246 	if (ret) {
2247 		mlog_errno(ret);
2248 		goto out;
2249 	}
2250 
2251 	ocfs2_fill_initial_dirents(inode, parent, data->id_data, size);
2252 	ocfs2_journal_dirty(handle, di_bh);
2253 
2254 	i_size_write(inode, size);
2255 	set_nlink(inode, 2);
2256 	inode->i_blocks = ocfs2_inode_sector_count(inode);
2257 
2258 	ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
2259 	if (ret < 0)
2260 		mlog_errno(ret);
2261 
2262 out:
2263 	return ret;
2264 }
2265 
ocfs2_fill_new_dir_el(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 buffer_head ** ret_new_bh)2266 static int ocfs2_fill_new_dir_el(struct ocfs2_super *osb,
2267 				 handle_t *handle,
2268 				 struct inode *parent,
2269 				 struct inode *inode,
2270 				 struct buffer_head *fe_bh,
2271 				 struct ocfs2_alloc_context *data_ac,
2272 				 struct buffer_head **ret_new_bh)
2273 {
2274 	int status;
2275 	unsigned int size = osb->sb->s_blocksize;
2276 	struct buffer_head *new_bh = NULL;
2277 	struct ocfs2_dir_entry *de;
2278 
2279 	if (ocfs2_new_dir_wants_trailer(inode))
2280 		size = ocfs2_dir_trailer_blk_off(parent->i_sb);
2281 
2282 	status = ocfs2_do_extend_dir(osb->sb, handle, inode, fe_bh,
2283 				     data_ac, NULL, &new_bh);
2284 	if (status < 0) {
2285 		mlog_errno(status);
2286 		goto bail;
2287 	}
2288 
2289 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2290 
2291 	status = ocfs2_journal_access_db(handle, INODE_CACHE(inode), new_bh,
2292 					 OCFS2_JOURNAL_ACCESS_CREATE);
2293 	if (status < 0) {
2294 		mlog_errno(status);
2295 		goto bail;
2296 	}
2297 	memset(new_bh->b_data, 0, osb->sb->s_blocksize);
2298 
2299 	de = ocfs2_fill_initial_dirents(inode, parent, new_bh->b_data, size);
2300 	if (ocfs2_new_dir_wants_trailer(inode)) {
2301 		int size = le16_to_cpu(de->rec_len);
2302 
2303 		/*
2304 		 * Figure out the size of the hole left over after
2305 		 * insertion of '.' and '..'. The trailer wants this
2306 		 * information.
2307 		 */
2308 		size -= OCFS2_DIR_REC_LEN(2);
2309 		size -= sizeof(struct ocfs2_dir_block_trailer);
2310 
2311 		ocfs2_init_dir_trailer(inode, new_bh, size);
2312 	}
2313 
2314 	ocfs2_journal_dirty(handle, new_bh);
2315 
2316 	i_size_write(inode, inode->i_sb->s_blocksize);
2317 	set_nlink(inode, 2);
2318 	inode->i_blocks = ocfs2_inode_sector_count(inode);
2319 	status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
2320 	if (status < 0) {
2321 		mlog_errno(status);
2322 		goto bail;
2323 	}
2324 
2325 	status = 0;
2326 	if (ret_new_bh) {
2327 		*ret_new_bh = new_bh;
2328 		new_bh = NULL;
2329 	}
2330 bail:
2331 	brelse(new_bh);
2332 
2333 	return status;
2334 }
2335 
ocfs2_dx_dir_attach_index(struct ocfs2_super * osb,handle_t * handle,struct inode * dir,struct buffer_head * di_bh,struct buffer_head * dirdata_bh,struct ocfs2_alloc_context * meta_ac,int dx_inline,u32 num_entries,struct buffer_head ** ret_dx_root_bh)2336 static int ocfs2_dx_dir_attach_index(struct ocfs2_super *osb,
2337 				     handle_t *handle, struct inode *dir,
2338 				     struct buffer_head *di_bh,
2339 				     struct buffer_head *dirdata_bh,
2340 				     struct ocfs2_alloc_context *meta_ac,
2341 				     int dx_inline, u32 num_entries,
2342 				     struct buffer_head **ret_dx_root_bh)
2343 {
2344 	int ret;
2345 	struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data;
2346 	u16 dr_suballoc_bit;
2347 	u64 suballoc_loc, dr_blkno;
2348 	unsigned int num_bits;
2349 	struct buffer_head *dx_root_bh = NULL;
2350 	struct ocfs2_dx_root_block *dx_root;
2351 	struct ocfs2_dir_block_trailer *trailer =
2352 		ocfs2_trailer_from_bh(dirdata_bh, dir->i_sb);
2353 
2354 	ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
2355 				   &dr_suballoc_bit, &num_bits, &dr_blkno);
2356 	if (ret) {
2357 		mlog_errno(ret);
2358 		goto out;
2359 	}
2360 
2361 	trace_ocfs2_dx_dir_attach_index(
2362 				(unsigned long long)OCFS2_I(dir)->ip_blkno,
2363 				(unsigned long long)dr_blkno);
2364 
2365 	dx_root_bh = sb_getblk(osb->sb, dr_blkno);
2366 	if (dx_root_bh == NULL) {
2367 		ret = -ENOMEM;
2368 		goto out;
2369 	}
2370 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dx_root_bh);
2371 
2372 	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
2373 				      OCFS2_JOURNAL_ACCESS_CREATE);
2374 	if (ret < 0) {
2375 		mlog_errno(ret);
2376 		goto out;
2377 	}
2378 
2379 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2380 	memset(dx_root, 0, osb->sb->s_blocksize);
2381 	strcpy(dx_root->dr_signature, OCFS2_DX_ROOT_SIGNATURE);
2382 	dx_root->dr_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
2383 	dx_root->dr_suballoc_loc = cpu_to_le64(suballoc_loc);
2384 	dx_root->dr_suballoc_bit = cpu_to_le16(dr_suballoc_bit);
2385 	dx_root->dr_fs_generation = cpu_to_le32(osb->fs_generation);
2386 	dx_root->dr_blkno = cpu_to_le64(dr_blkno);
2387 	dx_root->dr_dir_blkno = cpu_to_le64(OCFS2_I(dir)->ip_blkno);
2388 	dx_root->dr_num_entries = cpu_to_le32(num_entries);
2389 	if (le16_to_cpu(trailer->db_free_rec_len))
2390 		dx_root->dr_free_blk = cpu_to_le64(dirdata_bh->b_blocknr);
2391 	else
2392 		dx_root->dr_free_blk = cpu_to_le64(0);
2393 
2394 	if (dx_inline) {
2395 		dx_root->dr_flags |= OCFS2_DX_FLAG_INLINE;
2396 		dx_root->dr_entries.de_count =
2397 			cpu_to_le16(ocfs2_dx_entries_per_root(osb->sb));
2398 	} else {
2399 		dx_root->dr_list.l_count =
2400 			cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
2401 	}
2402 	ocfs2_journal_dirty(handle, dx_root_bh);
2403 
2404 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
2405 				      OCFS2_JOURNAL_ACCESS_CREATE);
2406 	if (ret) {
2407 		mlog_errno(ret);
2408 		goto out;
2409 	}
2410 
2411 	di->i_dx_root = cpu_to_le64(dr_blkno);
2412 
2413 	spin_lock(&OCFS2_I(dir)->ip_lock);
2414 	OCFS2_I(dir)->ip_dyn_features |= OCFS2_INDEXED_DIR_FL;
2415 	di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
2416 	spin_unlock(&OCFS2_I(dir)->ip_lock);
2417 
2418 	ocfs2_journal_dirty(handle, di_bh);
2419 
2420 	*ret_dx_root_bh = dx_root_bh;
2421 	dx_root_bh = NULL;
2422 
2423 out:
2424 	brelse(dx_root_bh);
2425 	return ret;
2426 }
2427 
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)2428 static int ocfs2_dx_dir_format_cluster(struct ocfs2_super *osb,
2429 				       handle_t *handle, struct inode *dir,
2430 				       struct buffer_head **dx_leaves,
2431 				       int num_dx_leaves, u64 start_blk)
2432 {
2433 	int ret, i;
2434 	struct ocfs2_dx_leaf *dx_leaf;
2435 	struct buffer_head *bh;
2436 
2437 	for (i = 0; i < num_dx_leaves; i++) {
2438 		bh = sb_getblk(osb->sb, start_blk + i);
2439 		if (bh == NULL) {
2440 			ret = -ENOMEM;
2441 			goto out;
2442 		}
2443 		dx_leaves[i] = bh;
2444 
2445 		ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), bh);
2446 
2447 		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), bh,
2448 					      OCFS2_JOURNAL_ACCESS_CREATE);
2449 		if (ret < 0) {
2450 			mlog_errno(ret);
2451 			goto out;
2452 		}
2453 
2454 		dx_leaf = (struct ocfs2_dx_leaf *) bh->b_data;
2455 
2456 		memset(dx_leaf, 0, osb->sb->s_blocksize);
2457 		strcpy(dx_leaf->dl_signature, OCFS2_DX_LEAF_SIGNATURE);
2458 		dx_leaf->dl_fs_generation = cpu_to_le32(osb->fs_generation);
2459 		dx_leaf->dl_blkno = cpu_to_le64(bh->b_blocknr);
2460 		dx_leaf->dl_list.de_count =
2461 			cpu_to_le16(ocfs2_dx_entries_per_leaf(osb->sb));
2462 
2463 		trace_ocfs2_dx_dir_format_cluster(
2464 				(unsigned long long)OCFS2_I(dir)->ip_blkno,
2465 				(unsigned long long)bh->b_blocknr,
2466 				le16_to_cpu(dx_leaf->dl_list.de_count));
2467 
2468 		ocfs2_journal_dirty(handle, bh);
2469 	}
2470 
2471 	ret = 0;
2472 out:
2473 	return ret;
2474 }
2475 
2476 /*
2477  * Allocates and formats a new cluster for use in an indexed dir
2478  * leaf. This version will not do the extent insert, so that it can be
2479  * used by operations which need careful ordering.
2480  */
__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)2481 static int __ocfs2_dx_dir_new_cluster(struct inode *dir,
2482 				      u32 cpos, handle_t *handle,
2483 				      struct ocfs2_alloc_context *data_ac,
2484 				      struct buffer_head **dx_leaves,
2485 				      int num_dx_leaves, u64 *ret_phys_blkno)
2486 {
2487 	int ret;
2488 	u32 phys, num;
2489 	u64 phys_blkno;
2490 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2491 
2492 	/*
2493 	 * XXX: For create, this should claim cluster for the index
2494 	 * *before* the unindexed insert so that we have a better
2495 	 * chance of contiguousness as the directory grows in number
2496 	 * of entries.
2497 	 */
2498 	ret = __ocfs2_claim_clusters(handle, data_ac, 1, 1, &phys, &num);
2499 	if (ret) {
2500 		mlog_errno(ret);
2501 		goto out;
2502 	}
2503 
2504 	/*
2505 	 * Format the new cluster first. That way, we're inserting
2506 	 * valid data.
2507 	 */
2508 	phys_blkno = ocfs2_clusters_to_blocks(osb->sb, phys);
2509 	ret = ocfs2_dx_dir_format_cluster(osb, handle, dir, dx_leaves,
2510 					  num_dx_leaves, phys_blkno);
2511 	if (ret) {
2512 		mlog_errno(ret);
2513 		goto out;
2514 	}
2515 
2516 	*ret_phys_blkno = phys_blkno;
2517 out:
2518 	return ret;
2519 }
2520 
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)2521 static int ocfs2_dx_dir_new_cluster(struct inode *dir,
2522 				    struct ocfs2_extent_tree *et,
2523 				    u32 cpos, handle_t *handle,
2524 				    struct ocfs2_alloc_context *data_ac,
2525 				    struct ocfs2_alloc_context *meta_ac,
2526 				    struct buffer_head **dx_leaves,
2527 				    int num_dx_leaves)
2528 {
2529 	int ret;
2530 	u64 phys_blkno;
2531 
2532 	ret = __ocfs2_dx_dir_new_cluster(dir, cpos, handle, data_ac, dx_leaves,
2533 					 num_dx_leaves, &phys_blkno);
2534 	if (ret) {
2535 		mlog_errno(ret);
2536 		goto out;
2537 	}
2538 
2539 	ret = ocfs2_insert_extent(handle, et, cpos, phys_blkno, 1, 0,
2540 				  meta_ac);
2541 	if (ret)
2542 		mlog_errno(ret);
2543 out:
2544 	return ret;
2545 }
2546 
ocfs2_dx_dir_kmalloc_leaves(struct super_block * sb,int * ret_num_leaves)2547 static struct buffer_head **ocfs2_dx_dir_kmalloc_leaves(struct super_block *sb,
2548 							int *ret_num_leaves)
2549 {
2550 	int num_dx_leaves = ocfs2_clusters_to_blocks(sb, 1);
2551 	struct buffer_head **dx_leaves;
2552 
2553 	dx_leaves = kcalloc(num_dx_leaves, sizeof(struct buffer_head *),
2554 			    GFP_NOFS);
2555 	if (dx_leaves && ret_num_leaves)
2556 		*ret_num_leaves = num_dx_leaves;
2557 
2558 	return dx_leaves;
2559 }
2560 
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)2561 static int ocfs2_fill_new_dir_dx(struct ocfs2_super *osb,
2562 				 handle_t *handle,
2563 				 struct inode *parent,
2564 				 struct inode *inode,
2565 				 struct buffer_head *di_bh,
2566 				 struct ocfs2_alloc_context *data_ac,
2567 				 struct ocfs2_alloc_context *meta_ac)
2568 {
2569 	int ret;
2570 	struct buffer_head *leaf_bh = NULL;
2571 	struct buffer_head *dx_root_bh = NULL;
2572 	struct ocfs2_dx_hinfo hinfo;
2573 	struct ocfs2_dx_root_block *dx_root;
2574 	struct ocfs2_dx_entry_list *entry_list;
2575 
2576 	/*
2577 	 * Our strategy is to create the directory as though it were
2578 	 * unindexed, then add the index block. This works with very
2579 	 * little complication since the state of a new directory is a
2580 	 * very well known quantity.
2581 	 *
2582 	 * Essentially, we have two dirents ("." and ".."), in the 1st
2583 	 * block which need indexing. These are easily inserted into
2584 	 * the index block.
2585 	 */
2586 
2587 	ret = ocfs2_fill_new_dir_el(osb, handle, parent, inode, di_bh,
2588 				    data_ac, &leaf_bh);
2589 	if (ret) {
2590 		mlog_errno(ret);
2591 		goto out;
2592 	}
2593 
2594 	ret = ocfs2_dx_dir_attach_index(osb, handle, inode, di_bh, leaf_bh,
2595 					meta_ac, 1, 2, &dx_root_bh);
2596 	if (ret) {
2597 		mlog_errno(ret);
2598 		goto out;
2599 	}
2600 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2601 	entry_list = &dx_root->dr_entries;
2602 
2603 	/* Buffer has been journaled for us by ocfs2_dx_dir_attach_index */
2604 	ocfs2_dx_dir_name_hash(inode, ".", 1, &hinfo);
2605 	ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2606 
2607 	ocfs2_dx_dir_name_hash(inode, "..", 2, &hinfo);
2608 	ocfs2_dx_entry_list_insert(entry_list, &hinfo, leaf_bh->b_blocknr);
2609 
2610 out:
2611 	brelse(dx_root_bh);
2612 	brelse(leaf_bh);
2613 	return ret;
2614 }
2615 
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)2616 int ocfs2_fill_new_dir(struct ocfs2_super *osb,
2617 		       handle_t *handle,
2618 		       struct inode *parent,
2619 		       struct inode *inode,
2620 		       struct buffer_head *fe_bh,
2621 		       struct ocfs2_alloc_context *data_ac,
2622 		       struct ocfs2_alloc_context *meta_ac)
2623 
2624 {
2625 	BUG_ON(!ocfs2_supports_inline_data(osb) && data_ac == NULL);
2626 
2627 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2628 		return ocfs2_fill_new_dir_id(osb, handle, parent, inode, fe_bh);
2629 
2630 	if (ocfs2_supports_indexed_dirs(osb))
2631 		return ocfs2_fill_new_dir_dx(osb, handle, parent, inode, fe_bh,
2632 					     data_ac, meta_ac);
2633 
2634 	return ocfs2_fill_new_dir_el(osb, handle, parent, inode, fe_bh,
2635 				     data_ac, NULL);
2636 }
2637 
ocfs2_dx_dir_index_block(struct inode * dir,handle_t * handle,struct buffer_head ** dx_leaves,int num_dx_leaves,u32 * num_dx_entries,struct buffer_head * dirent_bh)2638 static int ocfs2_dx_dir_index_block(struct inode *dir,
2639 				    handle_t *handle,
2640 				    struct buffer_head **dx_leaves,
2641 				    int num_dx_leaves,
2642 				    u32 *num_dx_entries,
2643 				    struct buffer_head *dirent_bh)
2644 {
2645 	int ret = 0, namelen, i;
2646 	char *de_buf, *limit;
2647 	struct ocfs2_dir_entry *de;
2648 	struct buffer_head *dx_leaf_bh;
2649 	struct ocfs2_dx_hinfo hinfo;
2650 	u64 dirent_blk = dirent_bh->b_blocknr;
2651 
2652 	de_buf = dirent_bh->b_data;
2653 	limit = de_buf + dir->i_sb->s_blocksize;
2654 
2655 	while (de_buf < limit) {
2656 		de = (struct ocfs2_dir_entry *)de_buf;
2657 
2658 		namelen = de->name_len;
2659 		if (!namelen || !de->inode)
2660 			goto inc;
2661 
2662 		ocfs2_dx_dir_name_hash(dir, de->name, namelen, &hinfo);
2663 
2664 		i = ocfs2_dx_dir_hash_idx(OCFS2_SB(dir->i_sb), &hinfo);
2665 		dx_leaf_bh = dx_leaves[i];
2666 
2667 		ret = __ocfs2_dx_dir_leaf_insert(dir, handle, &hinfo,
2668 						 dirent_blk, dx_leaf_bh);
2669 		if (ret) {
2670 			mlog_errno(ret);
2671 			goto out;
2672 		}
2673 
2674 		*num_dx_entries = *num_dx_entries + 1;
2675 
2676 inc:
2677 		de_buf += le16_to_cpu(de->rec_len);
2678 	}
2679 
2680 out:
2681 	return ret;
2682 }
2683 
2684 /*
2685  * XXX: This expects dx_root_bh to already be part of the transaction.
2686  */
ocfs2_dx_dir_index_root_block(struct inode * dir,struct buffer_head * dx_root_bh,struct buffer_head * dirent_bh)2687 static void ocfs2_dx_dir_index_root_block(struct inode *dir,
2688 					 struct buffer_head *dx_root_bh,
2689 					 struct buffer_head *dirent_bh)
2690 {
2691 	char *de_buf, *limit;
2692 	struct ocfs2_dx_root_block *dx_root;
2693 	struct ocfs2_dir_entry *de;
2694 	struct ocfs2_dx_hinfo hinfo;
2695 	u64 dirent_blk = dirent_bh->b_blocknr;
2696 
2697 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
2698 
2699 	de_buf = dirent_bh->b_data;
2700 	limit = de_buf + dir->i_sb->s_blocksize;
2701 
2702 	while (de_buf < limit) {
2703 		de = (struct ocfs2_dir_entry *)de_buf;
2704 
2705 		if (!de->name_len || !de->inode)
2706 			goto inc;
2707 
2708 		ocfs2_dx_dir_name_hash(dir, de->name, de->name_len, &hinfo);
2709 
2710 		trace_ocfs2_dx_dir_index_root_block(
2711 				(unsigned long long)dir->i_ino,
2712 				hinfo.major_hash, hinfo.minor_hash,
2713 				de->name_len, de->name,
2714 				le16_to_cpu(dx_root->dr_entries.de_num_used));
2715 
2716 		ocfs2_dx_entry_list_insert(&dx_root->dr_entries, &hinfo,
2717 					   dirent_blk);
2718 
2719 		le32_add_cpu(&dx_root->dr_num_entries, 1);
2720 inc:
2721 		de_buf += le16_to_cpu(de->rec_len);
2722 	}
2723 }
2724 
2725 /*
2726  * Count the number of inline directory entries in di_bh and compare
2727  * them against the number of entries we can hold in an inline dx root
2728  * block.
2729  */
ocfs2_new_dx_should_be_inline(struct inode * dir,struct buffer_head * di_bh)2730 static int ocfs2_new_dx_should_be_inline(struct inode *dir,
2731 					 struct buffer_head *di_bh)
2732 {
2733 	int dirent_count = 0;
2734 	char *de_buf, *limit;
2735 	struct ocfs2_dir_entry *de;
2736 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2737 
2738 	de_buf = di->id2.i_data.id_data;
2739 	limit = de_buf + i_size_read(dir);
2740 
2741 	while (de_buf < limit) {
2742 		de = (struct ocfs2_dir_entry *)de_buf;
2743 
2744 		if (de->name_len && de->inode)
2745 			dirent_count++;
2746 
2747 		de_buf += le16_to_cpu(de->rec_len);
2748 	}
2749 
2750 	/* We are careful to leave room for one extra record. */
2751 	return dirent_count < ocfs2_dx_entries_per_root(dir->i_sb);
2752 }
2753 
2754 /*
2755  * Expand rec_len of the rightmost dirent in a directory block so that it
2756  * contains the end of our valid space for dirents. We do this during
2757  * expansion from an inline directory to one with extents. The first dir block
2758  * in that case is taken from the inline data portion of the inode block.
2759  *
2760  * This will also return the largest amount of contiguous space for a dirent
2761  * in the block. That value is *not* necessarily the last dirent, even after
2762  * expansion. The directory indexing code wants this value for free space
2763  * accounting. We do this here since we're already walking the entire dir
2764  * block.
2765  *
2766  * We add the dir trailer if this filesystem wants it.
2767  */
ocfs2_expand_last_dirent(char * start,unsigned int old_size,struct inode * dir)2768 static unsigned int ocfs2_expand_last_dirent(char *start, unsigned int old_size,
2769 					     struct inode *dir)
2770 {
2771 	struct super_block *sb = dir->i_sb;
2772 	struct ocfs2_dir_entry *de;
2773 	struct ocfs2_dir_entry *prev_de;
2774 	char *de_buf, *limit;
2775 	unsigned int new_size = sb->s_blocksize;
2776 	unsigned int bytes, this_hole;
2777 	unsigned int largest_hole = 0;
2778 
2779 	if (ocfs2_new_dir_wants_trailer(dir))
2780 		new_size = ocfs2_dir_trailer_blk_off(sb);
2781 
2782 	bytes = new_size - old_size;
2783 
2784 	limit = start + old_size;
2785 	de_buf = start;
2786 	de = (struct ocfs2_dir_entry *)de_buf;
2787 	do {
2788 		this_hole = ocfs2_figure_dirent_hole(de);
2789 		if (this_hole > largest_hole)
2790 			largest_hole = this_hole;
2791 
2792 		prev_de = de;
2793 		de_buf += le16_to_cpu(de->rec_len);
2794 		de = (struct ocfs2_dir_entry *)de_buf;
2795 	} while (de_buf < limit);
2796 
2797 	le16_add_cpu(&prev_de->rec_len, bytes);
2798 
2799 	/* We need to double check this after modification of the final
2800 	 * dirent. */
2801 	this_hole = ocfs2_figure_dirent_hole(prev_de);
2802 	if (this_hole > largest_hole)
2803 		largest_hole = this_hole;
2804 
2805 	if (largest_hole >= OCFS2_DIR_MIN_REC_LEN)
2806 		return largest_hole;
2807 	return 0;
2808 }
2809 
2810 /*
2811  * We allocate enough clusters to fulfill "blocks_wanted", but set
2812  * i_size to exactly one block. Ocfs2_extend_dir() will handle the
2813  * rest automatically for us.
2814  *
2815  * *first_block_bh is a pointer to the 1st data block allocated to the
2816  *  directory.
2817  */
ocfs2_expand_inline_dir(struct inode * dir,struct buffer_head * di_bh,unsigned int blocks_wanted,struct ocfs2_dir_lookup_result * lookup,struct buffer_head ** first_block_bh)2818 static int ocfs2_expand_inline_dir(struct inode *dir, struct buffer_head *di_bh,
2819 				   unsigned int blocks_wanted,
2820 				   struct ocfs2_dir_lookup_result *lookup,
2821 				   struct buffer_head **first_block_bh)
2822 {
2823 	u32 alloc, dx_alloc, bit_off, len, num_dx_entries = 0;
2824 	struct super_block *sb = dir->i_sb;
2825 	int ret, i, num_dx_leaves = 0, dx_inline = 0,
2826 		credits = ocfs2_inline_to_extents_credits(sb);
2827 	u64 dx_insert_blkno, blkno,
2828 		bytes = blocks_wanted << sb->s_blocksize_bits;
2829 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
2830 	struct ocfs2_inode_info *oi = OCFS2_I(dir);
2831 	struct ocfs2_alloc_context *data_ac = NULL;
2832 	struct ocfs2_alloc_context *meta_ac = NULL;
2833 	struct buffer_head *dirdata_bh = NULL;
2834 	struct buffer_head *dx_root_bh = NULL;
2835 	struct buffer_head **dx_leaves = NULL;
2836 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2837 	handle_t *handle;
2838 	struct ocfs2_extent_tree et;
2839 	struct ocfs2_extent_tree dx_et;
2840 	int did_quota = 0, bytes_allocated = 0;
2841 
2842 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir), di_bh);
2843 
2844 	alloc = ocfs2_clusters_for_bytes(sb, bytes);
2845 	dx_alloc = 0;
2846 
2847 	down_write(&oi->ip_alloc_sem);
2848 
2849 	if (ocfs2_supports_indexed_dirs(osb)) {
2850 		credits += ocfs2_add_dir_index_credits(sb);
2851 
2852 		dx_inline = ocfs2_new_dx_should_be_inline(dir, di_bh);
2853 		if (!dx_inline) {
2854 			/* Add one more cluster for an index leaf */
2855 			dx_alloc++;
2856 			dx_leaves = ocfs2_dx_dir_kmalloc_leaves(sb,
2857 								&num_dx_leaves);
2858 			if (!dx_leaves) {
2859 				ret = -ENOMEM;
2860 				mlog_errno(ret);
2861 				goto out;
2862 			}
2863 		}
2864 
2865 		/* This gets us the dx_root */
2866 		ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
2867 		if (ret) {
2868 			mlog_errno(ret);
2869 			goto out;
2870 		}
2871 	}
2872 
2873 	/*
2874 	 * We should never need more than 2 clusters for the unindexed
2875 	 * tree - maximum dirent size is far less than one block. In
2876 	 * fact, the only time we'd need more than one cluster is if
2877 	 * blocksize == clustersize and the dirent won't fit in the
2878 	 * extra space that the expansion to a single block gives. As
2879 	 * of today, that only happens on 4k/4k file systems.
2880 	 */
2881 	BUG_ON(alloc > 2);
2882 
2883 	ret = ocfs2_reserve_clusters(osb, alloc + dx_alloc, &data_ac);
2884 	if (ret) {
2885 		mlog_errno(ret);
2886 		goto out;
2887 	}
2888 
2889 	/*
2890 	 * Prepare for worst case allocation scenario of two separate
2891 	 * extents in the unindexed tree.
2892 	 */
2893 	if (alloc == 2)
2894 		credits += OCFS2_SUBALLOC_ALLOC;
2895 
2896 	handle = ocfs2_start_trans(osb, credits);
2897 	if (IS_ERR(handle)) {
2898 		ret = PTR_ERR(handle);
2899 		mlog_errno(ret);
2900 		goto out;
2901 	}
2902 
2903 	ret = dquot_alloc_space_nodirty(dir,
2904 		ocfs2_clusters_to_bytes(osb->sb, alloc + dx_alloc));
2905 	if (ret)
2906 		goto out_commit;
2907 	did_quota = 1;
2908 
2909 	if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2910 		/*
2911 		 * Allocate our index cluster first, to maximize the
2912 		 * possibility that unindexed leaves grow
2913 		 * contiguously.
2914 		 */
2915 		ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac,
2916 						 dx_leaves, num_dx_leaves,
2917 						 &dx_insert_blkno);
2918 		if (ret) {
2919 			mlog_errno(ret);
2920 			goto out_commit;
2921 		}
2922 		bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2923 	}
2924 
2925 	/*
2926 	 * Try to claim as many clusters as the bitmap can give though
2927 	 * if we only get one now, that's enough to continue. The rest
2928 	 * will be claimed after the conversion to extents.
2929 	 */
2930 	if (ocfs2_dir_resv_allowed(osb))
2931 		data_ac->ac_resv = &oi->ip_la_data_resv;
2932 	ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, &len);
2933 	if (ret) {
2934 		mlog_errno(ret);
2935 		goto out_commit;
2936 	}
2937 	bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
2938 
2939 	/*
2940 	 * Operations are carefully ordered so that we set up the new
2941 	 * data block first. The conversion from inline data to
2942 	 * extents follows.
2943 	 */
2944 	blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
2945 	dirdata_bh = sb_getblk(sb, blkno);
2946 	if (!dirdata_bh) {
2947 		ret = -ENOMEM;
2948 		mlog_errno(ret);
2949 		goto out_commit;
2950 	}
2951 
2952 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), dirdata_bh);
2953 
2954 	ret = ocfs2_journal_access_db(handle, INODE_CACHE(dir), dirdata_bh,
2955 				      OCFS2_JOURNAL_ACCESS_CREATE);
2956 	if (ret) {
2957 		mlog_errno(ret);
2958 		goto out_commit;
2959 	}
2960 
2961 	memcpy(dirdata_bh->b_data, di->id2.i_data.id_data, i_size_read(dir));
2962 	memset(dirdata_bh->b_data + i_size_read(dir), 0,
2963 	       sb->s_blocksize - i_size_read(dir));
2964 	i = ocfs2_expand_last_dirent(dirdata_bh->b_data, i_size_read(dir), dir);
2965 	if (ocfs2_new_dir_wants_trailer(dir)) {
2966 		/*
2967 		 * Prepare the dir trailer up front. It will otherwise look
2968 		 * like a valid dirent. Even if inserting the index fails
2969 		 * (unlikely), then all we'll have done is given first dir
2970 		 * block a small amount of fragmentation.
2971 		 */
2972 		ocfs2_init_dir_trailer(dir, dirdata_bh, i);
2973 	}
2974 
2975 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
2976 	ocfs2_journal_dirty(handle, dirdata_bh);
2977 
2978 	if (ocfs2_supports_indexed_dirs(osb) && !dx_inline) {
2979 		/*
2980 		 * Dx dirs with an external cluster need to do this up
2981 		 * front. Inline dx root's get handled later, after
2982 		 * we've allocated our root block. We get passed back
2983 		 * a total number of items so that dr_num_entries can
2984 		 * be correctly set once the dx_root has been
2985 		 * allocated.
2986 		 */
2987 		ret = ocfs2_dx_dir_index_block(dir, handle, dx_leaves,
2988 					       num_dx_leaves, &num_dx_entries,
2989 					       dirdata_bh);
2990 		if (ret) {
2991 			mlog_errno(ret);
2992 			goto out_commit;
2993 		}
2994 	}
2995 
2996 	/*
2997 	 * Set extent, i_size, etc on the directory. After this, the
2998 	 * inode should contain the same exact dirents as before and
2999 	 * be fully accessible from system calls.
3000 	 *
3001 	 * We let the later dirent insert modify c/mtime - to the user
3002 	 * the data hasn't changed.
3003 	 */
3004 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
3005 				      OCFS2_JOURNAL_ACCESS_CREATE);
3006 	if (ret) {
3007 		mlog_errno(ret);
3008 		goto out_commit;
3009 	}
3010 
3011 	spin_lock(&oi->ip_lock);
3012 	oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
3013 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
3014 	spin_unlock(&oi->ip_lock);
3015 
3016 	ocfs2_dinode_new_extent_list(dir, di);
3017 
3018 	i_size_write(dir, sb->s_blocksize);
3019 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
3020 
3021 	di->i_size = cpu_to_le64(sb->s_blocksize);
3022 	di->i_ctime = di->i_mtime = cpu_to_le64(inode_get_ctime_sec(dir));
3023 	di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode_get_ctime_nsec(dir));
3024 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
3025 
3026 	/*
3027 	 * This should never fail as our extent list is empty and all
3028 	 * related blocks have been journaled already.
3029 	 */
3030 	ret = ocfs2_insert_extent(handle, &et, 0, blkno, len,
3031 				  0, NULL);
3032 	if (ret) {
3033 		mlog_errno(ret);
3034 		goto out_commit;
3035 	}
3036 
3037 	/*
3038 	 * Set i_blocks after the extent insert for the most up to
3039 	 * date ip_clusters value.
3040 	 */
3041 	dir->i_blocks = ocfs2_inode_sector_count(dir);
3042 
3043 	ocfs2_journal_dirty(handle, di_bh);
3044 
3045 	if (ocfs2_supports_indexed_dirs(osb)) {
3046 		ret = ocfs2_dx_dir_attach_index(osb, handle, dir, di_bh,
3047 						dirdata_bh, meta_ac, dx_inline,
3048 						num_dx_entries, &dx_root_bh);
3049 		if (ret) {
3050 			mlog_errno(ret);
3051 			goto out_commit;
3052 		}
3053 
3054 		if (dx_inline) {
3055 			ocfs2_dx_dir_index_root_block(dir, dx_root_bh,
3056 						      dirdata_bh);
3057 		} else {
3058 			ocfs2_init_dx_root_extent_tree(&dx_et,
3059 						       INODE_CACHE(dir),
3060 						       dx_root_bh);
3061 			ret = ocfs2_insert_extent(handle, &dx_et, 0,
3062 						  dx_insert_blkno, 1, 0, NULL);
3063 			if (ret)
3064 				mlog_errno(ret);
3065 		}
3066 	}
3067 
3068 	/*
3069 	 * We asked for two clusters, but only got one in the 1st
3070 	 * pass. Claim the 2nd cluster as a separate extent.
3071 	 */
3072 	if (alloc > len) {
3073 		ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
3074 					   &len);
3075 		if (ret) {
3076 			mlog_errno(ret);
3077 			goto out_commit;
3078 		}
3079 		blkno = ocfs2_clusters_to_blocks(dir->i_sb, bit_off);
3080 
3081 		ret = ocfs2_insert_extent(handle, &et, 1,
3082 					  blkno, len, 0, NULL);
3083 		if (ret) {
3084 			mlog_errno(ret);
3085 			goto out_commit;
3086 		}
3087 		bytes_allocated += ocfs2_clusters_to_bytes(dir->i_sb, 1);
3088 	}
3089 
3090 	*first_block_bh = dirdata_bh;
3091 	dirdata_bh = NULL;
3092 	if (ocfs2_supports_indexed_dirs(osb)) {
3093 		unsigned int off;
3094 
3095 		if (!dx_inline) {
3096 			/*
3097 			 * We need to return the correct block within the
3098 			 * cluster which should hold our entry.
3099 			 */
3100 			off = ocfs2_dx_dir_hash_idx(osb,
3101 						    &lookup->dl_hinfo);
3102 			get_bh(dx_leaves[off]);
3103 			lookup->dl_dx_leaf_bh = dx_leaves[off];
3104 		}
3105 		lookup->dl_dx_root_bh = dx_root_bh;
3106 		dx_root_bh = NULL;
3107 	}
3108 
3109 out_commit:
3110 	if (ret < 0 && did_quota)
3111 		dquot_free_space_nodirty(dir, bytes_allocated);
3112 
3113 	ocfs2_commit_trans(osb, handle);
3114 
3115 out:
3116 	up_write(&oi->ip_alloc_sem);
3117 	if (data_ac)
3118 		ocfs2_free_alloc_context(data_ac);
3119 	if (meta_ac)
3120 		ocfs2_free_alloc_context(meta_ac);
3121 
3122 	if (dx_leaves) {
3123 		for (i = 0; i < num_dx_leaves; i++)
3124 			brelse(dx_leaves[i]);
3125 		kfree(dx_leaves);
3126 	}
3127 
3128 	brelse(dirdata_bh);
3129 	brelse(dx_root_bh);
3130 
3131 	return ret;
3132 }
3133 
3134 /* returns a bh of the 1st new block in the allocation. */
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)3135 static int ocfs2_do_extend_dir(struct super_block *sb,
3136 			       handle_t *handle,
3137 			       struct inode *dir,
3138 			       struct buffer_head *parent_fe_bh,
3139 			       struct ocfs2_alloc_context *data_ac,
3140 			       struct ocfs2_alloc_context *meta_ac,
3141 			       struct buffer_head **new_bh)
3142 {
3143 	int status;
3144 	int extend, did_quota = 0;
3145 	u64 p_blkno, v_blkno;
3146 
3147 	spin_lock(&OCFS2_I(dir)->ip_lock);
3148 	extend = (i_size_read(dir) == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters));
3149 	spin_unlock(&OCFS2_I(dir)->ip_lock);
3150 
3151 	if (extend) {
3152 		u32 offset = OCFS2_I(dir)->ip_clusters;
3153 
3154 		status = dquot_alloc_space_nodirty(dir,
3155 					ocfs2_clusters_to_bytes(sb, 1));
3156 		if (status)
3157 			goto bail;
3158 		did_quota = 1;
3159 
3160 		status = ocfs2_add_inode_data(OCFS2_SB(sb), dir, &offset,
3161 					      1, 0, parent_fe_bh, handle,
3162 					      data_ac, meta_ac, NULL);
3163 		BUG_ON(status == -EAGAIN);
3164 		if (status < 0) {
3165 			mlog_errno(status);
3166 			goto bail;
3167 		}
3168 	}
3169 
3170 	v_blkno = ocfs2_blocks_for_bytes(sb, i_size_read(dir));
3171 	status = ocfs2_extent_map_get_blocks(dir, v_blkno, &p_blkno, NULL, NULL);
3172 	if (status < 0) {
3173 		mlog_errno(status);
3174 		goto bail;
3175 	}
3176 
3177 	*new_bh = sb_getblk(sb, p_blkno);
3178 	if (!*new_bh) {
3179 		status = -ENOMEM;
3180 		mlog_errno(status);
3181 		goto bail;
3182 	}
3183 	status = 0;
3184 bail:
3185 	if (did_quota && status < 0)
3186 		dquot_free_space_nodirty(dir, ocfs2_clusters_to_bytes(sb, 1));
3187 	return status;
3188 }
3189 
3190 /*
3191  * Assumes you already have a cluster lock on the directory.
3192  *
3193  * 'blocks_wanted' is only used if we have an inline directory which
3194  * is to be turned into an extent based one. The size of the dirent to
3195  * insert might be larger than the space gained by growing to just one
3196  * block, so we may have to grow the inode by two blocks in that case.
3197  *
3198  * If the directory is already indexed, dx_root_bh must be provided.
3199  */
ocfs2_extend_dir(struct ocfs2_super * osb,struct inode * dir,struct buffer_head * parent_fe_bh,unsigned int blocks_wanted,struct ocfs2_dir_lookup_result * lookup,struct buffer_head ** new_de_bh)3200 static int ocfs2_extend_dir(struct ocfs2_super *osb,
3201 			    struct inode *dir,
3202 			    struct buffer_head *parent_fe_bh,
3203 			    unsigned int blocks_wanted,
3204 			    struct ocfs2_dir_lookup_result *lookup,
3205 			    struct buffer_head **new_de_bh)
3206 {
3207 	int status = 0;
3208 	int credits, num_free_extents, drop_alloc_sem = 0;
3209 	loff_t dir_i_size;
3210 	struct ocfs2_dinode *fe = (struct ocfs2_dinode *) parent_fe_bh->b_data;
3211 	struct ocfs2_extent_list *el = &fe->id2.i_list;
3212 	struct ocfs2_alloc_context *data_ac = NULL;
3213 	struct ocfs2_alloc_context *meta_ac = NULL;
3214 	handle_t *handle = NULL;
3215 	struct buffer_head *new_bh = NULL;
3216 	struct ocfs2_dir_entry * de;
3217 	struct super_block *sb = osb->sb;
3218 	struct ocfs2_extent_tree et;
3219 	struct buffer_head *dx_root_bh = lookup->dl_dx_root_bh;
3220 
3221 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
3222 		/*
3223 		 * This would be a code error as an inline directory should
3224 		 * never have an index root.
3225 		 */
3226 		BUG_ON(dx_root_bh);
3227 
3228 		status = ocfs2_expand_inline_dir(dir, parent_fe_bh,
3229 						 blocks_wanted, lookup,
3230 						 &new_bh);
3231 		if (status) {
3232 			mlog_errno(status);
3233 			goto bail;
3234 		}
3235 
3236 		/* Expansion from inline to an indexed directory will
3237 		 * have given us this. */
3238 		dx_root_bh = lookup->dl_dx_root_bh;
3239 
3240 		if (blocks_wanted == 1) {
3241 			/*
3242 			 * If the new dirent will fit inside the space
3243 			 * created by pushing out to one block, then
3244 			 * we can complete the operation
3245 			 * here. Otherwise we have to expand i_size
3246 			 * and format the 2nd block below.
3247 			 */
3248 			BUG_ON(new_bh == NULL);
3249 			goto bail_bh;
3250 		}
3251 
3252 		/*
3253 		 * Get rid of 'new_bh' - we want to format the 2nd
3254 		 * data block and return that instead.
3255 		 */
3256 		brelse(new_bh);
3257 		new_bh = NULL;
3258 
3259 		down_write(&OCFS2_I(dir)->ip_alloc_sem);
3260 		drop_alloc_sem = 1;
3261 		dir_i_size = i_size_read(dir);
3262 		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3263 		goto do_extend;
3264 	}
3265 
3266 	down_write(&OCFS2_I(dir)->ip_alloc_sem);
3267 	drop_alloc_sem = 1;
3268 	dir_i_size = i_size_read(dir);
3269 	trace_ocfs2_extend_dir((unsigned long long)OCFS2_I(dir)->ip_blkno,
3270 			       dir_i_size);
3271 
3272 	/* dir->i_size is always block aligned. */
3273 	spin_lock(&OCFS2_I(dir)->ip_lock);
3274 	if (dir_i_size == ocfs2_clusters_to_bytes(sb, OCFS2_I(dir)->ip_clusters)) {
3275 		spin_unlock(&OCFS2_I(dir)->ip_lock);
3276 		ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(dir),
3277 					      parent_fe_bh);
3278 		num_free_extents = ocfs2_num_free_extents(&et);
3279 		if (num_free_extents < 0) {
3280 			status = num_free_extents;
3281 			mlog_errno(status);
3282 			goto bail;
3283 		}
3284 
3285 		if (!num_free_extents) {
3286 			status = ocfs2_reserve_new_metadata(osb, el, &meta_ac);
3287 			if (status < 0) {
3288 				if (status != -ENOSPC)
3289 					mlog_errno(status);
3290 				goto bail;
3291 			}
3292 		}
3293 
3294 		status = ocfs2_reserve_clusters(osb, 1, &data_ac);
3295 		if (status < 0) {
3296 			if (status != -ENOSPC)
3297 				mlog_errno(status);
3298 			goto bail;
3299 		}
3300 
3301 		if (ocfs2_dir_resv_allowed(osb))
3302 			data_ac->ac_resv = &OCFS2_I(dir)->ip_la_data_resv;
3303 
3304 		credits = ocfs2_calc_extend_credits(sb, el);
3305 	} else {
3306 		spin_unlock(&OCFS2_I(dir)->ip_lock);
3307 		credits = OCFS2_SIMPLE_DIR_EXTEND_CREDITS;
3308 	}
3309 
3310 do_extend:
3311 	if (ocfs2_dir_indexed(dir))
3312 		credits++; /* For attaching the new dirent block to the
3313 			    * dx_root */
3314 
3315 	handle = ocfs2_start_trans(osb, credits);
3316 	if (IS_ERR(handle)) {
3317 		status = PTR_ERR(handle);
3318 		handle = NULL;
3319 		mlog_errno(status);
3320 		goto bail;
3321 	}
3322 
3323 	status = ocfs2_do_extend_dir(osb->sb, handle, dir, parent_fe_bh,
3324 				     data_ac, meta_ac, &new_bh);
3325 	if (status < 0) {
3326 		mlog_errno(status);
3327 		goto bail;
3328 	}
3329 
3330 	ocfs2_set_new_buffer_uptodate(INODE_CACHE(dir), new_bh);
3331 
3332 	status = ocfs2_journal_access_db(handle, INODE_CACHE(dir), new_bh,
3333 					 OCFS2_JOURNAL_ACCESS_CREATE);
3334 	if (status < 0) {
3335 		mlog_errno(status);
3336 		goto bail;
3337 	}
3338 	memset(new_bh->b_data, 0, sb->s_blocksize);
3339 
3340 	de = (struct ocfs2_dir_entry *) new_bh->b_data;
3341 	de->inode = 0;
3342 	if (ocfs2_supports_dir_trailer(dir)) {
3343 		de->rec_len = cpu_to_le16(ocfs2_dir_trailer_blk_off(sb));
3344 
3345 		ocfs2_init_dir_trailer(dir, new_bh, le16_to_cpu(de->rec_len));
3346 
3347 		if (ocfs2_dir_indexed(dir)) {
3348 			status = ocfs2_dx_dir_link_trailer(dir, handle,
3349 							   dx_root_bh, new_bh);
3350 			if (status) {
3351 				mlog_errno(status);
3352 				goto bail;
3353 			}
3354 		}
3355 	} else {
3356 		de->rec_len = cpu_to_le16(sb->s_blocksize);
3357 	}
3358 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
3359 	ocfs2_journal_dirty(handle, new_bh);
3360 
3361 	dir_i_size += dir->i_sb->s_blocksize;
3362 	i_size_write(dir, dir_i_size);
3363 	dir->i_blocks = ocfs2_inode_sector_count(dir);
3364 	status = ocfs2_mark_inode_dirty(handle, dir, parent_fe_bh);
3365 	if (status < 0) {
3366 		mlog_errno(status);
3367 		goto bail;
3368 	}
3369 
3370 bail_bh:
3371 	*new_de_bh = new_bh;
3372 	get_bh(*new_de_bh);
3373 bail:
3374 	if (handle)
3375 		ocfs2_commit_trans(osb, handle);
3376 	if (drop_alloc_sem)
3377 		up_write(&OCFS2_I(dir)->ip_alloc_sem);
3378 
3379 	if (data_ac)
3380 		ocfs2_free_alloc_context(data_ac);
3381 	if (meta_ac)
3382 		ocfs2_free_alloc_context(meta_ac);
3383 
3384 	brelse(new_bh);
3385 
3386 	return status;
3387 }
3388 
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)3389 static int ocfs2_find_dir_space_id(struct inode *dir, struct buffer_head *di_bh,
3390 				   const char *name, int namelen,
3391 				   struct buffer_head **ret_de_bh,
3392 				   unsigned int *blocks_wanted)
3393 {
3394 	int ret;
3395 	struct super_block *sb = dir->i_sb;
3396 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3397 	struct ocfs2_dir_entry *de, *last_de = NULL;
3398 	char *first_de, *de_buf, *limit;
3399 	unsigned long offset = 0;
3400 	unsigned int rec_len, new_rec_len, free_space;
3401 
3402 	/*
3403 	 * This calculates how many free bytes we'd have in block zero, should
3404 	 * this function force expansion to an extent tree.
3405 	 */
3406 	if (ocfs2_new_dir_wants_trailer(dir))
3407 		free_space = ocfs2_dir_trailer_blk_off(sb) - i_size_read(dir);
3408 	else
3409 		free_space = dir->i_sb->s_blocksize - i_size_read(dir);
3410 
3411 	first_de = di->id2.i_data.id_data;
3412 	de_buf = first_de;
3413 	limit = de_buf + i_size_read(dir);
3414 	rec_len = OCFS2_DIR_REC_LEN(namelen);
3415 
3416 	while (de_buf < limit) {
3417 		de = (struct ocfs2_dir_entry *)de_buf;
3418 
3419 		if (!ocfs2_check_dir_entry(dir, de, di_bh, first_de,
3420 					   i_size_read(dir), offset)) {
3421 			ret = -ENOENT;
3422 			goto out;
3423 		}
3424 		if (ocfs2_match(namelen, name, de)) {
3425 			ret = -EEXIST;
3426 			goto out;
3427 		}
3428 		/*
3429 		 * No need to check for a trailing dirent record here as
3430 		 * they're not used for inline dirs.
3431 		 */
3432 
3433 		if (ocfs2_dirent_would_fit(de, rec_len)) {
3434 			/* Ok, we found a spot. Return this bh and let
3435 			 * the caller actually fill it in. */
3436 			*ret_de_bh = di_bh;
3437 			get_bh(*ret_de_bh);
3438 			ret = 0;
3439 			goto out;
3440 		}
3441 
3442 		last_de = de;
3443 		de_buf += le16_to_cpu(de->rec_len);
3444 		offset += le16_to_cpu(de->rec_len);
3445 	}
3446 
3447 	if (!last_de) {
3448 		ret = ocfs2_error(sb, "Directory entry (#%llu: size=%lld) "
3449 				  "is unexpectedly short",
3450 				  (unsigned long long)OCFS2_I(dir)->ip_blkno,
3451 				  i_size_read(dir));
3452 		goto out;
3453 	}
3454 
3455 	/*
3456 	 * We're going to require expansion of the directory - figure
3457 	 * out how many blocks we'll need so that a place for the
3458 	 * dirent can be found.
3459 	 */
3460 	*blocks_wanted = 1;
3461 	new_rec_len = le16_to_cpu(last_de->rec_len) + free_space;
3462 	if (new_rec_len < (rec_len + OCFS2_DIR_REC_LEN(last_de->name_len)))
3463 		*blocks_wanted = 2;
3464 
3465 	ret = -ENOSPC;
3466 out:
3467 	return ret;
3468 }
3469 
ocfs2_find_dir_space_el(struct inode * dir,const char * name,int namelen,struct buffer_head ** ret_de_bh)3470 static int ocfs2_find_dir_space_el(struct inode *dir, const char *name,
3471 				   int namelen, struct buffer_head **ret_de_bh)
3472 {
3473 	unsigned long offset;
3474 	struct buffer_head *bh = NULL;
3475 	unsigned short rec_len;
3476 	struct ocfs2_dir_entry *de;
3477 	struct super_block *sb = dir->i_sb;
3478 	int status;
3479 	int blocksize = dir->i_sb->s_blocksize;
3480 
3481 	status = ocfs2_read_dir_block(dir, 0, &bh, 0);
3482 	if (status)
3483 		goto bail;
3484 
3485 	rec_len = OCFS2_DIR_REC_LEN(namelen);
3486 	offset = 0;
3487 	de = (struct ocfs2_dir_entry *) bh->b_data;
3488 	while (1) {
3489 		if ((char *)de >= sb->s_blocksize + bh->b_data) {
3490 			brelse(bh);
3491 			bh = NULL;
3492 
3493 			if (i_size_read(dir) <= offset) {
3494 				/*
3495 				 * Caller will have to expand this
3496 				 * directory.
3497 				 */
3498 				status = -ENOSPC;
3499 				goto bail;
3500 			}
3501 			status = ocfs2_read_dir_block(dir,
3502 					     offset >> sb->s_blocksize_bits,
3503 					     &bh, 0);
3504 			if (status)
3505 				goto bail;
3506 
3507 			/* move to next block */
3508 			de = (struct ocfs2_dir_entry *) bh->b_data;
3509 		}
3510 		if (!ocfs2_check_dir_entry(dir, de, bh, bh->b_data, blocksize,
3511 					   offset)) {
3512 			status = -ENOENT;
3513 			goto bail;
3514 		}
3515 		if (ocfs2_match(namelen, name, de)) {
3516 			status = -EEXIST;
3517 			goto bail;
3518 		}
3519 
3520 		if (ocfs2_skip_dir_trailer(dir, de, offset % blocksize,
3521 					   blocksize))
3522 			goto next;
3523 
3524 		if (ocfs2_dirent_would_fit(de, rec_len)) {
3525 			/* Ok, we found a spot. Return this bh and let
3526 			 * the caller actually fill it in. */
3527 			*ret_de_bh = bh;
3528 			get_bh(*ret_de_bh);
3529 			status = 0;
3530 			goto bail;
3531 		}
3532 next:
3533 		offset += le16_to_cpu(de->rec_len);
3534 		de = (struct ocfs2_dir_entry *)((char *) de + le16_to_cpu(de->rec_len));
3535 	}
3536 
3537 bail:
3538 	brelse(bh);
3539 	if (status)
3540 		mlog_errno(status);
3541 
3542 	return status;
3543 }
3544 
dx_leaf_sort_cmp(const void * a,const void * b)3545 static int dx_leaf_sort_cmp(const void *a, const void *b)
3546 {
3547 	const struct ocfs2_dx_entry *entry1 = a;
3548 	const struct ocfs2_dx_entry *entry2 = b;
3549 	u32 major_hash1 = le32_to_cpu(entry1->dx_major_hash);
3550 	u32 major_hash2 = le32_to_cpu(entry2->dx_major_hash);
3551 	u32 minor_hash1 = le32_to_cpu(entry1->dx_minor_hash);
3552 	u32 minor_hash2 = le32_to_cpu(entry2->dx_minor_hash);
3553 
3554 	if (major_hash1 > major_hash2)
3555 		return 1;
3556 	if (major_hash1 < major_hash2)
3557 		return -1;
3558 
3559 	/*
3560 	 * It is not strictly necessary to sort by minor
3561 	 */
3562 	if (minor_hash1 > minor_hash2)
3563 		return 1;
3564 	if (minor_hash1 < minor_hash2)
3565 		return -1;
3566 	return 0;
3567 }
3568 
ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf * dx_leaf)3569 static int ocfs2_dx_leaf_same_major(struct ocfs2_dx_leaf *dx_leaf)
3570 {
3571 	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3572 	int i, num = le16_to_cpu(dl_list->de_num_used);
3573 
3574 	for (i = 0; i < (num - 1); i++) {
3575 		if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) !=
3576 		    le32_to_cpu(dl_list->de_entries[i + 1].dx_major_hash))
3577 			return 0;
3578 	}
3579 
3580 	return 1;
3581 }
3582 
3583 /*
3584  * Find the optimal value to split this leaf on. This expects the leaf
3585  * entries to be in sorted order.
3586  *
3587  * leaf_cpos is the cpos of the leaf we're splitting. insert_hash is
3588  * the hash we want to insert.
3589  *
3590  * This function is only concerned with the major hash - that which
3591  * determines which cluster an item belongs to.
3592  */
ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf * dx_leaf,u32 leaf_cpos,u32 insert_hash,u32 * split_hash)3593 static int ocfs2_dx_dir_find_leaf_split(struct ocfs2_dx_leaf *dx_leaf,
3594 					u32 leaf_cpos, u32 insert_hash,
3595 					u32 *split_hash)
3596 {
3597 	struct ocfs2_dx_entry_list *dl_list = &dx_leaf->dl_list;
3598 	int i, num_used = le16_to_cpu(dl_list->de_num_used);
3599 	int allsame;
3600 
3601 	/*
3602 	 * There's a couple rare, but nasty corner cases we have to
3603 	 * check for here. All of them involve a leaf where all value
3604 	 * have the same hash, which is what we look for first.
3605 	 *
3606 	 * Most of the time, all of the above is false, and we simply
3607 	 * pick the median value for a split.
3608 	 */
3609 	allsame = ocfs2_dx_leaf_same_major(dx_leaf);
3610 	if (allsame) {
3611 		u32 val = le32_to_cpu(dl_list->de_entries[0].dx_major_hash);
3612 
3613 		if (val == insert_hash) {
3614 			/*
3615 			 * No matter where we would choose to split,
3616 			 * the new entry would want to occupy the same
3617 			 * block as these. Since there's no space left
3618 			 * in their existing block, we know there
3619 			 * won't be space after the split.
3620 			 */
3621 			return -ENOSPC;
3622 		}
3623 
3624 		if (val == leaf_cpos) {
3625 			/*
3626 			 * Because val is the same as leaf_cpos (which
3627 			 * is the smallest value this leaf can have),
3628 			 * yet is not equal to insert_hash, then we
3629 			 * know that insert_hash *must* be larger than
3630 			 * val (and leaf_cpos). At least cpos+1 in value.
3631 			 *
3632 			 * We also know then, that there cannot be an
3633 			 * adjacent extent (otherwise we'd be looking
3634 			 * at it). Choosing this value gives us a
3635 			 * chance to get some contiguousness.
3636 			 */
3637 			*split_hash = leaf_cpos + 1;
3638 			return 0;
3639 		}
3640 
3641 		if (val > insert_hash) {
3642 			/*
3643 			 * val can not be the same as insert hash, and
3644 			 * also must be larger than leaf_cpos. Also,
3645 			 * we know that there can't be a leaf between
3646 			 * cpos and val, otherwise the entries with
3647 			 * hash 'val' would be there.
3648 			 */
3649 			*split_hash = val;
3650 			return 0;
3651 		}
3652 
3653 		*split_hash = insert_hash;
3654 		return 0;
3655 	}
3656 
3657 	/*
3658 	 * Since the records are sorted and the checks above
3659 	 * guaranteed that not all records in this block are the same,
3660 	 * we simple travel forward, from the median, and pick the 1st
3661 	 * record whose value is larger than leaf_cpos.
3662 	 */
3663 	for (i = (num_used / 2); i < num_used; i++)
3664 		if (le32_to_cpu(dl_list->de_entries[i].dx_major_hash) >
3665 		    leaf_cpos)
3666 			break;
3667 
3668 	BUG_ON(i == num_used); /* Should be impossible */
3669 	*split_hash = le32_to_cpu(dl_list->de_entries[i].dx_major_hash);
3670 	return 0;
3671 }
3672 
3673 /*
3674  * Transfer all entries in orig_dx_leaves whose major hash is equal to or
3675  * larger than split_hash into new_dx_leaves. We use a temporary
3676  * buffer (tmp_dx_leaf) to make the changes to the original leaf blocks.
3677  *
3678  * Since the block offset inside a leaf (cluster) is a constant mask
3679  * of minor_hash, we can optimize - an item at block offset X within
3680  * the original cluster, will be at offset X within the new cluster.
3681  */
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)3682 static void ocfs2_dx_dir_transfer_leaf(struct inode *dir, u32 split_hash,
3683 				       handle_t *handle,
3684 				       struct ocfs2_dx_leaf *tmp_dx_leaf,
3685 				       struct buffer_head **orig_dx_leaves,
3686 				       struct buffer_head **new_dx_leaves,
3687 				       int num_dx_leaves)
3688 {
3689 	int i, j, num_used;
3690 	u32 major_hash;
3691 	struct ocfs2_dx_leaf *orig_dx_leaf, *new_dx_leaf;
3692 	struct ocfs2_dx_entry_list *orig_list, *tmp_list;
3693 	struct ocfs2_dx_entry *dx_entry;
3694 
3695 	tmp_list = &tmp_dx_leaf->dl_list;
3696 
3697 	for (i = 0; i < num_dx_leaves; i++) {
3698 		orig_dx_leaf = (struct ocfs2_dx_leaf *) orig_dx_leaves[i]->b_data;
3699 		orig_list = &orig_dx_leaf->dl_list;
3700 		new_dx_leaf = (struct ocfs2_dx_leaf *) new_dx_leaves[i]->b_data;
3701 
3702 		num_used = le16_to_cpu(orig_list->de_num_used);
3703 
3704 		memcpy(tmp_dx_leaf, orig_dx_leaf, dir->i_sb->s_blocksize);
3705 		tmp_list->de_num_used = cpu_to_le16(0);
3706 		memset(&tmp_list->de_entries, 0, sizeof(*dx_entry)*num_used);
3707 
3708 		for (j = 0; j < num_used; j++) {
3709 			dx_entry = &orig_list->de_entries[j];
3710 			major_hash = le32_to_cpu(dx_entry->dx_major_hash);
3711 			if (major_hash >= split_hash)
3712 				ocfs2_dx_dir_leaf_insert_tail(new_dx_leaf,
3713 							      dx_entry);
3714 			else
3715 				ocfs2_dx_dir_leaf_insert_tail(tmp_dx_leaf,
3716 							      dx_entry);
3717 		}
3718 		memcpy(orig_dx_leaf, tmp_dx_leaf, dir->i_sb->s_blocksize);
3719 
3720 		ocfs2_journal_dirty(handle, orig_dx_leaves[i]);
3721 		ocfs2_journal_dirty(handle, new_dx_leaves[i]);
3722 	}
3723 }
3724 
ocfs2_dx_dir_rebalance_credits(struct ocfs2_super * osb,struct ocfs2_dx_root_block * dx_root)3725 static int ocfs2_dx_dir_rebalance_credits(struct ocfs2_super *osb,
3726 					  struct ocfs2_dx_root_block *dx_root)
3727 {
3728 	int credits = ocfs2_clusters_to_blocks(osb->sb, 3);
3729 
3730 	credits += ocfs2_calc_extend_credits(osb->sb, &dx_root->dr_list);
3731 	credits += ocfs2_quota_trans_credits(osb->sb);
3732 	return credits;
3733 }
3734 
3735 /*
3736  * Find the median value in dx_leaf_bh and allocate a new leaf to move
3737  * half our entries into.
3738  */
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)3739 static int ocfs2_dx_dir_rebalance(struct ocfs2_super *osb, struct inode *dir,
3740 				  struct buffer_head *dx_root_bh,
3741 				  struct buffer_head *dx_leaf_bh,
3742 				  struct ocfs2_dx_hinfo *hinfo, u32 leaf_cpos,
3743 				  u64 leaf_blkno)
3744 {
3745 	struct ocfs2_dx_leaf *dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3746 	int credits, ret, i, num_used, did_quota = 0;
3747 	u32 cpos, split_hash, insert_hash = hinfo->major_hash;
3748 	u64 orig_leaves_start;
3749 	int num_dx_leaves;
3750 	struct buffer_head **orig_dx_leaves = NULL;
3751 	struct buffer_head **new_dx_leaves = NULL;
3752 	struct ocfs2_alloc_context *data_ac = NULL, *meta_ac = NULL;
3753 	struct ocfs2_extent_tree et;
3754 	handle_t *handle = NULL;
3755 	struct ocfs2_dx_root_block *dx_root;
3756 	struct ocfs2_dx_leaf *tmp_dx_leaf = NULL;
3757 
3758 	trace_ocfs2_dx_dir_rebalance((unsigned long long)OCFS2_I(dir)->ip_blkno,
3759 				     (unsigned long long)leaf_blkno,
3760 				     insert_hash);
3761 
3762 	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
3763 
3764 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3765 	/*
3766 	 * XXX: This is a rather large limit. We should use a more
3767 	 * realistic value.
3768 	 */
3769 	if (le32_to_cpu(dx_root->dr_clusters) == UINT_MAX)
3770 		return -ENOSPC;
3771 
3772 	num_used = le16_to_cpu(dx_leaf->dl_list.de_num_used);
3773 	if (num_used < le16_to_cpu(dx_leaf->dl_list.de_count)) {
3774 		mlog(ML_ERROR, "DX Dir: %llu, Asked to rebalance empty leaf: "
3775 		     "%llu, %d\n", (unsigned long long)OCFS2_I(dir)->ip_blkno,
3776 		     (unsigned long long)leaf_blkno, num_used);
3777 		ret = -EIO;
3778 		goto out;
3779 	}
3780 
3781 	orig_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
3782 	if (!orig_dx_leaves) {
3783 		ret = -ENOMEM;
3784 		mlog_errno(ret);
3785 		goto out;
3786 	}
3787 
3788 	new_dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, NULL);
3789 	if (!new_dx_leaves) {
3790 		ret = -ENOMEM;
3791 		mlog_errno(ret);
3792 		goto out;
3793 	}
3794 
3795 	ret = ocfs2_lock_allocators(dir, &et, 1, 0, &data_ac, &meta_ac);
3796 	if (ret) {
3797 		if (ret != -ENOSPC)
3798 			mlog_errno(ret);
3799 		goto out;
3800 	}
3801 
3802 	credits = ocfs2_dx_dir_rebalance_credits(osb, dx_root);
3803 	handle = ocfs2_start_trans(osb, credits);
3804 	if (IS_ERR(handle)) {
3805 		ret = PTR_ERR(handle);
3806 		handle = NULL;
3807 		mlog_errno(ret);
3808 		goto out;
3809 	}
3810 
3811 	ret = dquot_alloc_space_nodirty(dir,
3812 				       ocfs2_clusters_to_bytes(dir->i_sb, 1));
3813 	if (ret)
3814 		goto out_commit;
3815 	did_quota = 1;
3816 
3817 	ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir), dx_leaf_bh,
3818 				      OCFS2_JOURNAL_ACCESS_WRITE);
3819 	if (ret) {
3820 		mlog_errno(ret);
3821 		goto out_commit;
3822 	}
3823 
3824 	/*
3825 	 * This block is changing anyway, so we can sort it in place.
3826 	 */
3827 	sort(dx_leaf->dl_list.de_entries, num_used,
3828 	     sizeof(struct ocfs2_dx_entry), dx_leaf_sort_cmp,
3829 	     NULL);
3830 
3831 	ocfs2_journal_dirty(handle, dx_leaf_bh);
3832 
3833 	ret = ocfs2_dx_dir_find_leaf_split(dx_leaf, leaf_cpos, insert_hash,
3834 					   &split_hash);
3835 	if (ret) {
3836 		mlog_errno(ret);
3837 		goto  out_commit;
3838 	}
3839 
3840 	trace_ocfs2_dx_dir_rebalance_split(leaf_cpos, split_hash, insert_hash);
3841 
3842 	/*
3843 	 * We have to carefully order operations here. There are items
3844 	 * which want to be in the new cluster before insert, but in
3845 	 * order to put those items in the new cluster, we alter the
3846 	 * old cluster. A failure to insert gets nasty.
3847 	 *
3848 	 * So, start by reserving writes to the old
3849 	 * cluster. ocfs2_dx_dir_new_cluster will reserve writes on
3850 	 * the new cluster for us, before inserting it. The insert
3851 	 * won't happen if there's an error before that. Once the
3852 	 * insert is done then, we can transfer from one leaf into the
3853 	 * other without fear of hitting any error.
3854 	 */
3855 
3856 	/*
3857 	 * The leaf transfer wants some scratch space so that we don't
3858 	 * wind up doing a bunch of expensive memmove().
3859 	 */
3860 	tmp_dx_leaf = kmalloc(osb->sb->s_blocksize, GFP_NOFS);
3861 	if (!tmp_dx_leaf) {
3862 		ret = -ENOMEM;
3863 		mlog_errno(ret);
3864 		goto out_commit;
3865 	}
3866 
3867 	orig_leaves_start = ocfs2_block_to_cluster_start(dir->i_sb, leaf_blkno);
3868 	ret = ocfs2_read_dx_leaves(dir, orig_leaves_start, num_dx_leaves,
3869 				   orig_dx_leaves);
3870 	if (ret) {
3871 		mlog_errno(ret);
3872 		goto out_commit;
3873 	}
3874 
3875 	cpos = split_hash;
3876 	ret = ocfs2_dx_dir_new_cluster(dir, &et, cpos, handle,
3877 				       data_ac, meta_ac, new_dx_leaves,
3878 				       num_dx_leaves);
3879 	if (ret) {
3880 		mlog_errno(ret);
3881 		goto out_commit;
3882 	}
3883 
3884 	for (i = 0; i < num_dx_leaves; i++) {
3885 		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3886 					      orig_dx_leaves[i],
3887 					      OCFS2_JOURNAL_ACCESS_WRITE);
3888 		if (ret) {
3889 			mlog_errno(ret);
3890 			goto out_commit;
3891 		}
3892 
3893 		ret = ocfs2_journal_access_dl(handle, INODE_CACHE(dir),
3894 					      new_dx_leaves[i],
3895 					      OCFS2_JOURNAL_ACCESS_WRITE);
3896 		if (ret) {
3897 			mlog_errno(ret);
3898 			goto out_commit;
3899 		}
3900 	}
3901 
3902 	ocfs2_dx_dir_transfer_leaf(dir, split_hash, handle, tmp_dx_leaf,
3903 				   orig_dx_leaves, new_dx_leaves, num_dx_leaves);
3904 
3905 out_commit:
3906 	if (ret < 0 && did_quota)
3907 		dquot_free_space_nodirty(dir,
3908 				ocfs2_clusters_to_bytes(dir->i_sb, 1));
3909 
3910 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
3911 	ocfs2_commit_trans(osb, handle);
3912 
3913 out:
3914 	if (orig_dx_leaves || new_dx_leaves) {
3915 		for (i = 0; i < num_dx_leaves; i++) {
3916 			if (orig_dx_leaves)
3917 				brelse(orig_dx_leaves[i]);
3918 			if (new_dx_leaves)
3919 				brelse(new_dx_leaves[i]);
3920 		}
3921 		kfree(orig_dx_leaves);
3922 		kfree(new_dx_leaves);
3923 	}
3924 
3925 	if (meta_ac)
3926 		ocfs2_free_alloc_context(meta_ac);
3927 	if (data_ac)
3928 		ocfs2_free_alloc_context(data_ac);
3929 
3930 	kfree(tmp_dx_leaf);
3931 	return ret;
3932 }
3933 
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)3934 static int ocfs2_find_dir_space_dx(struct ocfs2_super *osb, struct inode *dir,
3935 				   struct buffer_head *di_bh,
3936 				   struct buffer_head *dx_root_bh,
3937 				   const char *name, int namelen,
3938 				   struct ocfs2_dir_lookup_result *lookup)
3939 {
3940 	int ret, rebalanced = 0;
3941 	struct ocfs2_dx_root_block *dx_root;
3942 	struct buffer_head *dx_leaf_bh = NULL;
3943 	struct ocfs2_dx_leaf *dx_leaf;
3944 	u64 blkno;
3945 	u32 leaf_cpos;
3946 
3947 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
3948 
3949 restart_search:
3950 	ret = ocfs2_dx_dir_lookup(dir, &dx_root->dr_list, &lookup->dl_hinfo,
3951 				  &leaf_cpos, &blkno);
3952 	if (ret) {
3953 		mlog_errno(ret);
3954 		goto out;
3955 	}
3956 
3957 	ret = ocfs2_read_dx_leaf(dir, blkno, &dx_leaf_bh);
3958 	if (ret) {
3959 		mlog_errno(ret);
3960 		goto out;
3961 	}
3962 
3963 	dx_leaf = (struct ocfs2_dx_leaf *)dx_leaf_bh->b_data;
3964 
3965 	if (le16_to_cpu(dx_leaf->dl_list.de_num_used) >=
3966 	    le16_to_cpu(dx_leaf->dl_list.de_count)) {
3967 		if (rebalanced) {
3968 			/*
3969 			 * Rebalancing should have provided us with
3970 			 * space in an appropriate leaf.
3971 			 *
3972 			 * XXX: Is this an abnormal condition then?
3973 			 * Should we print a message here?
3974 			 */
3975 			ret = -ENOSPC;
3976 			goto out;
3977 		}
3978 
3979 		ret = ocfs2_dx_dir_rebalance(osb, dir, dx_root_bh, dx_leaf_bh,
3980 					     &lookup->dl_hinfo, leaf_cpos,
3981 					     blkno);
3982 		if (ret) {
3983 			if (ret != -ENOSPC)
3984 				mlog_errno(ret);
3985 			goto out;
3986 		}
3987 
3988 		/*
3989 		 * Restart the lookup. The rebalance might have
3990 		 * changed which block our item fits into. Mark our
3991 		 * progress, so we only execute this once.
3992 		 */
3993 		brelse(dx_leaf_bh);
3994 		dx_leaf_bh = NULL;
3995 		rebalanced = 1;
3996 		goto restart_search;
3997 	}
3998 
3999 	lookup->dl_dx_leaf_bh = dx_leaf_bh;
4000 	dx_leaf_bh = NULL;
4001 
4002 out:
4003 	brelse(dx_leaf_bh);
4004 	return ret;
4005 }
4006 
ocfs2_search_dx_free_list(struct inode * dir,struct buffer_head * dx_root_bh,int namelen,struct ocfs2_dir_lookup_result * lookup)4007 static int ocfs2_search_dx_free_list(struct inode *dir,
4008 				     struct buffer_head *dx_root_bh,
4009 				     int namelen,
4010 				     struct ocfs2_dir_lookup_result *lookup)
4011 {
4012 	int ret = -ENOSPC;
4013 	struct buffer_head *leaf_bh = NULL, *prev_leaf_bh = NULL;
4014 	struct ocfs2_dir_block_trailer *db;
4015 	u64 next_block;
4016 	int rec_len = OCFS2_DIR_REC_LEN(namelen);
4017 	struct ocfs2_dx_root_block *dx_root;
4018 
4019 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4020 	next_block = le64_to_cpu(dx_root->dr_free_blk);
4021 
4022 	while (next_block) {
4023 		brelse(prev_leaf_bh);
4024 		prev_leaf_bh = leaf_bh;
4025 		leaf_bh = NULL;
4026 
4027 		ret = ocfs2_read_dir_block_direct(dir, next_block, &leaf_bh);
4028 		if (ret) {
4029 			mlog_errno(ret);
4030 			goto out;
4031 		}
4032 
4033 		db = ocfs2_trailer_from_bh(leaf_bh, dir->i_sb);
4034 		if (rec_len <= le16_to_cpu(db->db_free_rec_len)) {
4035 			lookup->dl_leaf_bh = leaf_bh;
4036 			lookup->dl_prev_leaf_bh = prev_leaf_bh;
4037 			leaf_bh = NULL;
4038 			prev_leaf_bh = NULL;
4039 			break;
4040 		}
4041 
4042 		next_block = le64_to_cpu(db->db_free_next);
4043 	}
4044 
4045 	if (!next_block)
4046 		ret = -ENOSPC;
4047 
4048 out:
4049 
4050 	brelse(leaf_bh);
4051 	brelse(prev_leaf_bh);
4052 	return ret;
4053 }
4054 
ocfs2_expand_inline_dx_root(struct inode * dir,struct buffer_head * dx_root_bh)4055 static int ocfs2_expand_inline_dx_root(struct inode *dir,
4056 				       struct buffer_head *dx_root_bh)
4057 {
4058 	int ret, num_dx_leaves, i, j, did_quota = 0;
4059 	struct buffer_head **dx_leaves = NULL;
4060 	struct ocfs2_extent_tree et;
4061 	u64 insert_blkno;
4062 	struct ocfs2_alloc_context *data_ac = NULL;
4063 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4064 	handle_t *handle = NULL;
4065 	struct ocfs2_dx_root_block *dx_root;
4066 	struct ocfs2_dx_entry_list *entry_list;
4067 	struct ocfs2_dx_entry *dx_entry;
4068 	struct ocfs2_dx_leaf *target_leaf;
4069 
4070 	ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
4071 	if (ret) {
4072 		mlog_errno(ret);
4073 		goto out;
4074 	}
4075 
4076 	dx_leaves = ocfs2_dx_dir_kmalloc_leaves(osb->sb, &num_dx_leaves);
4077 	if (!dx_leaves) {
4078 		ret = -ENOMEM;
4079 		mlog_errno(ret);
4080 		goto out;
4081 	}
4082 
4083 	handle = ocfs2_start_trans(osb, ocfs2_calc_dxi_expand_credits(osb->sb));
4084 	if (IS_ERR(handle)) {
4085 		ret = PTR_ERR(handle);
4086 		mlog_errno(ret);
4087 		goto out;
4088 	}
4089 
4090 	ret = dquot_alloc_space_nodirty(dir,
4091 				       ocfs2_clusters_to_bytes(osb->sb, 1));
4092 	if (ret)
4093 		goto out_commit;
4094 	did_quota = 1;
4095 
4096 	/*
4097 	 * We do this up front, before the allocation, so that a
4098 	 * failure to add the dx_root_bh to the journal won't result
4099 	 * us losing clusters.
4100 	 */
4101 	ret = ocfs2_journal_access_dr(handle, INODE_CACHE(dir), dx_root_bh,
4102 				      OCFS2_JOURNAL_ACCESS_WRITE);
4103 	if (ret) {
4104 		mlog_errno(ret);
4105 		goto out_commit;
4106 	}
4107 
4108 	ret = __ocfs2_dx_dir_new_cluster(dir, 0, handle, data_ac, dx_leaves,
4109 					 num_dx_leaves, &insert_blkno);
4110 	if (ret) {
4111 		mlog_errno(ret);
4112 		goto out_commit;
4113 	}
4114 
4115 	/*
4116 	 * Transfer the entries from our dx_root into the appropriate
4117 	 * block
4118 	 */
4119 	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4120 	entry_list = &dx_root->dr_entries;
4121 
4122 	for (i = 0; i < le16_to_cpu(entry_list->de_num_used); i++) {
4123 		dx_entry = &entry_list->de_entries[i];
4124 
4125 		j = __ocfs2_dx_dir_hash_idx(osb,
4126 					    le32_to_cpu(dx_entry->dx_minor_hash));
4127 		target_leaf = (struct ocfs2_dx_leaf *)dx_leaves[j]->b_data;
4128 
4129 		ocfs2_dx_dir_leaf_insert_tail(target_leaf, dx_entry);
4130 
4131 		/* Each leaf has been passed to the journal already
4132 		 * via __ocfs2_dx_dir_new_cluster() */
4133 	}
4134 
4135 	dx_root->dr_flags &= ~OCFS2_DX_FLAG_INLINE;
4136 
4137 	dx_root->dr_list.l_tree_depth = 0;
4138 	dx_root->dr_list.l_count =
4139 		cpu_to_le16(ocfs2_extent_recs_per_dx_root(osb->sb));
4140 	dx_root->dr_list.l_next_free_rec = 0;
4141 	memset(&dx_root->dr_list.l_recs, 0,
4142 	       osb->sb->s_blocksize -
4143 	       (offsetof(struct ocfs2_dx_root_block, dr_list) +
4144 		offsetof(struct ocfs2_extent_list, l_recs)));
4145 
4146 	/* This should never fail considering we start with an empty
4147 	 * dx_root. */
4148 	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4149 	ret = ocfs2_insert_extent(handle, &et, 0, insert_blkno, 1, 0, NULL);
4150 	if (ret)
4151 		mlog_errno(ret);
4152 	did_quota = 0;
4153 
4154 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
4155 	ocfs2_journal_dirty(handle, dx_root_bh);
4156 
4157 out_commit:
4158 	if (ret < 0 && did_quota)
4159 		dquot_free_space_nodirty(dir,
4160 					  ocfs2_clusters_to_bytes(dir->i_sb, 1));
4161 
4162 	ocfs2_commit_trans(osb, handle);
4163 
4164 out:
4165 	if (data_ac)
4166 		ocfs2_free_alloc_context(data_ac);
4167 
4168 	if (dx_leaves) {
4169 		for (i = 0; i < num_dx_leaves; i++)
4170 			brelse(dx_leaves[i]);
4171 		kfree(dx_leaves);
4172 	}
4173 	return ret;
4174 }
4175 
ocfs2_inline_dx_has_space(struct buffer_head * dx_root_bh)4176 static int ocfs2_inline_dx_has_space(struct buffer_head *dx_root_bh)
4177 {
4178 	struct ocfs2_dx_root_block *dx_root;
4179 	struct ocfs2_dx_entry_list *entry_list;
4180 
4181 	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4182 	entry_list = &dx_root->dr_entries;
4183 
4184 	if (le16_to_cpu(entry_list->de_num_used) >=
4185 	    le16_to_cpu(entry_list->de_count))
4186 		return -ENOSPC;
4187 
4188 	return 0;
4189 }
4190 
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)4191 static int ocfs2_prepare_dx_dir_for_insert(struct inode *dir,
4192 					   struct buffer_head *di_bh,
4193 					   const char *name,
4194 					   int namelen,
4195 					   struct ocfs2_dir_lookup_result *lookup)
4196 {
4197 	int ret, free_dx_root = 1;
4198 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4199 	struct buffer_head *dx_root_bh = NULL;
4200 	struct buffer_head *leaf_bh = NULL;
4201 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4202 	struct ocfs2_dx_root_block *dx_root;
4203 
4204 	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4205 	if (ret) {
4206 		mlog_errno(ret);
4207 		goto out;
4208 	}
4209 
4210 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4211 	if (le32_to_cpu(dx_root->dr_num_entries) == OCFS2_DX_ENTRIES_MAX) {
4212 		ret = -ENOSPC;
4213 		mlog_errno(ret);
4214 		goto out;
4215 	}
4216 
4217 	if (ocfs2_dx_root_inline(dx_root)) {
4218 		ret = ocfs2_inline_dx_has_space(dx_root_bh);
4219 
4220 		if (ret == 0)
4221 			goto search_el;
4222 
4223 		/*
4224 		 * We ran out of room in the root block. Expand it to
4225 		 * an extent, then allow ocfs2_find_dir_space_dx to do
4226 		 * the rest.
4227 		 */
4228 		ret = ocfs2_expand_inline_dx_root(dir, dx_root_bh);
4229 		if (ret) {
4230 			mlog_errno(ret);
4231 			goto out;
4232 		}
4233 	}
4234 
4235 	/*
4236 	 * Insert preparation for an indexed directory is split into two
4237 	 * steps. The call to find_dir_space_dx reserves room in the index for
4238 	 * an additional item. If we run out of space there, it's a real error
4239 	 * we can't continue on.
4240 	 */
4241 	ret = ocfs2_find_dir_space_dx(osb, dir, di_bh, dx_root_bh, name,
4242 				      namelen, lookup);
4243 	if (ret) {
4244 		mlog_errno(ret);
4245 		goto out;
4246 	}
4247 
4248 search_el:
4249 	/*
4250 	 * Next, we need to find space in the unindexed tree. This call
4251 	 * searches using the free space linked list. If the unindexed tree
4252 	 * lacks sufficient space, we'll expand it below. The expansion code
4253 	 * is smart enough to add any new blocks to the free space list.
4254 	 */
4255 	ret = ocfs2_search_dx_free_list(dir, dx_root_bh, namelen, lookup);
4256 	if (ret && ret != -ENOSPC) {
4257 		mlog_errno(ret);
4258 		goto out;
4259 	}
4260 
4261 	/* Do this up here - ocfs2_extend_dir might need the dx_root */
4262 	lookup->dl_dx_root_bh = dx_root_bh;
4263 	free_dx_root = 0;
4264 
4265 	if (ret == -ENOSPC) {
4266 		ret = ocfs2_extend_dir(osb, dir, di_bh, 1, lookup, &leaf_bh);
4267 
4268 		if (ret) {
4269 			mlog_errno(ret);
4270 			goto out;
4271 		}
4272 
4273 		/*
4274 		 * We make the assumption here that new leaf blocks are added
4275 		 * to the front of our free list.
4276 		 */
4277 		lookup->dl_prev_leaf_bh = NULL;
4278 		lookup->dl_leaf_bh = leaf_bh;
4279 	}
4280 
4281 out:
4282 	if (free_dx_root)
4283 		brelse(dx_root_bh);
4284 	return ret;
4285 }
4286 
4287 /*
4288  * Get a directory ready for insert. Any directory allocation required
4289  * happens here. Success returns zero, and enough context in the dir
4290  * lookup result that ocfs2_add_entry() will be able complete the task
4291  * with minimal performance impact.
4292  */
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)4293 int ocfs2_prepare_dir_for_insert(struct ocfs2_super *osb,
4294 				 struct inode *dir,
4295 				 struct buffer_head *parent_fe_bh,
4296 				 const char *name,
4297 				 int namelen,
4298 				 struct ocfs2_dir_lookup_result *lookup)
4299 {
4300 	int ret;
4301 	unsigned int blocks_wanted = 1;
4302 	struct buffer_head *bh = NULL;
4303 
4304 	trace_ocfs2_prepare_dir_for_insert(
4305 		(unsigned long long)OCFS2_I(dir)->ip_blkno, namelen);
4306 
4307 	/*
4308 	 * Do this up front to reduce confusion.
4309 	 *
4310 	 * The directory might start inline, then be turned into an
4311 	 * indexed one, in which case we'd need to hash deep inside
4312 	 * ocfs2_find_dir_space_id(). Since
4313 	 * ocfs2_prepare_dx_dir_for_insert() also needs this hash
4314 	 * done, there seems no point in spreading out the calls. We
4315 	 * can optimize away the case where the file system doesn't
4316 	 * support indexing.
4317 	 */
4318 	if (ocfs2_supports_indexed_dirs(osb))
4319 		ocfs2_dx_dir_name_hash(dir, name, namelen, &lookup->dl_hinfo);
4320 
4321 	if (ocfs2_dir_indexed(dir)) {
4322 		ret = ocfs2_prepare_dx_dir_for_insert(dir, parent_fe_bh,
4323 						      name, namelen, lookup);
4324 		if (ret)
4325 			mlog_errno(ret);
4326 		goto out;
4327 	}
4328 
4329 	if (OCFS2_I(dir)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4330 		ret = ocfs2_find_dir_space_id(dir, parent_fe_bh, name,
4331 					      namelen, &bh, &blocks_wanted);
4332 	} else
4333 		ret = ocfs2_find_dir_space_el(dir, name, namelen, &bh);
4334 
4335 	if (ret && ret != -ENOSPC) {
4336 		mlog_errno(ret);
4337 		goto out;
4338 	}
4339 
4340 	if (ret == -ENOSPC) {
4341 		/*
4342 		 * We have to expand the directory to add this name.
4343 		 */
4344 		BUG_ON(bh);
4345 
4346 		ret = ocfs2_extend_dir(osb, dir, parent_fe_bh, blocks_wanted,
4347 				       lookup, &bh);
4348 		if (ret) {
4349 			if (ret != -ENOSPC)
4350 				mlog_errno(ret);
4351 			goto out;
4352 		}
4353 
4354 		BUG_ON(!bh);
4355 	}
4356 
4357 	lookup->dl_leaf_bh = bh;
4358 	bh = NULL;
4359 out:
4360 	brelse(bh);
4361 	return ret;
4362 }
4363 
ocfs2_dx_dir_remove_index(struct inode * dir,struct buffer_head * di_bh,struct buffer_head * dx_root_bh)4364 static int ocfs2_dx_dir_remove_index(struct inode *dir,
4365 				     struct buffer_head *di_bh,
4366 				     struct buffer_head *dx_root_bh)
4367 {
4368 	int ret;
4369 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4370 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4371 	struct ocfs2_dx_root_block *dx_root;
4372 	struct inode *dx_alloc_inode = NULL;
4373 	struct buffer_head *dx_alloc_bh = NULL;
4374 	handle_t *handle;
4375 	u64 blk;
4376 	u16 bit;
4377 	u64 bg_blkno;
4378 
4379 	dx_root = (struct ocfs2_dx_root_block *) dx_root_bh->b_data;
4380 
4381 	dx_alloc_inode = ocfs2_get_system_file_inode(osb,
4382 					EXTENT_ALLOC_SYSTEM_INODE,
4383 					le16_to_cpu(dx_root->dr_suballoc_slot));
4384 	if (!dx_alloc_inode) {
4385 		ret = -ENOMEM;
4386 		mlog_errno(ret);
4387 		goto out;
4388 	}
4389 	inode_lock(dx_alloc_inode);
4390 
4391 	ret = ocfs2_inode_lock(dx_alloc_inode, &dx_alloc_bh, 1);
4392 	if (ret) {
4393 		mlog_errno(ret);
4394 		goto out_mutex;
4395 	}
4396 
4397 	handle = ocfs2_start_trans(osb, OCFS2_DX_ROOT_REMOVE_CREDITS);
4398 	if (IS_ERR(handle)) {
4399 		ret = PTR_ERR(handle);
4400 		mlog_errno(ret);
4401 		goto out_unlock;
4402 	}
4403 
4404 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(dir), di_bh,
4405 				      OCFS2_JOURNAL_ACCESS_WRITE);
4406 	if (ret) {
4407 		mlog_errno(ret);
4408 		goto out_commit;
4409 	}
4410 
4411 	spin_lock(&OCFS2_I(dir)->ip_lock);
4412 	OCFS2_I(dir)->ip_dyn_features &= ~OCFS2_INDEXED_DIR_FL;
4413 	di->i_dyn_features = cpu_to_le16(OCFS2_I(dir)->ip_dyn_features);
4414 	spin_unlock(&OCFS2_I(dir)->ip_lock);
4415 	di->i_dx_root = cpu_to_le64(0ULL);
4416 	ocfs2_update_inode_fsync_trans(handle, dir, 1);
4417 
4418 	ocfs2_journal_dirty(handle, di_bh);
4419 
4420 	blk = le64_to_cpu(dx_root->dr_blkno);
4421 	bit = le16_to_cpu(dx_root->dr_suballoc_bit);
4422 	if (dx_root->dr_suballoc_loc)
4423 		bg_blkno = le64_to_cpu(dx_root->dr_suballoc_loc);
4424 	else
4425 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
4426 	ret = ocfs2_free_suballoc_bits(handle, dx_alloc_inode, dx_alloc_bh,
4427 				       bit, bg_blkno, 1);
4428 	if (ret)
4429 		mlog_errno(ret);
4430 
4431 out_commit:
4432 	ocfs2_commit_trans(osb, handle);
4433 
4434 out_unlock:
4435 	ocfs2_inode_unlock(dx_alloc_inode, 1);
4436 
4437 out_mutex:
4438 	inode_unlock(dx_alloc_inode);
4439 	brelse(dx_alloc_bh);
4440 out:
4441 	iput(dx_alloc_inode);
4442 	return ret;
4443 }
4444 
ocfs2_dx_dir_truncate(struct inode * dir,struct buffer_head * di_bh)4445 int ocfs2_dx_dir_truncate(struct inode *dir, struct buffer_head *di_bh)
4446 {
4447 	int ret;
4448 	unsigned int clen;
4449 	u32 major_hash = UINT_MAX, p_cpos, cpos;
4450 	u64 blkno;
4451 	struct ocfs2_super *osb = OCFS2_SB(dir->i_sb);
4452 	struct buffer_head *dx_root_bh = NULL;
4453 	struct ocfs2_dx_root_block *dx_root;
4454 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
4455 	struct ocfs2_cached_dealloc_ctxt dealloc;
4456 	struct ocfs2_extent_tree et;
4457 
4458 	ocfs2_init_dealloc_ctxt(&dealloc);
4459 
4460 	if (!ocfs2_dir_indexed(dir))
4461 		return 0;
4462 
4463 	ret = ocfs2_read_dx_root(dir, di, &dx_root_bh);
4464 	if (ret) {
4465 		mlog_errno(ret);
4466 		goto out;
4467 	}
4468 	dx_root = (struct ocfs2_dx_root_block *)dx_root_bh->b_data;
4469 
4470 	if (ocfs2_dx_root_inline(dx_root))
4471 		goto remove_index;
4472 
4473 	ocfs2_init_dx_root_extent_tree(&et, INODE_CACHE(dir), dx_root_bh);
4474 
4475 	/* XXX: What if dr_clusters is too large? */
4476 	while (le32_to_cpu(dx_root->dr_clusters)) {
4477 		ret = ocfs2_dx_dir_lookup_rec(dir, &dx_root->dr_list,
4478 					      major_hash, &cpos, &blkno, &clen);
4479 		if (ret) {
4480 			mlog_errno(ret);
4481 			goto out;
4482 		}
4483 
4484 		p_cpos = ocfs2_blocks_to_clusters(dir->i_sb, blkno);
4485 
4486 		ret = ocfs2_remove_btree_range(dir, &et, cpos, p_cpos, clen, 0,
4487 					       &dealloc, 0, false);
4488 		if (ret) {
4489 			mlog_errno(ret);
4490 			goto out;
4491 		}
4492 
4493 		if (cpos == 0)
4494 			break;
4495 
4496 		major_hash = cpos - 1;
4497 	}
4498 
4499 remove_index:
4500 	ret = ocfs2_dx_dir_remove_index(dir, di_bh, dx_root_bh);
4501 	if (ret) {
4502 		mlog_errno(ret);
4503 		goto out;
4504 	}
4505 
4506 	ocfs2_remove_from_cache(INODE_CACHE(dir), dx_root_bh);
4507 out:
4508 	ocfs2_schedule_truncate_log_flush(osb, 1);
4509 	ocfs2_run_deallocs(osb, &dealloc);
4510 
4511 	brelse(dx_root_bh);
4512 	return ret;
4513 }
4514