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