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