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